-
Notifications
You must be signed in to change notification settings - Fork 0
/
listeners.go
182 lines (158 loc) · 3.57 KB
/
listeners.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
179
180
181
182
package db
import (
"fmt"
"sync"
"github.com/dcnetio/gothreads-lib/core/app"
core "github.com/dcnetio/gothreads-lib/core/db"
"github.com/dcnetio/gothreads-lib/core/thread"
format "github.com/ipfs/go-ipld-format"
)
// Listen returns a Listener which notifies about actions applying the
// defined filters. The DB *won't* wait for slow receivers, so if the
// channel is full, the action will be dropped.
func (d *DB) Listen(los ...ListenOption) (Listener, error) {
d.txnlock.Lock()
defer d.txnlock.Unlock()
if d.closed {
return nil, fmt.Errorf("can't listen on closed DB")
}
sl := &listener{
scn: d.stateChangedNotifee,
filters: los,
c: make(chan Action, 1),
}
d.stateChangedNotifee.addListener(sl)
return sl, nil
}
func (d *DB) notifyStateChanged(actions []Action) {
d.stateChangedNotifee.notify(actions)
}
func (d *DB) notifyTxnEvents(node format.Node, token thread.Token) error {
return d.localEventsBus.Send(&app.LocalEvent{
Node: node,
Token: token,
})
}
type ActionType int
type ListenActionType int
const (
ActionCreate ActionType = iota + 1
ActionSave
ActionDelete
)
const (
ListenAll ListenActionType = iota
ListenCreate
ListenSave
ListenDelete
)
type Action struct {
Collection string
Type ActionType
ID core.InstanceID
}
type ListenOption struct {
Type ListenActionType
Collection string
ID core.InstanceID
}
type Listener interface {
Channel() <-chan Action
Close()
}
type stateChangedNotifee struct {
lock sync.RWMutex
listeners []*listener
}
type listener struct {
scn *stateChangedNotifee
filters []ListenOption
c chan Action
}
var _ Listener = (*listener)(nil)
func (scn *stateChangedNotifee) notify(actions []Action) {
scn.lock.RLock()
defer scn.lock.RUnlock()
for _, a := range actions {
for _, l := range scn.listeners {
if l.evaluate(a) {
select {
case l.c <- a:
default:
log.Warnf("dropped action %v for reducer with filters %v", a, l.filters)
}
}
}
}
}
func (scn *stateChangedNotifee) addListener(sl *listener) {
scn.lock.Lock()
defer scn.lock.Unlock()
scn.listeners = append(scn.listeners, sl)
}
func (scn *stateChangedNotifee) remove(sl *listener) bool {
scn.lock.Lock()
defer scn.lock.Unlock()
for i := range scn.listeners {
if scn.listeners[i] == sl {
scn.listeners[i] = scn.listeners[len(scn.listeners)-1]
scn.listeners[len(scn.listeners)-1] = nil
scn.listeners = scn.listeners[:len(scn.listeners)-1]
return true
}
}
return false
}
func (scn *stateChangedNotifee) close() {
scn.lock.Lock()
defer scn.lock.Unlock()
for i := range scn.listeners {
close(scn.listeners[i].c)
scn.listeners[i] = nil
}
scn.listeners = nil
}
// Channel returns an unbuffered channel to receive
// db change notifications
func (sl *listener) Channel() <-chan Action {
return sl.c
}
// Close indicates that no further notifications will be received
// and ready for being garbage collected
func (sl *listener) Close() {
if ok := sl.scn.remove(sl); ok {
close(sl.c)
}
}
func (sl *listener) evaluate(a Action) bool {
if len(sl.filters) == 0 {
return true
}
for _, f := range sl.filters {
switch f.Type {
case ListenAll:
case ListenCreate:
if a.Type != ActionCreate {
continue
}
case ListenSave:
if a.Type != ActionSave {
continue
}
case ListenDelete:
if a.Type != ActionDelete {
continue
}
default:
panic("unknown action type")
}
if f.Collection != "" && f.Collection != a.Collection {
continue
}
if f.ID != core.EmptyInstanceID && f.ID != a.ID {
continue
}
return true
}
return false
}