-
Notifications
You must be signed in to change notification settings - Fork 3
/
qtree_sub.go
57 lines (47 loc) · 1007 Bytes
/
qtree_sub.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
package qtree
import (
"sync"
)
type QTNodeOperation int
const (
Operation_AddChild QTNodeOperation = iota
Operation_DelChild
Operation_Delete
Operation_Error
)
// A update to a QueryTreeNode
type QTNodeUpdate struct {
Operation QTNodeOperation
Child *QueryTreeNode
}
type qtNodeSubscription struct {
id uint32
node *QueryTreeNode
mtx sync.RWMutex
chChans []chan<- *QTNodeUpdate
}
func (sub *qtNodeSubscription) nextChange(upd *QTNodeUpdate) {
sub.mtx.RLock()
defer sub.mtx.RUnlock()
for _, ch := range sub.chChans {
select {
case ch <- upd:
default:
}
}
}
func (sub *qtNodeSubscription) Changes() <-chan *QTNodeUpdate {
nch := make(chan *QTNodeUpdate, 50)
sub.mtx.Lock()
sub.chChans = append(sub.chChans, nch)
sub.mtx.Unlock()
return nch
}
func (sub *qtNodeSubscription) Unsubscribe() {
sub.node.removeSubscription(sub.id)
}
// A subscription to changes to the node
type QTNodeSubscription interface {
Changes() <-chan *QTNodeUpdate
Unsubscribe()
}