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.

50 lines
1.3 KiB

  1. package types
  2. // Interface assertions
  3. var _ TxEventPublisher = (*TxEventBuffer)(nil)
  4. // TxEventBuffer is a buffer of events, which uses a slice to temporarily store
  5. // events.
  6. type TxEventBuffer struct {
  7. next TxEventPublisher
  8. capacity int
  9. events []EventDataTx
  10. }
  11. // NewTxEventBuffer accepts a TxEventPublisher and returns a new buffer with the given
  12. // capacity.
  13. func NewTxEventBuffer(next TxEventPublisher, capacity int) *TxEventBuffer {
  14. return &TxEventBuffer{
  15. next: next,
  16. capacity: capacity,
  17. events: make([]EventDataTx, 0, capacity),
  18. }
  19. }
  20. // Len returns the number of events cached.
  21. func (b TxEventBuffer) Len() int {
  22. return len(b.events)
  23. }
  24. // PublishEventTx buffers an event to be fired upon finality.
  25. func (b *TxEventBuffer) PublishEventTx(e EventDataTx) error {
  26. b.events = append(b.events, e)
  27. return nil
  28. }
  29. // Flush publishes events by running next.PublishWithTags on all cached events.
  30. // Blocks. Clears cached events.
  31. func (b *TxEventBuffer) Flush() error {
  32. for _, e := range b.events {
  33. err := b.next.PublishEventTx(e)
  34. if err != nil {
  35. return err
  36. }
  37. }
  38. // Clear out the elements and set the length to 0
  39. // but maintain the underlying slice's capacity.
  40. // See Issue https://github.com/tendermint/tendermint/issues/1189
  41. b.events = b.events[:0]
  42. return nil
  43. }