【问题标题】:Meteor State Machine流星状态机
【发布时间】:2016-10-04 01:30:36
【问题描述】:

我已经实现了 Nate Strauser 的状态机 (https://github.com/nate-strauser/meteor-statemachine)。我让 FSM 成功将其状态保存到数据库,但我正在跟踪几个实例。在我的示例中,我正在跟踪工人的轮班状态。

我希望系统在流星启动时加载每个状态。然后,我想对状态机实例发出状态更改请求,并让它更新数据库文档的状态(如果允许更改)。

如何将 FSM 实例与实际的 Shift 实例结合?我是以错误的方式接近这个吗?任何想法表示赞赏。

Meteor.startup(function () {

var machineEvents = [
    { name: 'toggleduty',  from: 'Off_Duty',      to: 'On_Duty_Idle' },
    { name: 'toggleduty',  from: 'On_Duty_Idle',  to: 'Off_Duty' },
    { name: 'toggleduty',  from: 'On_Duty_Busy',  to: 'Off_Duty_Busy' },
    { name: 'toggleduty',  from: 'Off_Duty_Busy', to: 'On_Duty_Busy' },
    { name: 'togglebusy',  from: 'On_Duty_Idle',  to: 'On_Duty_Busy' },
    { name: 'togglebusy',  from: 'On_Duty_Busy',  to: 'On_Duty_Idle' },
    { name: 'togglebusy',  from: 'Off_Duty_Busy', to: 'Off_Duty' },
    { name: 'start',       from: 'Init',          to: 'On_Duty_Idle' },];

var machineCallbacks = {
    ontoggleduty: function(event, from, to, shift) {
        console.log('Toggling Duty', shift);
        Shifts.update(shift._id, {$set: { 'status':to }});
    },
    ontogglebusy: function(event, from, to, shift) {
        console.log('Toggling Busy', shift);
        Shifts.update(shift._id, {$set: { 'status':to }});
    },
};


var makeStateMachine = function(shift){
    console.log('new state machine generating');
    var stateMachine = StateMachine.create({
        initial: shift.status,
        events: machineEvents,
        callbacks: machineCallbacks
    });
    switch (shift.state) {
        case "Init":
            console.log('Init to On_Duty_Idle',shift);
            stateMachine.start(shift);
            break;
    }
};

// function killStateMachine(shift){  // not sure how to kill the specific reference
//     stateMachine = null;
// }

//look for new state machines
Shifts.find({'status': 'Init'}).observe({
    added: makeStateMachine,
    //removed: killStateMachine
});

// In the mongo shell I trigger with db.statemachines.insert({_id:'driver1', state:'start'})

});

【问题讨论】:

    标签: javascript meteor state-machine


    【解决方案1】:

    原始状态机的一个分支发生了变化,它添加了一个 transitionTo 方法,允许选择任何状态 - 我认为这是您从数据库恢复状态时想要的。我有一个叉子,它整合了其他一些人的变化

    https://github.com/mikkelking/javascript-state-machine

    您可以在项目中本地克隆 natestrauser 的包以引用我的 repo(尽管我还没有发布它)。一旦我的更改稳定(看起来稳定),我打算在某个时候发出拉取请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多