event.go 1.3 KB

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