tendermint/events/event_cache.go

46 lines
1000 B
Go
Raw Normal View History

2015-04-15 23:40:27 -07:00
package events
import (
"github.com/tendermint/tendermint/types"
)
2015-04-15 23:40:27 -07:00
const (
eventsBufferSize = 1000
)
// An EventCache buffers events for a Fireable
// All events are cached. Filtering happens on Flush
type EventCache struct {
evsw Fireable
events []eventInfo
}
// Create a new EventCache with an EventSwitch as backend
func NewEventCache(evsw Fireable) *EventCache {
return &EventCache{
evsw: evsw,
events: make([]eventInfo, eventsBufferSize),
}
}
// a cached event
type eventInfo struct {
event string
data types.EventData
2015-04-15 23:40:27 -07:00
}
// Cache an event to be fired upon finality.
func (evc *EventCache) FireEvent(event string, data types.EventData) {
2015-04-15 23:40:27 -07:00
// append to list
evc.events = append(evc.events, eventInfo{event, data})
2015-04-15 23:40:27 -07:00
}
// Fire events by running evsw.FireEvent on all cached events. Blocks.
2015-04-17 13:18:50 -07:00
// Clears cached events
2015-04-15 23:40:27 -07:00
func (evc *EventCache) Flush() {
for _, ei := range evc.events {
evc.evsw.FireEvent(ei.event, ei.data)
2015-04-15 23:40:27 -07:00
}
2015-04-17 13:18:50 -07:00
evc.events = make([]eventInfo, eventsBufferSize)
2015-04-15 23:40:27 -07:00
}