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
|
function andthen(fsm, event)
{
var closure = function() {
fsm.handle(event, this.json);
return false;
}
return closure
}
function transthen(fsm, state, event) {
var closure = function() {
fsm.trans(state);
fsm.handle(event, this.json);
return false;
}
return closure
}
var FSM = function(states)
{
this.states = states;
this.state = this.states.start;
this.state_name = 'start';
this.intervalId = null;
}
FSM.prototype.handle = function(event, data)
{
var res = null;
var target = this.state[event];
if(target) {
if(this.onevent) this.onevent(this, event);
try {
res = target(this, data);
} catch(e) {
if(this.onexception) {
this.onexception(this, e);
} else {
throw e;
}
}
} else if(this.onunknownevent) {
// the target does not exist and they have registered an event for that
this.onunknownevent(this, event);
}
return res
}
FSM.prototype.trans = function(target)
{
var new_state = this.states[target]
if(new_state) {
if(this.ontrans) this.ontrans(this, target)
this.state_name = target
this.state = new_state
} else if(this.onunknownstate) {
this.onunknownstate(fsm, target)
}
}
FSM.prototype.start = function(event) {
this.states.start(this, event);
}
|