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.

41 lines
935 B

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