event.go 1.5 KB

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