event.go 1.4 KB

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