0
我已经实现了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'})
});