event.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. HeartbeatEventType = "heartbeat"
  11. )
  12. // Event represents an action that occurred within the Raft library.
  13. // Listeners can subscribe to event types by using the Server.AddEventListener() function.
  14. type Event interface {
  15. Type() string
  16. Source() interface{}
  17. Value() interface{}
  18. PrevValue() interface{}
  19. }
  20. // event is the concrete implementation of the Event interface.
  21. type event struct {
  22. typ string
  23. source interface{}
  24. value interface{}
  25. prevValue interface{}
  26. }
  27. // newEvent creates a new event.
  28. func newEvent(typ string, value interface{}, prevValue interface{}) *event {
  29. return &event{
  30. typ: typ,
  31. value: value,
  32. prevValue: prevValue,
  33. }
  34. }
  35. // Type returns the type of event that occurred.
  36. func (e *event) Type() string {
  37. return e.typ
  38. }
  39. // Source returns the object that dispatched the event.
  40. func (e *event) Source() interface{} {
  41. return e.source
  42. }
  43. // Value returns the current value associated with the event, if applicable.
  44. func (e *event) Value() interface{} {
  45. return e.value
  46. }
  47. // PrevValue returns the previous value associated with the event, if applicable.
  48. func (e *event) PrevValue() interface{} {
  49. return e.prevValue
  50. }