forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocketlist.go
47 lines (40 loc) · 982 Bytes
/
websocketlist.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
package server
import (
"sync"
)
type WebsocketList struct {
items []*WebsocketSubscriber
mu sync.RWMutex
}
func NewWebsocketList() *WebsocketList {
return &WebsocketList{}
}
func (l *WebsocketList) Add(w *WebsocketSubscriber) {
l.mu.Lock()
defer l.mu.Unlock()
l.items = append(l.items, w)
}
func (l *WebsocketList) Remove(w *WebsocketSubscriber) {
l.mu.Lock()
defer l.mu.Unlock()
for i, item := range l.items {
if item == w {
l.items = append(l.items[:i], l.items[i+1:]...)
return
}
}
}
// Operate on all websockets in the list.
//
// While the ForEach is running, the list may not be modified.
//
// In the future, it might make sense allow modification of the list while the
// foreach runs, but then we'd need additional synchronization to make sure
// we don't get websocket send() after removal.
func (l *WebsocketList) ForEach(f func(w *WebsocketSubscriber)) {
l.mu.RLock()
defer l.mu.RUnlock()
for _, item := range l.items {
f(item)
}
}