-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_state_machine.toit
40 lines (27 loc) · 980 Bytes
/
basic_state_machine.toit
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
// basic_state_machine.toit
abstract class BasicStateMachine :
states_/Map := {:}
currentState_/int := ?
constructor .currentState_/int :
create
abstract create -> none
abstract publish state/int -> none
abstract getStateName state/int -> string
abstract getEventName event/int -> string
setState state/int -> none :
currentState_ = state
getState -> int :
return currentState_
dispatch event/int -> none :
currentState := states_[currentState_]
if (currentState == null or currentState.size == 0) :
print("? Failed to get container for $(getStateName currentState_)")
return
transition := currentState.transition event
if (transition == null) :
print("? Failed to get transition for event [$(getEventName event)] and state [$(getStateName currentState_)]")
return
setState transition.state
method := transition.method
method.execute
publish currentState_