diff --git a/.gitignore b/.gitignore index 3c3629e..66fd66c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +npm-debug.log* diff --git a/index.js b/index.js index c922370..e4c6ad6 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,9 @@ const machina = require("machina"); let parent = new machina.Fsm({ initialState: "one", + memory: { + done: false + }, states: { one: { _onEnter: function () { @@ -19,9 +22,32 @@ let parent = new machina.Fsm({ }, _child: { factory: function () { - console.log("Creating child"); + 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"); } } } @@ -42,6 +68,7 @@ function createChild() { b: { _onEnter: function () { console.log(`(${this.namespace}) Child state B reached`); + return this.handle("done"); } } }