event.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package raft
  2. const (
  3. StateChangeEventType = "stateChange"
  4. LeaderChangeEventType = "leaderChange"
  5. TermChangeEventType = "termChange"
  6. CommitEventType = "commit"
  7. AddPeerEventType = "addPeer"
  8. RemovePeerEventType = "removePeer"
  9. RemovedEventType = "removed"
  10. HeartbeatIntervalEventType = "heartbeatInterval"
  11. ElectionTimeoutThresholdEventType = "electionTimeoutThreshold"
  12. HeartbeatEventType = "heartbeat"
  13. )
  14. // Event represents an action that occurred within the Raft library.
  15. // Listeners can subscribe to event types by using the Server.AddEventListener() function.
  16. type Event interface {
  17. Type() string
  18. Source() interface{}
  19. Value() interface{}
  20. PrevValue() interface{}
  21. }
  22. // event is the concrete implementation of the Event interface.
  23. type event struct {
  24. typ string
  25. source interface{}
  26. value interface{}
  27. prevValue interface{}
  28. }
  29. // newEvent creates a new event.
  30. func newEvent(typ string, value interface{}, prevValue interface{}) *event {
  31. return &event{
  32. typ: typ,
  33. value: value,
  34. prevValue: prevValue,
  35. }
  36. }
  37. // Type returns the type of event that occurred.
  38. func (e *event) Type() string {
  39. return e.typ
  40. }
  41. // Source returns the object that dispatched the event.
  42. func (e *event) Source() interface{} {
  43. return e.source
  44. }
  45. // Value returns the current value associated with the event, if applicable.
  46. func (e *event) Value() interface{} {
  47. return e.value
  48. }
  49. // PrevValue returns the previous value associated with the event, if applicable.
  50. func (e *event) PrevValue() interface{} {
  51. return e.prevValue
  52. }