-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateMachine.brs
201 lines (160 loc) · 5.91 KB
/
StateMachine.brs
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
Function StateMachine()
this = {}
this.version = "0.2" ' wow, so stable, much features
this.create = Function(config)
this = m ' StateMachine() scope
fsm = {} ' StateMachine instance
config.events["startup"] = {"none": config.initial}
' private properties
fsm.p_config = config
' public properties
fsm.current = config.initial
fsm.is = Function(state)
If state = m.current
return true
Else
return false
End If
End Function
fsm.can = Function(event)
this = m
If this.p_config.events[event][this.current] <> invalid or this.p_config.events[event]["*"] = "*"
return true
End If
return false
End Function
fsm.cannot = Function(event)
If m.can(event) = true
return false
Else
return true
End If
End Function
For Each callback In config.callbacks
If Type(config.callbacks[callback]) = "roFunction"
fsm[callback] = config.callbacks[callback]
End If
End For
fsm.call = this.p_buildEvent()
fsm.call("startup")
return fsm
End Function
' callbacks
' func = this['onbefore' + name]
' name - event name
' from - current state
' into - next state
' ===
' m - StateMachine scope
' this - instance scope
this.leaveState = Function(this, name, from, into, arguments)
func = this["onleave" + from]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End Function
this.enterState = Function(this, name, from, into, arguments)
func = this["onenter" + into]
If Type(func) = "roFunction"
func(name, from, into, arguments)
Else
func = this["on" + into]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End If
End Function
this.changeState = Function(this, name, from, into, arguments)
func = this["onchangestate"]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End Function
this.beforeThisEvent = Function(this, name, from, into, arguments)
func = this["onbefore" + name]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End Function
this.beforeAnyEvent = Function(this, name, from, into, arguments)
func = this["onbeforeevent"]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End Function
this.afterThisEvent = Function(this, name, from, into, arguments)
func = this["onafter" + name]
If Type(func) = "roFunction"
func(name, from, into, arguments)
Else
func = this["on" + name]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End If
End Function
this.afterAnyEvent = Function(this, name, from, into, arguments)
func = this["onafterevent"]
If Type(func) = "roFunction"
func(name, from, into, arguments)
Else
func = this["onevent"]
If Type(func) = "roFunction"
func(name, from, into, arguments)
End If
End If
End Function
this.beforeEvent = Function(this, name, from, into, arguments)
If StateMachine().beforeThisEvent(this, name, from, into, arguments) = false OR StateMachine().beforeAnyEvent(this, name, from, into, arguments) = false
return false
End If
End Function
this.afterEvent = Function(this, name, from, into, arguments)
StateMachine().afterAnyEvent(this, name, from, into, arguments)
StateMachine().afterThisEvent(this, name, from, into, arguments)
End Function
this.p_buildEvent = Function()
' there is no closures in BrightScript, boo-hoo
return Function(name, arguments = {})
this = m
If this.cannot(name) = true
print "event " + name + " innapropriate in current state " + this.current
return 0
End If
from = this.current
If NOT isEmptyAA(this.p_config.events[name])
If this.p_config.events[name][from] <> invalid
into = this.p_config.events[name][from]
Else
into = this.current ' do not change state
End If
Else
into = this.current ' do not change state
End If
If false = StateMachine().beforeEvent(this, name, from, into, arguments)
print "something wen't wrong before calling event"
return 0
End If
If from = into
' do not change state
StateMachine().afterEvent(this, name, from, into, arguments)
return 0
End If
' do transition, yo
this.transition = Function(name, from, into, arguments)
this = m
this.transition = invalid ' transition can be called only once
this.current = into
StateMachine().enterState(this, name, from, into, arguments)
StateMachine().changeState(this, name, from, into, arguments)
StateMachine().afterEvent(this, name, from, into, arguments)
End Function
If false <> StateMachine().leaveState(this, name, from, into, arguments)
If this.transition <> invalid
this.transition(name, from, into, arguments)
End If
End If
End Function
End Function
return this
End Function