-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsync.go
More file actions
47 lines (39 loc) · 859 Bytes
/
sync.go
File metadata and controls
47 lines (39 loc) · 859 Bytes
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
package arksdk
import (
"sync"
"github.com/arkade-os/go-sdk/types"
)
type syncListeners struct {
lock *sync.RWMutex
listeners map[chan types.SyncEvent]struct{}
}
func newReadyListeners() *syncListeners {
return &syncListeners{
lock: &sync.RWMutex{},
listeners: make(map[chan types.SyncEvent]struct{}),
}
}
func (l *syncListeners) add(ch chan types.SyncEvent) {
l.lock.Lock()
defer l.lock.Unlock()
l.listeners[ch] = struct{}{}
}
func (l *syncListeners) broadcast(err error) {
l.lock.RLock()
defer l.lock.RUnlock()
event := types.SyncEvent{Synced: err == nil, Err: err}
for ch := range l.listeners {
select {
case ch <- event:
default:
}
}
}
func (l *syncListeners) clear() {
l.lock.Lock()
defer l.lock.Unlock()
for ch := range l.listeners {
close(ch)
}
l.listeners = make(map[chan types.SyncEvent]struct{})
}