-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSM.coffee
46 lines (41 loc) · 1.12 KB
/
FSM.coffee
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
'use strict'
{Actor} = require('@coffee-toolbox/actor')
class FSM extends Actor
constructor: (config)->
super config
@state = 'INIT'
@stateHandler =
TERMINATED: (reason)=>
@$terminate(reason)
if reason?
@logger.info 'terminated', reason
else
@logger.info 'terminated'
$start: (reg)->
reg ?= {}
reg.call = Object.assign {}, reg.call,
$set_new_state: (new_state)=>
@state = new_state.state
@$send_to this, '$run_state_handler', new_state
reg.receive = Object.assign {}, reg.receive,
$run_state_handler: (from, new_state)=>
@logger.assert from is this
f = @stateHandler[new_state.state]
@logger.assert f?,
"state handler for #{new_state.state} not exist"
@logger.assert f instanceof Function,
"state handler for #{new_state.state} is not a Function"
f new_state.args...
super reg
nextState: (state, v...)->
@$call '$set_new_state',
state: state
args: v
@$next()
# reg :: { State: Function }
# State :: string
# setup StateHandlers.
setStateHandlers: (reg)->
@stateHandler = Object.assign {}, @stateHandler, reg
module.exports =
FSM: FSM