|
@ -1,9 +1,5 @@ |
|
|
package events |
|
|
package events |
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
eventsBufferSize = 1000 |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// An EventCache buffers events for a Fireable
|
|
|
// An EventCache buffers events for a Fireable
|
|
|
// All events are cached. Filtering happens on Flush
|
|
|
// All events are cached. Filtering happens on Flush
|
|
|
type EventCache struct { |
|
|
type EventCache struct { |
|
@ -14,8 +10,7 @@ type EventCache struct { |
|
|
// Create a new EventCache with an EventSwitch as backend
|
|
|
// Create a new EventCache with an EventSwitch as backend
|
|
|
func NewEventCache(evsw Fireable) *EventCache { |
|
|
func NewEventCache(evsw Fireable) *EventCache { |
|
|
return &EventCache{ |
|
|
return &EventCache{ |
|
|
evsw: evsw, |
|
|
|
|
|
events: make([]eventInfo, eventsBufferSize), |
|
|
|
|
|
|
|
|
evsw: evsw, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -27,7 +22,7 @@ type eventInfo struct { |
|
|
|
|
|
|
|
|
// Cache an event to be fired upon finality.
|
|
|
// Cache an event to be fired upon finality.
|
|
|
func (evc *EventCache) FireEvent(event string, data EventData) { |
|
|
func (evc *EventCache) FireEvent(event string, data EventData) { |
|
|
// append to list
|
|
|
|
|
|
|
|
|
// append to list (go will grow our backing array exponentially)
|
|
|
evc.events = append(evc.events, eventInfo{event, data}) |
|
|
evc.events = append(evc.events, eventInfo{event, data}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -37,5 +32,6 @@ func (evc *EventCache) Flush() { |
|
|
for _, ei := range evc.events { |
|
|
for _, ei := range evc.events { |
|
|
evc.evsw.FireEvent(ei.event, ei.data) |
|
|
evc.evsw.FireEvent(ei.event, ei.data) |
|
|
} |
|
|
} |
|
|
evc.events = make([]eventInfo, eventsBufferSize) |
|
|
|
|
|
|
|
|
// Clear the buffer, since we only add to it with append it's safe to just set it to nil and maybe safe an allocation
|
|
|
|
|
|
evc.events = nil |
|
|
} |
|
|
} |