You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1000 B

  1. package events
  2. import (
  3. "github.com/tendermint/tendermint/types"
  4. )
  5. const (
  6. eventsBufferSize = 1000
  7. )
  8. // An EventCache buffers events for a Fireable
  9. // All events are cached. Filtering happens on Flush
  10. type EventCache struct {
  11. evsw Fireable
  12. events []eventInfo
  13. }
  14. // Create a new EventCache with an EventSwitch as backend
  15. func NewEventCache(evsw Fireable) *EventCache {
  16. return &EventCache{
  17. evsw: evsw,
  18. events: make([]eventInfo, eventsBufferSize),
  19. }
  20. }
  21. // a cached event
  22. type eventInfo struct {
  23. event string
  24. data types.EventData
  25. }
  26. // Cache an event to be fired upon finality.
  27. func (evc *EventCache) FireEvent(event string, data types.EventData) {
  28. // append to list
  29. evc.events = append(evc.events, eventInfo{event, data})
  30. }
  31. // Fire events by running evsw.FireEvent on all cached events. Blocks.
  32. // Clears cached events
  33. func (evc *EventCache) Flush() {
  34. for _, ei := range evc.events {
  35. evc.evsw.FireEvent(ei.event, ei.data)
  36. }
  37. evc.events = make([]eventInfo, eventsBufferSize)
  38. }