- Notifications
You must be signed in to change notification settings - Fork0
simple asynchronous finite state machine
License
NotificationsYou must be signed in to change notification settings
sscholle/ts-async-state-machine
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
the objective is to provide a simple interface to build a finite state machine
- thoroughly tested with good code coverage
- small package file: 3.2kb (compressed)
- written in TypeScript and therefore includes a definitions file with full documentation (*.d.ts)
- Define a set of
State
s, each with aname
, a list offromStates
and some optional callbacks - Call
.transition('newStateName')
to trigger a state change to the specified state - Use
Promises
inside theonBeforeEnter
callback to resolve/reject the state change (usually via server API)
import{Machine}from"ts-async-state-machine";// Create your MachineconstMicrowaveMachine=newMachine('Microwave');// Add States to the MachineMicrowaveMachine.addState('off',['running'],{onBeforeEnter:(prevState)=>newPromise((resolve,rejection)=>{// use a server APi request to 'confirm' the state changeresolve();// resolves the state transition - changes 'MicrowaveMachine's state to 'off'// a 'rejection' will fail the internal state transition})});MicrowaveMachine.addState('on',['off'],{// callbacks are optional and use them as needed: 'onEnter', 'onExit', 'onBeforeEnter' as optionsonExit:()=>{console.log('Exited ON state')}});MicrowaveMachine.addState('running',['on'],{onEnter:()=>{console.log('Entered RUNNING State')}});// Initialise into your required starting stateMicrowaveMachine.start('off');// Start Triggering State changes when neccessaryMicrowaveMachine.transition('on').then(newState=>{console.log('Entered State',newState.name)});// omitted 'catch' for brevityMicrowaveMachine.transition('running').then(newState=>{console.log('Entered State',newState.name)});
- see
/examples
folder for a full examples inmicrowave.js
,traffic-light.js
andtraffic-light-server-stateful.js
- run microwave example:
npm run example
- other examples:
npm run build && node ./examples/traffic-light.js
- other examples:
npm run build && node ./examples/traffic-light-server-stateful.js
- build:
npm run build
- test:
npm run test
- code coverage:
npm run test:coverage
- Inspired by the Js Async State machine libhttps://github.com/robguy21/js-async-state-machine
About
simple asynchronous finite state machine
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.