Monorepo for Tangled tangled.org
2

Configure Feed

Select the types of activity you want to include in your feed.

at icy/ytnwlw 711 B View raw
1package notifier 2 3import ( 4 "sync" 5) 6 7type Notifier struct { 8 subscribers map[chan struct{}]struct{} 9 mu sync.Mutex 10} 11 12func New() Notifier { 13 return Notifier{ 14 subscribers: make(map[chan struct{}]struct{}), 15 } 16} 17 18func (n *Notifier) Subscribe() chan struct{} { 19 ch := make(chan struct{}, 1) 20 n.mu.Lock() 21 n.subscribers[ch] = struct{}{} 22 n.mu.Unlock() 23 return ch 24} 25 26func (n *Notifier) Unsubscribe(ch chan struct{}) { 27 n.mu.Lock() 28 delete(n.subscribers, ch) 29 close(ch) 30 n.mu.Unlock() 31} 32 33func (n *Notifier) NotifyAll() { 34 if n == nil { 35 return 36 } 37 n.mu.Lock() 38 for ch := range n.subscribers { 39 select { 40 case ch <- struct{}{}: 41 default: 42 // avoid blocking if channel is full 43 } 44 } 45 n.mu.Unlock() 46}