UNPKG

@wiz-code/async-fsm

Version:

Finite StateMachine JavaScript Library

80 lines (63 loc) 2.62 kB
var _ = require('underscore'); var FSM = require('../src'); var subMachine = new FSM.Machine('sub-machine'); var subState1 = new FSM.State('sub-state1', { entryAction: function () { console.log('entryAction::', this.getName()); }, doActivity: function () { console.log('doActivity::', this.getName()); subState1ToSubState2.trigger(); }, }); var subState2 = new FSM.State('sub-state2', { entryAction: function () { console.log('entryAction::', this.getName()); }, doActivity: function () { console.log('doActivity::', this.getName()); subState2ToExitPoint.trigger(); } }); var subEntryPoint = new FSM.EntryPointPseudoState(false); var subExitPoint = new FSM.ExitPointPseudoState(false); var toSubState1 = new FSM.Transition(false, false, subState1); var subState1ToSubState2 = new FSM.Transition(false, subState1, subState2); var entryPointToSubState1 = new FSM.Transition(false, subEntryPoint, subState1); var subState2ToExitPoint = new FSM.Transition(false, subState2, subExitPoint); subMachine.addState(subState1, subState2, subEntryPoint, subExitPoint); subMachine.addTransition(toSubState1, subState1ToSubState2, entryPointToSubState1, subState2ToExitPoint); subMachine.deploy(); //// var superMachine = new FSM.Machine('super-machine'); var superState1 = new FSM.State('super-state1', { entryAction: function () { console.log('entryAction::', this.getName()); }, doActivity: function () { console.log('doActivity::', this.getName()); superState1ToEntryPoint.trigger(); //superState1ToSuperState2.trigger(); }, }); var superState2 = new FSM.SubMachine('super-state2', { // }); var superState3 = new FSM.State('super-state3', { // }); var entryPoint = new FSM.EntryPointPseudoState(false); var exitPoint = new FSM.ExitPointPseudoState(false); entryPoint.setKey(subEntryPoint.getId()); exitPoint.setKey(subExitPoint.getId()); superState2.addState(entryPoint, exitPoint); superState2.link(subMachine); superState2.deploy(); var toSuperState1 = new FSM.Transition(false, false, superState1); var superState1ToEntryPoint = new FSM.Transition(false, superState1, entryPoint); //var superState1ToSuperState2 = new Transition(false, superState1, superState2); var exitPointToSuperState3 = new FSM.Transition(false, exitPoint, superState3); superMachine.addState(superState1, superState2, superState3); superMachine.addTransition(toSuperState1, superState1ToEntryPoint/*superState1ToSuperState2*/, exitPointToSuperState3); superMachine.deploy(); superMachine.start();