blob: 92dc103a2cf414807a6385a1b5a80df730ef2b67 (
plain)
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
|
package events
import (
"sync"
"github.com/spiral/roadrunner/v2/interfaces/events"
)
// EventHandler helps to broadcast events to multiple listeners.
type EventHandler struct {
listeners []events.EventListener
sync.RWMutex
}
func NewEventsHandler() events.Handler {
return &EventHandler{listeners: make([]events.EventListener, 0, 2)}
}
// NumListeners returns number of event listeners.
func (eb *EventHandler) NumListeners() int {
return len(eb.listeners)
}
// AddListener registers new event listener.
func (eb *EventHandler) AddListener(listener events.EventListener) {
eb.Lock()
defer eb.Unlock()
eb.listeners = append(eb.listeners, listener)
}
// Push broadcast events across all event listeners.
func (eb *EventHandler) Push(e interface{}) {
eb.Lock()
defer eb.Unlock()
for k := range eb.listeners {
eb.listeners[k](e)
}
}
|