forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer_list.go
119 lines (103 loc) · 2.93 KB
/
observer_list.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"sync"
"github.com/keybase/client/go/kbfs/kbfsmd"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/kbfs/tlfhandle"
"github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
// observerList is a thread-safe list of observers.
type observerList struct {
lock sync.RWMutex
observers []Observer
}
func newObserverList() *observerList {
return &observerList{}
}
// It's the caller's responsibility to make sure add isn't called
// twice for the same Observer.
func (ol *observerList) add(o Observer) {
ol.lock.Lock()
defer ol.lock.Unlock()
ol.observers = append(ol.observers, o)
}
func (ol *observerList) remove(o Observer) {
ol.lock.Lock()
defer ol.lock.Unlock()
for i, oldObs := range ol.observers {
if oldObs == o {
ol.observers = append(ol.observers[:i], ol.observers[i+1:]...)
return
}
}
}
func (ol *observerList) localChange(
ctx context.Context, node Node, write WriteRange) {
ol.lock.RLock()
defer ol.lock.RUnlock()
for _, o := range ol.observers {
o.LocalChange(ctx, node, write)
}
}
func (ol *observerList) batchChanges(
ctx context.Context, changes []NodeChange, affectedNodeIDs []NodeID) {
ol.lock.RLock()
defer ol.lock.RUnlock()
for _, o := range ol.observers {
o.BatchChanges(ctx, changes, affectedNodeIDs)
}
}
func (ol *observerList) tlfHandleChange(
ctx context.Context, newHandle *tlfhandle.Handle) {
ol.lock.RLock()
defer ol.lock.RUnlock()
for _, o := range ol.observers {
o.TlfHandleChange(ctx, newHandle)
}
}
// syncedTlfObserverList is a thread-safe list of synced TLF observers.
type syncedTlfObserverList struct {
lock sync.RWMutex
observers []SyncedTlfObserver
}
func newSyncedTlfObserverList() *syncedTlfObserverList {
return &syncedTlfObserverList{}
}
// It's the caller's responsibility to make sure add isn't called
// twice for the same SyncedTlfObserver.
func (stol *syncedTlfObserverList) add(o SyncedTlfObserver) {
stol.lock.Lock()
defer stol.lock.Unlock()
stol.observers = append(stol.observers, o)
}
func (stol *syncedTlfObserverList) remove(o SyncedTlfObserver) {
stol.lock.Lock()
defer stol.lock.Unlock()
for i, oldObs := range stol.observers {
if oldObs == o {
stol.observers = append(stol.observers[:i], stol.observers[i+1:]...)
return
}
}
}
func (stol *syncedTlfObserverList) fullSyncStarted(
ctx context.Context, tlfID tlf.ID, rev kbfsmd.Revision,
waitCh <-chan struct{}) {
stol.lock.RLock()
defer stol.lock.RUnlock()
for _, o := range stol.observers {
o.FullSyncStarted(ctx, tlfID, rev, waitCh)
}
}
func (stol *syncedTlfObserverList) syncModeChanged(
ctx context.Context, tlfID tlf.ID, newMode keybase1.FolderSyncMode) {
stol.lock.RLock()
defer stol.lock.RUnlock()
for _, o := range stol.observers {
o.SyncModeChanged(ctx, tlfID, newMode)
}
}