Skip to content

Commit

Permalink
Merge pull request #7 from tendermint/develop
Browse files Browse the repository at this point in the history
EventSwitch is an interface
  • Loading branch information
ebuchman committed Oct 21, 2016
2 parents 48fa215 + 1652dc8 commit 1c85cb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
37 changes: 23 additions & 14 deletions events.go
Expand Up @@ -15,42 +15,51 @@ 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
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
eventCells map[string]*eventCell
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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion events_test.go
Expand Up @@ -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++ {
Expand Down

0 comments on commit 1c85cb9

Please sign in to comment.