Statemachine
The only statemachine where rules and states are fully handled independent. It is also possible to add a statemachine to another statemachine, run in async or sync mode, transit form step to step from machine to machine while link different statemachines together. This helps dividing big problems into smaller ones which can be solved much easier.
Content:
- Usage
- API
- setAsync
- addTransition
- addState
- setContext
- addStateMachine
- Events
- final
- error
- Errorhandling
- Test
- Issues
- Support
Usage
var StateMachine = StateMachine; var machine = ; // Adding state transition rulesmachine; // Adding states ; // Run the stateMachinemachine;
API
setAsync
Signature: .setAsync( true | false )
Defines if the statemachine should run in sync or async mode. Async mode is the default.
Here is an exmaple how the statemachine works in async (default) mode:
// Async modevar isTested = true; // optional -> async true is default ; console // outputs true
Here is an exmaple how the statemachine works in sync (default) mode:
// Async modevar isTested = true; // statemachine should run in sync mode ; console // outputs false
addTransition
Signature: .addTransition( object | array of objects )
Adds a new transition rule to the statemachine. A transition rule defines the "route" how to transit from one state to another. The expected parameter must be an object defined as follows:
;
This example expects the outcome of state start
to be yourOutcome
and will transit to state
state1
. If the outcome of state start
is not available via rules a
StateNotFoundError
will be the result. See Errorhandling
addState
Signature: .addState( string, function ) | .addState( object )
Adds a state to the statemachine. The name of the state is used in transitions to identify the state in dependency of the outcome of each state.
;
setContext
Signature: .setContext( object | any )
Adds a context object to the statemachine. If a context object is provided, the second parameter of the stats's function is the reference to this context object.
;
addStateMachine
Signature: .addStateMachine( maschine )
Adds a statemachine to another statemachine. Now it is possible to switch from maschineA.step1 to maschineB.step2 and backwards. This is done by a special notation inside addTransition.
var m1 = "m1" // Each statemachine MUST have a name ; var m2 = "m2" ; m1; m1;
Events
final
When the stateMachine reaches the final state an event final
is emitted.
statemachine
error
If any error occurs (async or sync mode) the event error
is emitted;
statemachine
See also Errorhandling
Errorhandling
Errorhandling is done by emitting an error if any error occurs.
Each state error is catched either the error is thrown with throw
or by
calling next(new Error(...), ...)
.
Because the statemachine is an original node event emitter you can register
an error listener to receive all errors:
statemachine
The error will be the original error thrown with
throw
or provided by the state'snext(new Error(...))
method.
Test
There are no dependencies. Just run npm test
Issues
If you have any problems please create an issue at https://github.com/DennisAhaus/statemachine/issues
Support
If you want to get support or just discuss an idea please write an email to the author and get in invitation to slack https://hackers-corner.slack.com/#js-smachine
See you soon...