Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/patbenatar/promise-state-machine
An event-driven finite state machine backed by Promises
https://github.com/patbenatar/promise-state-machine
Last synced: 2 months ago
JSON representation
An event-driven finite state machine backed by Promises
- Host: GitHub
- URL: https://github.com/patbenatar/promise-state-machine
- Owner: patbenatar
- License: mit
- Created: 2015-03-22T20:35:07.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-09-11T19:41:20.000Z (over 7 years ago)
- Last Synced: 2024-09-22T15:10:51.105Z (4 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 8
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Promise State Machine
[![npm version](https://badge.fury.io/js/promise-state-machine.svg)](http://badge.fury.io/js/promise-state-machine)
[![Code Climate](https://codeclimate.com/github/patbenatar/promise-state-machine/badges/gpa.svg)](https://codeclimate.com/github/patbenatar/promise-state-machine)
[![Test Coverage](https://codeclimate.com/github/patbenatar/promise-state-machine/badges/coverage.svg)](https://codeclimate.com/github/patbenatar/promise-state-machine)```javascript
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
```javascript
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
});
```