Created
March 24, 2014 19:30
-
-
Save nate-strauser/9747334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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