![Test Coverage](https://camo.githubusercontent.com/d0a8ddfbe847624239ddfbe4f0da34677c5298209bd513fd343a6cdee31e0061/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f70617462656e617461722f70726f6d6973652d73746174652d6d616368696e652f6261646765732f636f7665726167652e737667)
var fsm = new PromiseStateMachine({
initial: 'green',
events: {
warn: { from: 'green', to: 'yellow' },
panic: { from: ['green', 'yellow'], to: 'red' },
calm: { from: 'red', to: 'yellow' },
clear: { from: ['yellow', 'red'], to: 'green' }
}
});
fsm.on('warn', function(event, from, to, anyArgs) {
return Promise.resolve('result 1');
});
fsm.on('warn', function(event, from, to, anyArgs) {
var transaction = anyArgs[0];
var somethingElse = anyArgs[1];
return Promise.resolve('result 2');
});
fsm.warn(transaction, somethingElse).then(function(results) {
// results: ['result 1', 'result 2']
}).error(function(error) {
// could receive a StateTransitionError if trying to transition via an
// inaccessible event.
});
fsm.is('green'); // => false
fsm.is('yellow'); // => true
fsm.state(); // => 'yellow'
fsm.can('calm'); // => false
fsm.can('panic'); // => true
Extending an existing object
var MyObject = function() {
PromiseStateMachine.call(this, {
initial: 'green',
events: {
warn: { from: 'green', to: 'yellow' },
panic: { from: 'yellow', to: 'red' },
calm: { from: 'red', to: 'yellow' },
clear: { from: 'yellow', to: 'green' }
}
});
};
_.extend(MyObject, PromiseStateMachine);
_.extend(MyObject.prototype, PromiseStateMachine.prototype);
var fsm = new MyObject();
fsm.is('green'); // => true
fsm.warn().then(function() {
fsm.is('green'); // => false
});