makina.js
Simple finite state machine. With guard conditions and callbacks
Install
$ npm install --save makina
How to use
Basic example:
; // Some guard conditionsconst always = true;const never = false; // Create a simple SMconst myStateMachine = ; myStateMachinestart;// STATE 2
The createStateMachine
method receives a state machine config object with only 2 keys:
initState
: the initial state of the state machine.transitions
: a list with the transition definitions. A transition definition has the format:
'from state' 'transitionName' guardCondition 'to state'
Guard conditions
When a state machine (sm
) is in the estate 'from state'
and the sm.transitionName(data)
method is called, it
will return the state 'to state'
if guardCondition(data) == true
.
If the guard condition is not satisfied but there is another transition definition with the same name and a different guard condition, it will be executed.
const isEven = n % 2 === 0;const isOdd = !; const myStateMachine = ; myStateMachinestart2; // STATE1myStateMachinestart1; // STATE2
Callbacks
You can register a callback to be called after a transition is executed:
const myStateMachine = ; myStateMachinestart'hello callback'; // hello callback
Run tests
$ npm install
$ npm test