-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
184 lines (152 loc) · 2.93 KB
/
manager.go
File metadata and controls
184 lines (152 loc) · 2.93 KB
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
183
184
package manager
import (
"sync"
)
type Manager struct {
sync.Mutex
sync.Map
notifyChan chan Notify
sort func([]Entry) []Entry
}
func (c *Manager) notifyOperate(e Entry, op Operate) {
if c.notifyChan == nil {
return
}
c.notifyChan <- Notify{
Entry: e,
Operate: op,
}
}
func (c *Manager) NotifyChan() chan<- Notify {
return c.notifyChan
}
func (c *Manager) notifyClose() {
if c.notifyChan != nil {
close(c.notifyChan)
c.notifyChan = nil
}
}
func (c *Manager) NotifyClose() {
c.Lock()
defer c.Unlock()
c.notifyClose()
}
func (c *Manager) NotifyRegisterHandler(handler func(ch <-chan Notify)) {
c.Lock()
defer c.Unlock()
c.notifyClose()
c.notifyChan = make(chan Notify)
if handler == nil {
handler = func(ch <-chan Notify) {
for range ch {
}
}
}
go handler(c.notifyChan)
}
func (c *Manager) SortRegisterHandler(sort func([]Entry) []Entry) {
c.sort = sort
}
func (c *Manager) Get(key interface{}) Entry {
v, ok := c.Map.Load(key)
if !ok {
return nil
}
if e, ok := v.(Entry); ok {
return e
} else {
return nil
}
}
func (c *Manager) GetAll() (entries []Entry) {
c.Map.Range(func(k, v interface{}) bool {
if s, ok := v.(Entry); ok {
entries = append(entries, s)
}
return true
})
if sort := c.sort; sort != nil {
return sort(entries)
}
return entries
}
func (c *Manager) Traverse(handler func(e interface{}), Sort ...func()) {
if handler == nil {
return
}
c.Map.Range(func(k, v interface{}) bool {
handler(v)
return true
})
if len(Sort) > 0 && Sort[0] != nil {
Sort[0]()
}
}
func (c *Manager) update(e Entry) bool {
if old := c.Get(e.Key()); old == nil {
return false
} else {
old.Copy(e)
old.UpdateAfter()
c.notifyOperate(old, NotifyUpdate)
return true
}
}
func (c *Manager) Update(e Entry) bool {
c.Lock()
defer c.Unlock()
return c.update(e)
}
func (c *Manager) Add(e Entry) bool {
c.Lock()
defer c.Unlock()
if c.update(e) {
return false
}
c.Map.Store(e.Key(), e)
e.AddAfter()
c.notifyOperate(e, NotifyAdd)
return true
}
func (c *Manager) Delete(key interface{}) Entry {
c.Lock()
defer c.Unlock()
e := c.Get(key) // just for other use after delete it
c.Map.Delete(key)
if e != nil {
e.DeleteAfter()
c.notifyOperate(e, NotifyDelete)
}
return e
}
var Default = &Manager{}
func NotifyChan() chan<- Notify {
return Default.NotifyChan()
}
func SortRegisterHandler(sort func([]Entry) []Entry) {
Default.SortRegisterHandler(sort)
}
func NotifyClose() {
Default.NotifyClose()
}
func NotifyRegisterHandler(handler func(ch <-chan Notify)) {
Default.NotifyRegisterHandler(handler)
}
func Get(key interface{}) Entry {
return Default.Get(key)
}
func GetAll() (entries []Entry) {
return Default.GetAll()
}
func Traverse(handler func(e interface{})) {
Default.Traverse(handler)
}
func Update(e Entry) bool {
return Default.Update(e)
}
func Add(e Entry) bool {
return Default.Add(e)
}
func Delete(key interface{}) Entry {
return Default.Delete(key)
}