github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/utils/event_emitter.go (about) 1 // Copyright 2023 LiveKit, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package utils 16 17 import ( 18 "container/list" 19 "sync" 20 21 "golang.org/x/exp/maps" 22 23 "github.com/livekit/protocol/logger" 24 ) 25 26 const defaultQueueSize = 16 27 28 type EventEmitterParams struct { 29 QueueSize int 30 Logger logger.Logger 31 } 32 33 func DefaultEventEmitterParams() EventEmitterParams { 34 return EventEmitterParams{ 35 QueueSize: defaultQueueSize, 36 Logger: logger.GetLogger(), 37 } 38 } 39 40 type EventEmitter[K comparable, V any] struct { 41 params EventEmitterParams 42 mu sync.RWMutex 43 observers map[K]*EventObserverList[V] 44 } 45 46 func NewEventEmitter[K comparable, V any](params EventEmitterParams) *EventEmitter[K, V] { 47 return &EventEmitter[K, V]{ 48 params: params, 49 observers: map[K]*EventObserverList[V]{}, 50 } 51 } 52 53 func NewDefaultEventEmitter[K comparable, V any]() *EventEmitter[K, V] { 54 return NewEventEmitter[K, V](DefaultEventEmitterParams()) 55 } 56 57 func (e *EventEmitter[K, V]) Emit(k K, v V) { 58 e.mu.RLock() 59 l, ok := e.observers[k] 60 e.mu.RUnlock() 61 if !ok { 62 return 63 } 64 65 l.Emit(v) 66 } 67 68 func (e *EventEmitter[K, V]) Observe(k K) *EventObserver[V] { 69 e.mu.Lock() 70 l, ok := e.observers[k] 71 if !ok { 72 l = NewEventObserverList[V](e.params) 73 e.observers[k] = l 74 } 75 o := l.Observe() 76 e.mu.Unlock() 77 78 stop := o.stop 79 o.stop = func() { 80 stop() 81 e.cleanUpObserverList(k) 82 } 83 84 return o 85 } 86 87 func (e *EventEmitter[K, V]) ObservedKeys() []K { 88 e.mu.Lock() 89 defer e.mu.Unlock() 90 return maps.Keys(e.observers) 91 } 92 93 func (e *EventEmitter[K, V]) cleanUpObserverList(k K) { 94 e.mu.Lock() 95 defer e.mu.Unlock() 96 97 l, ok := e.observers[k] 98 if ok && l.Len() == 0 { 99 delete(e.observers, k) 100 } 101 } 102 103 type EventObserverList[V any] struct { 104 params EventEmitterParams 105 mu sync.RWMutex 106 observers *list.List 107 } 108 109 func NewEventObserverList[V any](params EventEmitterParams) *EventObserverList[V] { 110 return &EventObserverList[V]{ 111 params: params, 112 observers: list.New(), 113 } 114 } 115 116 func NewDefaultEventObserverList[V any]() *EventObserverList[V] { 117 return NewEventObserverList[V](DefaultEventEmitterParams()) 118 } 119 120 func (l *EventObserverList[V]) Len() int { 121 l.mu.RLock() 122 defer l.mu.RUnlock() 123 return l.observers.Len() 124 } 125 126 func (l *EventObserverList[V]) Observe() *EventObserver[V] { 127 o := &EventObserver[V]{ 128 logger: l.params.Logger, 129 ch: make(chan V, l.params.QueueSize), 130 } 131 132 l.mu.Lock() 133 le := l.observers.PushBack(o) 134 l.mu.Unlock() 135 136 o.stop = func() { l.stopObserving(le) } 137 138 return o 139 } 140 141 func (l *EventObserverList[V]) Emit(v V) { 142 l.mu.RLock() 143 defer l.mu.RUnlock() 144 for le := l.observers.Front(); le != nil; le = le.Next() { 145 le.Value.(*EventObserver[V]).emit(v) 146 } 147 } 148 149 func (l *EventObserverList[V]) stopObserving(le *list.Element) { 150 l.mu.Lock() 151 defer l.mu.Unlock() 152 l.observers.Remove(le) 153 } 154 155 type EventObserver[V any] struct { 156 logger logger.Logger 157 stop func() 158 ch chan V 159 } 160 161 func NewEventObserver[V any](stopFunc func()) (*EventObserver[V], func(v V)) { 162 o := &EventObserver[V]{ 163 logger: logger.GetLogger(), 164 stop: stopFunc, 165 ch: make(chan V, defaultQueueSize), 166 } 167 return o, o.emit 168 } 169 170 func (o *EventObserver[V]) emit(v V) { 171 select { 172 case o.ch <- v: 173 default: 174 o.logger.Warnw("could not add event to observer queue", nil) 175 } 176 } 177 178 func (o *EventObserver[V]) Stop() { 179 o.stop() 180 } 181 182 func (o *EventObserver[V]) Events() <-chan V { 183 return o.ch 184 }