diff options
Diffstat (limited to 'util/events.go')
-rwxr-xr-x | util/events.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/util/events.go b/util/events.go new file mode 100755 index 00000000..43a3e646 --- /dev/null +++ b/util/events.go @@ -0,0 +1,36 @@ +package util + +type EventsHandler interface { + NumListeners() int + AddListener(listener EventListener) + Push(e interface{}) +} + +// Event listener listens for the events produced by worker, worker pool or other service. +type EventListener func(event interface{}) + +// EventHandler helps to broadcast events to multiple listeners. +type EventHandler struct { + listeners []EventListener +} + +func NewEventsHandler() EventsHandler { + return &EventHandler{listeners: make([]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 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) + } +} |