On Machina.js (version 0.3.6), how do I create an instance of an extended FSM constructor, where both child and parent FSMs define behaviors in the same states?
Here is my code:
var _ = require('lodash');
var machina = require('machina')(_);
var MyFsm = machina.Fsm.extend({
eventListeners: {
NoHandler: _.bind(console.log, console, "NoHandler"),
invalidstate:_.bind(console.log, console, "invalidstate")
},
initialState: "start",
states: {
start: {
_onEnter: _.bind(console.log, console, "started"),
connect: function () {
console.log(this.id + " is connecting");
this.transition("done")
}
},
done: {}
}
});
var fsmExample = new MyFsm({
id: "fsmExample",
states: {
done: {
_onEnter: _.bind(console.log, console, "completed")
}
}
});
fsmExample.handle("connect");
And I get this error:
...\node_modules\machina\lib\machina.js:149
if (states[current][inputType] || states[current]["*"] || this
^
TypeError: Cannot read property 'connect' of undefined
at _.extend.handle (...\node_modules\machina\lib\machina.js:149:36)
at Object.<anonymous> (...\server.js:81:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
What am I doing wrong?
I've found a description of this problem in a recent PR comment: It seems that this area has known issues that should be better addressed in 0.3.7. Until then, I've made a work-around where I manage the inheritance in my code, and use the default constructor without extending it: