-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.go
178 lines (150 loc) · 4.45 KB
/
subscriber.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package subscriber
import (
"sync"
"sync/atomic"
)
// Error type to allow constant error declarations.
type Error string
// Error method implements Error interface.
func (e Error) Error() string {
return string(e)
}
// Unsubscribed is the error returned by wait when the Unsubscribe method
// is called on the subscription.
const ErrUnsubscribed = Error("subscriber unsubscribed")
// Subscription is an interface that allows code to monitor and control a
// subscription it received.
type Subscription interface {
// Subscribed returns true when the subscription is currently active.
Subscribed() bool
// Unsubscribe will do nothing if the subscription is not active. If the
// state is still active however, it will be changed to canceled.
// Subsequently, it will call Unsubscribe on all child subscriptions added
// through Add, along with all methods added through OnUnsubscribe. When the
// subscription is canceled by calling Unsubscribe a call to the Wait method
// will return the error ErrUnsubscribed.
Unsubscribe()
// Canceled returns true when the subscription state is canceled.
Canceled() bool
// Wait will by default block the calling goroutine and wait for the
// Unsubscribe method to be called on this subscription.
// However, when OnWait was called with a callback wait function it will
// call that instead. Calling Wait on a subscription that has already been
// canceled will return immediately. If the subscriber was canceled by
// calling Unsubscribe, then the error returned is ErrUnsubscribed.
// If the subscriber was terminated by calling Done, then the error
// returned here is the one passed to Done.
Wait() error
}
// Subscriber is a Subscription with management functionality.
type Subscriber interface {
// A Subscriber is also a Subscription.
Subscription
// Add will create and return a new child Subscriber setup in such a way that
// calling Unsubscribe on the parent will also call Unsubscribe on the child.
// Calling the Unsubscribe method on the child will NOT propagate to the
// parent!
Add() Subscriber
// OnUnsubscribe will add the given callback function to the subscriber.
// The callback will be called when either the Unsubscribe of the parent
// or of the subscriber itself is called. If the subscription was already
// canceled, then the callback function will just be called immediately.
OnUnsubscribe(callback func())
// OnWait will register a callback to call when subscription Wait is called.
OnWait(callback func())
// Done will set the error internally and then cancel the subscription by
// calling the Unsubscribe method. A nil value for error indicates success.
Done(err error)
// Error returns the error set by calling the Done(err) method. As long as
// the subscriber is still subscribed Error will return nil.
Error() error
}
// New will create and return a new Subscriber.
func New() Subscriber {
return &subscriber{err: ErrUnsubscribed}
}
const (
subscribed = iota
unsubscribed
)
type subscriber struct {
state int32
sync.Mutex
callbacks []func()
onWait func()
err error
}
func (s *subscriber) Subscribed() bool {
return atomic.LoadInt32(&s.state) == subscribed
}
func (s *subscriber) Unsubscribe() {
if atomic.CompareAndSwapInt32(&s.state, subscribed, unsubscribed) {
s.Lock()
for _, cb := range s.callbacks {
cb()
}
s.callbacks = nil
s.Unlock()
}
}
func (s *subscriber) Canceled() bool {
return atomic.LoadInt32(&s.state) != subscribed
}
func (s *subscriber) Wait() error {
s.Lock()
wait := s.onWait
s.Unlock()
if wait != nil {
wait()
}
if atomic.LoadInt32(&s.state) == subscribed {
var wg sync.WaitGroup
wg.Add(1)
s.OnUnsubscribe(wg.Done)
wg.Wait()
}
return s.Error()
}
func (s *subscriber) Add() Subscriber {
child := New()
s.Lock()
if atomic.LoadInt32(&s.state) != subscribed {
child.Unsubscribe()
} else {
s.callbacks = append(s.callbacks, child.Unsubscribe)
}
s.Unlock()
return child
}
func (s *subscriber) OnUnsubscribe(callback func()) {
if callback == nil {
return
}
s.Lock()
if atomic.LoadInt32(&s.state) == subscribed {
s.callbacks = append(s.callbacks, callback)
} else {
callback()
}
s.Unlock()
}
func (s *subscriber) OnWait(callback func()) {
s.Lock()
s.onWait = callback
s.Unlock()
}
func (s *subscriber) Done(err error) {
s.Lock()
s.err = err
s.Unlock()
s.Unsubscribe()
}
func (s *subscriber) Error() error {
s.Lock()
err := s.err
s.Unlock()
if atomic.LoadInt32(&s.state) == subscribed {
err = nil
}
return err
}