'use strict'; const machina = require("machina"); let parent = new machina.Fsm({ initialState: "one", memory: { done: false }, states: { one: { _onEnter: function () { console.log(`(${this.namespace}) Parent state 1 reached`); return this.transition("two"); } }, two: { _onEnter: function () { console.log(`(${this.namespace}) Parent state 2 reached`); return this.handle("childAction"); }, _child: { factory: function () { console.log("Creating child for state 2"); return createChild(); } }, done: function () { if (!this.memory.done) { return this.transition("three"); } } }, three: { _onEnter: function () { console.log(`(${this.namespace}) Parent state 3 reached`); return this.handle("childAction"); }, _child: { factory: function () { console.log("Creating child for state 3"); return createChild(); } }, done: function () { this.memory.done = true; console.log("Parent done! Returning to state 2 one last time..."); return this.transition("two"); } } } }); function createChild() { return new machina.Fsm({ initialState: "a", states: { a: { _onEnter: function () { console.log(`(${this.namespace}) Child state A reached`); }, childAction: function () { return this.transition("b"); } }, b: { _onEnter: function () { console.log(`(${this.namespace}) Child state B reached`); return this.handle("done"); } } } }) }