forked from jwreagor/transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition.go
220 lines (187 loc) · 5.31 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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[T Stater](_ T) *StateMachine[T] {
return &StateMachine[T]{
states: map[string]*State[T]{},
events: map[string]*Event[T]{},
}
}
// StateMachine a struct that hold states, events definitions
type StateMachine[T Stater] struct {
initialState string
states map[string]*State[T]
events map[string]*Event[T]
}
// Initial define the initial state
func (sm *StateMachine[T]) Initial(name string) *StateMachine[T] {
sm.initialState = name
return sm
}
// State define a state
func (sm *StateMachine[T]) State(name string) *State[T] {
if _, ok := sm.states[name]; ok {
return sm.states[name]
}
state := &State[T]{Name: name}
sm.states[name] = state
return state
}
// Event define an event
func (sm *StateMachine[T]) Event(name string) *Event[T] {
if _, ok := sm.events[name]; ok {
return sm.events[name]
}
event := &Event[T]{Name: name}
sm.events[name] = event
return event
}
// Trigger trigger an event
func (sm *StateMachine[T]) Trigger(name string, value T) error {
stateWas := value.GetState()
if stateWas == "" {
stateWas = sm.initialState
value.SetState(sm.initialState)
}
if event := sm.events[name]; event != nil {
var matchedTransitions []*EventTransition[T]
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[T Stater] struct {
Name string
enters []func(value T) error
exits []func(value T) error
}
// Enter register an enter hook for State
func (state *State[T]) Enter(fc func(value T) error) *State[T] {
state.enters = append(state.enters, fc)
return state
}
// Exit register an exit hook for State
func (state *State[T]) Exit(fc func(value T) error) *State[T] {
state.exits = append(state.exits, fc)
return state
}
// Event contains Event information, including transition hooks
type Event[T Stater] struct {
Name string
transitions map[string]*EventTransition[T]
}
// To define EventTransition of go to a state
func (event *Event[T]) To(name string) *EventTransition[T] {
if event.transitions == nil {
event.transitions = map[string]*EventTransition[T]{}
}
if _, ok := event.transitions[name]; ok {
return event.transitions[name]
}
transition := &EventTransition[T]{to: name}
event.transitions[name] = transition
return transition
}
// EventTransition hold event's to/froms states, also including befores, afters hooks
type EventTransition[T Stater] struct {
to string
froms []string
befores []func(value T) error
afters []func(value T) error
}
// From used to define from states
func (transition *EventTransition[T]) From(states ...string) *EventTransition[T] {
transition.froms = append(transition.froms, states...)
transition.froms = removeDuplicateValues(transition.froms)
return transition
}
// Before register before hooks
func (transition *EventTransition[T]) Before(fc func(value T) error) *EventTransition[T] {
transition.befores = append(transition.befores, fc)
return transition
}
// After register after hooks
func (transition *EventTransition[T]) After(fc func(value T) error) *EventTransition[T] {
transition.afters = append(transition.afters, fc)
return transition
}
func removeDuplicateValues[T comparable](slice []T) []T {
keys := make(map[T]bool)
list := []T{}
// If the key(values of the slice) is not equal
// to the already present value in new slice (list)
// then we append it. else we jump on another element.
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}