From 1652dc8b3f7780079aa98c3ce20a83ee90b9758b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 10 Oct 2016 03:20:42 -0400 Subject: [PATCH] EventSwitch is an interface --- events.go | 37 +++++++++++++++++++++++-------------- events_test.go | 2 +- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/events.go b/events.go index 4c8604a25..9d4f2fa2e 100644 --- a/events.go +++ b/events.go @@ -15,7 +15,7 @@ type EventData interface { // reactors and other modules should export // this interface to become eventable type Eventable interface { - SetEventSwitch(evsw *EventSwitch) + SetEventSwitch(evsw EventSwitch) } // an event switch or cache implements fireable @@ -23,7 +23,16 @@ type Fireable interface { FireEvent(event string, data EventData) } -type EventSwitch struct { +type EventSwitch interface { + Service + Fireable + + AddListenerForEvent(listenerID, event string, cb EventCallback) + RemoveListenerForEvent(event string, listenerID string) + RemoveListener(listenerID string) +} + +type eventSwitch struct { BaseService mtx sync.RWMutex @@ -31,26 +40,26 @@ type EventSwitch struct { listeners map[string]*eventListener } -func NewEventSwitch() *EventSwitch { - evsw := &EventSwitch{} +func NewEventSwitch() EventSwitch { + evsw := &eventSwitch{} evsw.BaseService = *NewBaseService(log, "EventSwitch", evsw) return evsw } -func (evsw *EventSwitch) OnStart() error { +func (evsw *eventSwitch) OnStart() error { evsw.BaseService.OnStart() evsw.eventCells = make(map[string]*eventCell) evsw.listeners = make(map[string]*eventListener) return nil } -func (evsw *EventSwitch) OnStop() { +func (evsw *eventSwitch) OnStop() { evsw.BaseService.OnStop() evsw.eventCells = nil evsw.listeners = nil } -func (evsw *EventSwitch) AddListenerForEvent(listenerID, event string, cb eventCallback) { +func (evsw *eventSwitch) AddListenerForEvent(listenerID, event string, cb EventCallback) { // Get/Create eventCell and listener evsw.mtx.Lock() eventCell := evsw.eventCells[event] @@ -70,7 +79,7 @@ func (evsw *EventSwitch) AddListenerForEvent(listenerID, event string, cb eventC listener.AddEvent(event) } -func (evsw *EventSwitch) RemoveListener(listenerID string) { +func (evsw *eventSwitch) RemoveListener(listenerID string) { // Get and remove listener evsw.mtx.RLock() listener := evsw.listeners[listenerID] @@ -90,7 +99,7 @@ func (evsw *EventSwitch) RemoveListener(listenerID string) { } } -func (evsw *EventSwitch) RemoveListenerForEvent(event string, listenerID string) { +func (evsw *eventSwitch) RemoveListenerForEvent(event string, listenerID string) { // Get eventCell evsw.mtx.Lock() eventCell := evsw.eventCells[event] @@ -116,7 +125,7 @@ func (evsw *EventSwitch) RemoveListenerForEvent(event string, listenerID string) } } -func (evsw *EventSwitch) FireEvent(event string, data EventData) { +func (evsw *eventSwitch) FireEvent(event string, data EventData) { // Get the eventCell evsw.mtx.RLock() eventCell := evsw.eventCells[event] @@ -135,16 +144,16 @@ func (evsw *EventSwitch) FireEvent(event string, data EventData) { // eventCell handles keeping track of listener callbacks for a given event. type eventCell struct { mtx sync.RWMutex - listeners map[string]eventCallback + listeners map[string]EventCallback } func newEventCell() *eventCell { return &eventCell{ - listeners: make(map[string]eventCallback), + listeners: make(map[string]EventCallback), } } -func (cell *eventCell) AddListener(listenerID string, cb eventCallback) { +func (cell *eventCell) AddListener(listenerID string, cb EventCallback) { cell.mtx.Lock() cell.listeners[listenerID] = cb cell.mtx.Unlock() @@ -168,7 +177,7 @@ func (cell *eventCell) FireEvent(data EventData) { //----------------------------------------------------------------------------- -type eventCallback func(data EventData) +type EventCallback func(data EventData) type eventListener struct { id string diff --git a/events_test.go b/events_test.go index ecfe6068c..8766a57d2 100644 --- a/events_test.go +++ b/events_test.go @@ -322,7 +322,7 @@ func sumReceivedNumbers(numbers, doneSum chan uint64) { // to `offset` + 999. It additionally returns the addition of all integers // sent on `doneChan` for assertion that all events have been sent, and enabling // the test to assert all events have also been received. -func fireEvents(evsw *EventSwitch, event string, doneChan chan uint64, +func fireEvents(evsw EventSwitch, event string, doneChan chan uint64, offset uint64) { var sentSum uint64 = 0 for i := offset; i <= offset+uint64(999); i++ {