diff options
Diffstat (limited to 'pkg/events')
-rwxr-xr-x | pkg/events/events.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/pkg/events/events.go b/pkg/events/events.go new file mode 100755 index 00000000..92dc103a --- /dev/null +++ b/pkg/events/events.go @@ -0,0 +1,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) + } +} |