realtion.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package fsm
  2. // RelationState is relation state type
  3. type RelationState string
  4. // RelationEvent is relation event type
  5. type RelationEvent string
  6. // consts for relation
  7. var (
  8. // states
  9. StateNoRelation = RelationState("no_relation")
  10. StateWhisper = RelationState("whisper")
  11. StateFollowing = RelationState("following")
  12. StateBlacked = RelationState("blacked")
  13. StateFriend = RelationState("friend") // StateFriend is the most special state
  14. // events
  15. EventAddFollowing = RelationEvent("add_following")
  16. EventDelFollowing = RelationEvent("del_following")
  17. EventAddWhisper = RelationEvent("add_whisper")
  18. EventDelWhisper = RelationEvent("del_whisper")
  19. EventAddBlack = RelationEvent("add_black")
  20. EventDelBlack = RelationEvent("del_black")
  21. EventDelFollower = RelationEvent("del_follower")
  22. EventBeFriend = RelationEvent("be_friend") // EventBeFriend is the most special event
  23. )
  24. // RelationEventHandler is used to handle all state change for relation
  25. type RelationEventHandler interface {
  26. AddFollowing(*Event)
  27. DelFollowing(*Event)
  28. AddWhisper(*Event)
  29. DelWhisper(*Event)
  30. AddBlack(*Event)
  31. DelBlack(*Event)
  32. DelFollower(*Event)
  33. }
  34. // DefaultHandler is the default RelationEventHandler
  35. var DefaultHandler = &defaultHandlerImpl{}
  36. type defaultHandlerImpl struct{}
  37. func (*defaultHandlerImpl) AddFollowing(*Event) {}
  38. func (*defaultHandlerImpl) DelFollowing(*Event) {}
  39. func (*defaultHandlerImpl) AddWhisper(*Event) {}
  40. func (*defaultHandlerImpl) DelWhisper(*Event) {}
  41. func (*defaultHandlerImpl) AddBlack(*Event) {}
  42. func (*defaultHandlerImpl) DelBlack(*Event) {}
  43. func (*defaultHandlerImpl) DelFollower(*Event) {}
  44. // RelationStateMachine is used to describe all state change for relation
  45. type RelationStateMachine struct {
  46. *FSM
  47. }
  48. // NewRelationStateMachine will create a RelationStateMachine
  49. func NewRelationStateMachine(initial RelationState, handler RelationEventHandler) *RelationStateMachine {
  50. rs := &RelationStateMachine{
  51. FSM: NewFSM(
  52. string(StateNoRelation),
  53. Events{
  54. {
  55. Name: string(EventAddFollowing),
  56. Src: []string{
  57. string(StateNoRelation),
  58. string(StateWhisper),
  59. string(StateBlacked),
  60. },
  61. Dst: string(StateFollowing),
  62. },
  63. {
  64. Name: string(EventDelFollowing),
  65. Src: []string{
  66. string(StateFollowing),
  67. string(StateFriend),
  68. },
  69. Dst: string(StateNoRelation),
  70. },
  71. {
  72. Name: string(EventAddWhisper),
  73. Src: []string{
  74. string(StateNoRelation),
  75. string(StateFollowing),
  76. string(StateBlacked),
  77. string(StateFriend),
  78. },
  79. Dst: string(StateWhisper),
  80. },
  81. {
  82. Name: string(EventDelWhisper),
  83. Src: []string{
  84. string(StateWhisper),
  85. },
  86. Dst: string(StateNoRelation),
  87. },
  88. {
  89. Name: string(EventAddBlack),
  90. Src: []string{
  91. string(StateNoRelation),
  92. string(StateFollowing),
  93. string(StateFriend),
  94. string(StateWhisper),
  95. },
  96. Dst: string(StateBlacked),
  97. },
  98. {
  99. Name: string(EventDelBlack),
  100. Src: []string{
  101. string(StateBlacked),
  102. },
  103. Dst: string(StateNoRelation),
  104. },
  105. {
  106. Name: string(EventDelBlack),
  107. Src: []string{
  108. string(StateBlacked),
  109. },
  110. Dst: string(StateNoRelation),
  111. },
  112. },
  113. Callbacks{
  114. string(EventAddFollowing): handler.AddFollowing,
  115. string(EventDelFollowing): handler.DelFollowing,
  116. string(EventAddWhisper): handler.AddWhisper,
  117. string(EventDelWhisper): handler.DelWhisper,
  118. string(EventAddBlack): handler.AddBlack,
  119. string(EventDelBlack): handler.DelBlack,
  120. string(EventDelFollower): handler.DelFollower,
  121. },
  122. ),
  123. }
  124. return rs
  125. }
  126. // Event is used to execute any events
  127. func (r *RelationStateMachine) Event(event RelationEvent, args ...interface{}) error {
  128. return r.FSM.Event(string(event), args...)
  129. }
  130. // SetState is used to set state
  131. func (r *RelationStateMachine) SetState(state RelationState) {
  132. r.FSM.SetState(string(state))
  133. }