Finite State Machine in TypeScript
npm install sibvrv/MicroFSM
npm run build
- transpile TypeScript to ES6
In addition to the general-purpose events, transitions can be observed using your specific transition and state names:
onAfter<TRANSITION>
- fired after a specific TRANSITION completesonBefore<TRANSITION>
- fired before a specific TRANSITION beginson<STATE>
- fired when entering a specific STATEonChange
- fired after any transition
const state = new StateMachine({
initial: 'start',
states: {
start: ['end', 'start'],
end: ['start']
},
methods: {
onChange: (prev: string, next: string) => {
console.log(`State changed from ${prev} to ${next}`);
},
onBeforeStart: (prev: string, param: string) => {
console.log(`We can check the parameters before the start: ${param}`);
},
onAfterStart: (next: string) => {
console.log(`Going to ${next} state`);
},
onEnd: (prev: any, param: any) => {
console.log(`Game over! ${param}`);
}
}
});
state.to('end', 'Is it the End of the Line for the RED team?');
state.to('start', 'Hello world!');