流星状态机

Meteor State Machine

本文关键字:状态机 流星      更新时间:2023-09-26

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

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

如何将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'})
});

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

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

你可以在你的项目中本地克隆natestrauser的包来引用我的repo(尽管我还没有发布它)。我打算在某个时候做一个拉请求一旦我的变化是稳定的(他们似乎是)。