Promise State Machine ES6
This is a rewritten version of promise-state-machine but with no dependencies; not bluebird - just native ES6, DOT data generation support for use with Graphvis or vis.js, to generate a visual representation of your state machine.
Installation
npm install promise-state-machine-es6 --save
Class PromiseStateMachine
Constructor(options)
state
- May be any primitive type.
options.initial
- Initial state
.
options.events
- An Object where key denotes a transition.
options.events[transition].from
- From state
, may be an array of state
.
options.events[transition].to
- To state
Each key defined in options.events
is defined as a method on the current instance. Use these methods to request a state transition. These methods returns a Promise that Resolves when transition succeded and Rejects when failed. To intercept a transition listen on the transition name, eg if options.events
has "warn" transition we can intercept it by fsm.on('warn', (from, to, ...rest) => Promise.resolve())
. This event would get called when fsm.warn(...rest).then()
is called.
is(...states) : Bool
Check if instance is in any state. Argument may be one or many states or an array of states.
can(...events) : Bool
Check if instance can transition to a specific state. Argument may be one or many events or an array of events.
state [getter] : state
Get current state
toDOTsync(options) : String
Synchronously create DOT file data as a string.
options.replacer
- Optional synchronous replacer function that takes a data
object and returns a data
object with all values stringified.
data.from
- From state
data.to
- To state
data.transition
- Transition key
# Events
[eventsKey]
(from, to, ...arguments)
A transition event for each key of options.events
passed to the constructor may be emitted when calling any method as defined by options.events
.
transition (transitionKey, from, to, ...arguments)
This event is emitted before [transitionKey] event is emitted, returning a Promise that rejects will abort the transition and [eventsKey] event will not be emitted.
Examples
Complete Example
const fsm = 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; fsm; fsm; fsm; fsm; // => falsefsm; // => truefsmstate; // => 'yellow'fsm; // => falsefsm; // => true
toDOTsync
const dotDataString = events: approve: from: 'pending' to: 'approved' reject: from: 'pending' 'approved' to: 'rejected' pend: from: 'approved' 'rejected' to: 'pending' ;
toDOTsync
const dotDataString = events: doWork: from: 'Working' 'Starting' to: 'Working' handleError: from: 'Working' 'Starting' to: 'Error' end: from: 'Working' 'Starting' to: 'End' ;
Inheritance
{ superoptions; } const fsm = initial: 'green' events: warn: from: 'green' to: 'yellow' panic: from: 'yellow' to: 'red' calm: from: 'red' to: 'yellow' clear: from: 'yellow' to: 'green' ;fsm; // => truefsm;
TODO
- streamed version of toDOTsync
License
MIT