generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listeners.go
60 lines (49 loc) · 1.52 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
package clortho
import (
"container/list"
"sync"
)
// CancelListenerFunc removes the listener it's associated with and cancels any
// future events sent to that listener.
//
// A CancelListenerFunc is idempotent: after the first invocation, calling this
// closure will have no effect.
type CancelListenerFunc func()
// listeners is a generic container of listeners that is safe for concurrent access
// and concurrent dispatch of events through the visit method.
type listeners struct {
lock sync.Mutex
listeners *list.List
}
// cancelListener creates an idempotent closure that removes the given linked list element.
func (l *listeners) cancelListener(e *list.Element) CancelListenerFunc {
return func() {
l.lock.Lock()
defer l.lock.Unlock()
// NOTE: Remove is idempotent: it will not do anything if e is not in the list
l.listeners.Remove(e)
}
}
// addListener inserts a new listener into the list and returns a closure
// that will remove the listener from the list.
func (l *listeners) addListener(newListener interface{}) CancelListenerFunc {
l.lock.Lock()
defer l.lock.Unlock()
if l.listeners == nil {
l.listeners = list.New()
}
e := l.listeners.PushBack(newListener)
return l.cancelListener(e)
}
// visit applies the given closure to each listener in the list. This method
// is atomic with respect to addListener.
func (l *listeners) visit(f func(interface{})) {
l.lock.Lock()
defer l.lock.Unlock()
if l.listeners == nil {
return
}
for e := l.listeners.Front(); e != nil; e = e.Next() {
f(e.Value)
}
}