Hierarchical State Machine implementation, with support for guards and enter/exit events.
It extends FSM, read its documentation before reading this.
$ npm install --save @fabiospampinato/hsm
import HSM from '@fabiospampinato/hsm';
// The `states` object is similar to FSM's.
// The only difference being that an optional `states` key can be used to nest states.
const states = {
alive: {
states: {
standing: {
transitions: {
walk: 'walking'
}
},
walking: {
transitions: {
stop: 'standing',
speedUp: 'running'
}
},
running: {
transitions: {
slowDown: 'walking'
}
}
}
},
dead {}
};
// The `model` object doesn't differ from the one FSM uses.
const Model = new class {
walk () {
console.log ( 'Starting to walk...' );
}
walkingEnter () {
console.log ( 'Now I\'m walking' );
}
walkingExit () {
console.log ( 'Now I\'n not walking anymore' );
}
};
const machine = new HSM ( Model, states, 'standing' );
machine.transition ( 'walk' );
machine.is ( 'walking' ); // true
machine.is ( 'alive' ); // true
machine.is ( 'alive', true ); // false
The API is the same that FSM provides, except for the following differences:
Here the initial
state can be implicit, in that case the root state will be setted as the initial one.
If there are multiple states at the root level an error will be thrown.
By default it will return true also for parent states, if you only want it to match the current state pass true
as the exactly
argument.
- FSM - Finite State Machine implementation, with support for guards and enter/exit events.
MIT © Fabio Spampinato