私はネイト・ストレイザーの状態機械(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'})
});