summaryrefslogtreecommitdiff
path: root/util/events.go
blob: 9e12c4f78b76e5e03d5c44c1b2e04f6f3e9bc86c (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
package util

// Event listener listens for the events produced by worker, worker pool or other servce.
type EventListener func(event interface{})

// EventHandler helps to broadcast events to multiple listeners.
type EventHandler struct {
	listeners []EventListener
}

// 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 EventListener) {
	eb.listeners = append(eb.listeners, listener)
}

// Push broadcast events across all event listeners.
func (eb *EventHandler) Push(e interface{}) {
	for _, listener := range eb.listeners {
		listener(e)
	}
}