New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

makina

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

makina

Simple finite state machine. With guard conditions and callbacks

1.0.0
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

makina.js

npm npm

Simple finite state machine. With guard conditions and callbacks

Install

$ npm install --save makina

How to use

Basic example:

import createStateMachine from 'makina';

// Some guard conditions
const always = () => true;
const never = () => false;

// Create a simple SM
const myStateMachine = createStateMachine({
    initState: 'INIT',
    transitions: [
        ['INIT',   'start',    always,  'STATE1'],
        ['STATE1', 'goBack',   always,  'INIT'],
        ['STATE1', 'continue', always,  'STATE2'],
        ['STATE2', 'goBack',   never,   'INIT'],
        ['STATE2', 'goBack',   always,  'STATE1'],
        ['STATE2', 'finish',   always,  'END']
    ]
});

myStateMachine.start().continue().goBack().continue().getState();
// STATE 2

The createStateMachine method receives a state machine config object with only 2 keys:

  • initState: the initial state of the state machine.
  • transitions: a list with the transition definitions. A transition definition has the format:
['from state', 'transitionName', guardCondition, 'to state']

Guard conditions

When a state machine (sm) is in the estate 'from state' and the sm.transitionName(data) method is called, it will return the state 'to state' if guardCondition(data) == true.

If the guard condition is not satisfied but there is another transition definition with the same name and a different guard condition, it will be executed.

const isEven = (n) => n % 2 === 0;
const isOdd = (n) => !isEven(n);

const myStateMachine = createStateMachine({
    initState: 'INIT',
    transitions: [
        ['INIT', 'start', isEven, 'STATE1'],
        ['INIT', 'start', isOdd,  'STATE2']
    ]
});

myStateMachine.start(2).getState(); // STATE1
myStateMachine.start(1).getState(); // STATE2

Callbacks

You can register a callback to be called after a transition is executed:

const myStateMachine = createStateMachine({
    initState: 'INIT',
    transitions: [
        ['INIT', 'start', always, 'STATE1', (msg) => console.log(msg)]
    ]
});

myStateMachine.start('hello callback'); // hello callback

Run tests

$ npm install
$ npm test

Keywords

state machine

FAQs

Package last updated on 01 Nov 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts