commit e54f5c687b283594296f412ebf2f1e4f9c9d6733 Author: Sven Slootweg Date: Sun May 6 16:37:28 2018 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc9a0ff --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# machina-factory-bugcase + +A demonstration of a bug in machina v2.0.2, where a child FSM factory is called multiple times when specified as part of a config object. + +What should happen: + +``` +(fsm.0) Parent state 1 reached +Creating child +(fsm.1) Child state A reached +(fsm.0) Parent state 2 reached +Creating child +(fsm.2) Child state A reached +(fsm.2) Child state B reached +``` + +What happens instead: + +``` +(fsm.0) Parent state 1 reached +Creating child +(fsm.1) Child state A reached +Creating child +(fsm.2) Child state A reached +(fsm.0) Parent state 2 reached +Creating child +(fsm.3) Child state A reached +Creating child +(fsm.4) Child state A reached +(fsm.4) Child state B reached +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..c922370 --- /dev/null +++ b/index.js @@ -0,0 +1,49 @@ +'use strict'; + +const machina = require("machina"); + +let parent = new machina.Fsm({ + initialState: "one", + 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"); + return createChild(); + } + } + } + } +}); + +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`); + } + } + } + }) +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0d70221 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "machina-factory-bugcase", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@git.cryto.net:joepie91/machina-factory-bugcase.git" + }, + "keywords": ["machina"], + "author": "Sven Slootweg ", + "license": "WTFPL", + "dependencies": { + "machina": "^2.0.2" + } +}