ts-async-state-machine
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Simple Async State Machine

NPM

Description

the objective is to provide a simple interface to build a finite state machine

Features

  • 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)

Core Concepts

  • Define a set of States, each with a name, a list of fromStates and some optional callbacks
  • Call .transition('newStateName') to trigger a state change to the specified state
  • Use Promises inside the onBeforeEnter callback to resolve/reject the state change (usually via server API)

Example Usage

import { Machine } from "ts-async-state-machine";

// Create your Machine
const MicrowaveMachine = new Machine('Microwave');

// Add States to the Machine
MicrowaveMachine.addState('off', ['running'], {
   onBeforeEnter: (prevState) => new Promise((resolve, rejection) => {
       // use a server APi request to 'confirm' the state change
       resolve();// 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 options
   onExit: () => {
       console.log('Exited ON state')
   }
});
MicrowaveMachine.addState('running', ['on'], {
   onEnter: () => {
       console.log('Entered RUNNING State')
   }
});

// Initialise into your required starting state
MicrowaveMachine.start('off');

// Start Triggering State changes when neccessary
MicrowaveMachine.transition('on').then(newState => {
   console.log('Entered State', newState.name)
}); // omitted 'catch' for brevity

MicrowaveMachine.transition('running').then(newState => {
   console.log('Entered State', newState.name)
});

Examples

  • see /examples folder for a full examples in microwave.js, traffic-light.js and traffic-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

Development

  • build: npm run build
  • test: npm run test
  • code coverage: npm run test:coverage

Credits

Readme

Keywords

Package Sidebar

Install

npm i ts-async-state-machine

Weekly Downloads

2

Version

2.0.0

License

Apache-2.0

Unpacked Size

22.2 kB

Total Files

5

Last publish

Collaborators

  • sscholle