mini-fsm

1.1.0 • Public • Published

mini-fsm - A js/NodeJs minimum Finite State Machine (FSM)

NPM version Build Status Dependencies Status codecov npm License

Features

  • Minimalism, just a single file without any dependency
  • Semantics, pure ES6 class based, a fsm is a class definition
  • Callbacks support Promise & ES7 async/await

Quick Start

NodeJS version require >= v6

In Node.js you can install mini-fsm with npm:

npm install mini-fsm

A state machine can be defined as a class contains states and transitions, let's create an FSM to represent how traffic light works.

First we should define a fsm as bellow:

import FiniteStateMachine from 'mini-fsm';
 
export default class TrafficLightFsm extends FiniteStateMachine {
  get states() {
    return {
      'green': {},
      'yellow': {},
      'red': {}
    };
  }
 
  get transitions() {
    return {
      turnYellow: { from: 'green', to: 'yellow' },
      turnRed: { from: 'yellow', to: 'red' },
      turnGreen: { from: 'red', to: 'green' }
    };
  }
}

Then create a traffic light and make it work:

const light = new TrafficLightFsm('green'); //initial state is green
console.log(light.getCurrentState()); //output 'green'
(async () => {
  await light.do().turnYellow();
  console.log(light.getCurrentState()); //output 'yellow'
  await light.do().turnRed();
  console.log(light.getCurrentState()); //output 'red'
  await light.do().turnGreen();
  console.log(light.getCurrentState()); //output 'green'
})();

If state transfer not as expected, an Error will be throw:

const light = new TrafficLightFsm('green');
(async () => {
  try {
    await light.do().turnRed();
  } catch (e) {
    console.error(e.message); //output 'Error: Transition from green to red denied'
  }
})();

Package Sidebar

Install

npm i mini-fsm

Weekly Downloads

1

Version

1.1.0

License

MIT

Last publish

Collaborators

  • allovince