Debian Codegraph @

property.go

raw symbols SCIP
1 package observer
2
3 import "sync"
4
5 // Property is an object that is continuously updated by one or more
6 // publishers. It is completely goroutine safe: you can use Property
7 // concurrently from multiple goroutines.
8 type Property interface {
9 // Value returns the current value for this property.
10 Value() interface{}
11
12 // Update sets a new value for this property.
13 Update(value interface{})
14
15 // Observe returns a newly created Stream for this property.
16 Observe() Stream
17 }
18
19 // NewProperty creates a new Property with the initial value value.
20 // It returns the created Property.
21 func NewProperty(value interface{}) Property {
22 return &property{state: newState(value)}
23 }
24
25 type property struct {
26 sync.RWMutex
27 state *state
28 }
29
30 func (p *property) Value() interface{} {
31 p.RLock()
32 defer p.RUnlock()
33 return p.state.value
34 }
35
36 func (p *property) Update(value interface{}) {
37 p.Lock()
38 defer p.Unlock()
39 p.state = p.state.update(value)
40 }
41
42 func (p *property) Observe() Stream {
43 p.RLock()
44 defer p.RUnlock()
45 return &stream{state: p.state}
46 }