简体   繁体   中英

how do I extend a state's functionality in Machina.js?

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:

var _ = require('lodash');
var machina = require('machina')(_);

var myProtoProps = {
    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 myChildProps = _.merge(_.cloneDeep(myProtoProps), {
    id: "fsmExample",
    states: {
        done: {
            _onEnter: _.bind(console.log, console, "completed")
        }
    }
});
var fsmExample = new machina.Fsm(myChildProps);

fsmExample.handle("connect");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM