forked from imkira/go-observer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty.go
83 lines (66 loc) · 1.61 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package observer
import (
"io"
"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 interface {
// Value returns the current value for this property.
Value() interface{}
// Update sets new values for this property.
// Updating with `io.EOF` will mark this property as "ended",
// once ended further calls to Update will no-op
Update(value ...interface{})
// Observe returns a newly created Stream for this property.
Observe() Stream
// End emits a io.EOF message, shortcut for `Property.Update(io.EOF)`
End()
// Done returns a channel that is closed when property reaches a EOF
Done() <-chan struct{}
}
// NewProperty creates a new Property with the initial value value.
// It returns the created Property.
func NewProperty(value interface{}) Property {
return &property{
state: newState(value),
done: make(chan struct{}),
}
}
type property struct {
sync.RWMutex
ended bool
done chan struct{}
state *state
}
func (p *property) Value() interface{} {
p.RLock()
defer p.RUnlock()
return p.state.value
}
func (p *property) Update(values ...interface{}) {
p.Lock()
defer p.Unlock()
if p.ended {
return
}
for _, value := range values {
if value == io.EOF {
p.ended = true
close(p.done)
}
p.state = p.state.update(value)
}
}
func (p *property) Observe() Stream {
p.RLock()
defer p.RUnlock()
return &stream{state: p.state}
}
func (p *property) End() {
p.Update(io.EOF)
}
func (p *property) Done() <-chan struct{} {
return p.done
}