forked from qor/transition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transition.go
190 lines (160 loc) · 4.51 KB
/
transition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package transition
import (
"fmt"
)
// Transition is a struct, embed it in your struct to enable state machine for the struct
type Transition struct {
State string
}
// SetState set state to Stater, just set, won't save it into database
func (transition *Transition) SetState(name string) {
transition.State = name
}
// GetState get current state from
func (transition Transition) GetState() string {
return transition.State
}
// Stater is a interface including methods `GetState`, `SetState`
type Stater interface {
SetState(name string)
GetState() string
}
// New initialize a new StateMachine that hold states, events definitions
func New(value interface{}) *StateMachine {
return &StateMachine{
states: map[string]*State{},
events: map[string]*Event{},
}
}
// StateMachine a struct that hold states, events definitions
type StateMachine struct {
initialState string
states map[string]*State
events map[string]*Event
}
// Initial define the initial state
func (sm *StateMachine) Initial(name string) *StateMachine {
sm.initialState = name
return sm
}
// State define a state
func (sm *StateMachine) State(name string) *State {
state := &State{Name: name}
sm.states[name] = state
return state
}
// Event define an event
func (sm *StateMachine) Event(name string) *Event {
event := &Event{Name: name}
sm.events[name] = event
return event
}
// Trigger trigger an event
func (sm *StateMachine) Trigger(name string, value Stater) error {
stateWas := value.GetState()
if stateWas == "" {
stateWas = sm.initialState
value.SetState(sm.initialState)
}
if event := sm.events[name]; event != nil {
var matchedTransitions []*EventTransition
for _, transition := range event.transitions {
var validFrom = len(transition.froms) == 0
if len(transition.froms) > 0 {
for _, from := range transition.froms {
if from == stateWas {
validFrom = true
}
}
}
if validFrom {
matchedTransitions = append(matchedTransitions, transition)
}
}
if len(matchedTransitions) == 1 {
transition := matchedTransitions[0]
// State: exit
if state, ok := sm.states[stateWas]; ok {
for _, exit := range state.exits {
if err := exit(value); err != nil {
return err
}
}
}
// Transition: before
for _, before := range transition.befores {
if err := before(value); err != nil {
return err
}
}
value.SetState(transition.to)
// State: enter
if state, ok := sm.states[transition.to]; ok {
for _, enter := range state.enters {
if err := enter(value); err != nil {
value.SetState(stateWas)
return err
}
}
}
// Transition: after
for _, after := range transition.afters {
if err := after(value); err != nil {
value.SetState(stateWas)
return err
}
}
return nil
}
}
return fmt.Errorf("failed to perform event %s from state %s", name, stateWas)
}
// State contains State information, including enter, exit hooks
type State struct {
Name string
enters []func(value interface{}) error
exits []func(value interface{}) error
}
// Enter register an enter hook for State
func (state *State) Enter(fc func(value interface{}) error) *State {
state.enters = append(state.enters, fc)
return state
}
// Exit register an exit hook for State
func (state *State) Exit(fc func(value interface{}) error) *State {
state.exits = append(state.exits, fc)
return state
}
// Event contains Event information, including transition hooks
type Event struct {
Name string
transitions []*EventTransition
}
// To define EventTransition of go to a state
func (event *Event) To(name string) *EventTransition {
transition := &EventTransition{to: name}
event.transitions = append(event.transitions, transition)
return transition
}
// EventTransition hold event's to/froms states, also including befores, afters hooks
type EventTransition struct {
to string
froms []string
befores []func(value interface{}) error
afters []func(value interface{}) error
}
// From used to define from states
func (transition *EventTransition) From(states ...string) *EventTransition {
transition.froms = states
return transition
}
// Before register before hooks
func (transition *EventTransition) Before(fc func(value interface{}) error) *EventTransition {
transition.befores = append(transition.befores, fc)
return transition
}
// After register after hooks
func (transition *EventTransition) After(fc func(value interface{}) error) *EventTransition {
transition.afters = append(transition.afters, fc)
return transition
}