UNPKG

@wiz-code/async-fsm

Version:

Finite StateMachine JavaScript Library

94 lines (79 loc) 2.28 kB
var _ = require('underscore'); var FSM = require('../src'); FSM.logger.disable(); FSM.globalize(); var machine = new Machine('my-machine', { data: { message: 'Hello, Machine', score: { stage1: 50000, } }, props: { id: 'wizard-code', like: { game: 'wizardry' } }, methods: { handler: function () { return 'handled!'; } } }); var state1 = new State('state1', { data: { //message: 'Hello, State1' }, entryAction: function () { machine.region.set({message: 'Hello, Region!'}); console.log('entryAction::', this.getName()); console.log('$get::', this.$get('message')); }, doActivity: function () { console.log('doActivity::', this.getName()); }, }); var state1Sub1 = new State('state1-sub1', { data: { //message: 'Hello, State1Sub1' }, entryAction: function () { console.log('entryAction::', this.getName()); //console.log('$set::', this.$set('message', 'Hello, State1Sub1')); console.log('$get::', this.$get('message')); }, doActivity: function () { console.log('doActivity::', this.getName()); }, }); var state1Sub1Sub1 = new State('state1-sub1-sub1', { data: { //message: 'Hello, State1Sub1Sub1' }, entryAction: function () { console.log('entryAction::', this.getName()); console.log('$get::', this.$get('message')); console.log('$props::', this.$getProp('like/game')); console.log('$methods::', this.$getMethod('handler')()); }, doActivity: function () { console.log('doActivity::', this.getName()); if (this.getTicks() >= 10) { this.completion(); } }, loop: true, useRAF: true, }); machine.addState(state1); state1.addState(state1Sub1); state1Sub1.addState(state1Sub1Sub1); var toState1 = new Transition(false, false, state1); var toState1Sub1 = new Transition(false, false, state1Sub1); var toState1Sub1Sub1 = new Transition(false, false, state1Sub1Sub1); machine.addTransition(toState1); state1.addTransition(toState1Sub1); state1Sub1.addTransition(toState1Sub1Sub1); machine.deploy(); machine.start();