Skip to content

Instantly share code, notes, and snippets.

@nate-strauser
Created March 24, 2014 19:30
Show Gist options
  • Save nate-strauser/9747334 to your computer and use it in GitHub Desktop.
Save nate-strauser/9747334 to your computer and use it in GitHub Desktop.
var events = [
{ name: 'submit', from: 'pending', to: 'processing' },
{ name: 'update', from: 'processing', to: 'processing' },
{ name: 'finish', from: 'processing', to: 'finished' },
{ name: 'fail', from: '*', to: 'failed' }
];
var callbacks = {
onsubmit: function(event, from, to, obj) {
log.info("submit: " + obj.title);
CollectionA.update(obj._id, {$set: {
...
'state': to
}});
},
onfinish: function(event, from, to, obj) {
log.info("finish: " + obj.title);
CollectionA.update(obj._id, {$set: {
...
'state': to
}});
//can start another state machine too
CollectionB.update({objId:obj._id},{$set:{
'state':'pending'
}});
}
},
onfail: function(event, from, to, obj, error) {
log.trace(error, "fail: " + obj.title + " - " + error);
Collection.update(video._id, {$set: {
'error': error,
'state': to
}});
}
};
//init the state machine
var obj = CollecitonA.findOne();
var stateMachine = StateMachine.create({
initial: obj.state,
events: events,
callbacks: callbacks
});
//using
if(err)
stateMachine.fail(obj, err);
else
stateMachine.submit(obj);
stateMachine.finish(obj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment