mf

mini-fsm

A js/nodejs minimum Finite State Machine with fully ES6/ES7 support

Showing:

Popularity

Downloads/wk

0

GitHub Stars

3

Maintenance

Last Commit

5yrs ago

Contributors

0

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

No?

Readme

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'
  }
})();

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Tutorials

No tutorials found
Add a tutorial