【发布时间】: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