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