Finite state machine for TypeScript and JavaScript
JavaScript TypeScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

state

Executable finite state machine for TypeScript and JavaScript.

NPM Version NPM Downloads Build Status Code Climate Issue Count Test Coverage

Notes:

@steelbreeze/state the new home for state.js and the versioning starts here from v6.0.0.

If you like @steelbreeze/state, please star it...

Install

npm i @steelbreeze/state

Usage

The API is broken up into two distinct parts:

  1. A set of classes that represent a state machine model (State, PseudoState, Region, Transition, etc.);
  2. An interface to represent an instance of a state machine, which embodies the active state configuration of a state machine at runtime. This enables multiple instances adhering to the same state machine model. There are also a couple of implementations of the interface.

TypeScript

import * as state from "@steelbreeze/state";

// create the state machine model elements
const model = new state.StateMachine("model");
const initial = new state.PseudoState("initial", model, state.PseudoStateKind.Initial);
const stateA = new state.State("stateA", model);
const stateB = new state.State("stateB", model);

// create the state machine model transitions
initial.to(stateA);
stateA.to(stateB).when((instance, message) => message === "move");

// create a state machine instance
let instance = new state.JSONInstance("instance");

// initialise the model and instance
model.initialise(instance);

// send the machine instance a message for evaluation, this will trigger the transition from stateA to stateB
model.evaluate(instance, "move");

console.log(instance.toJSON());

Output

The output of the above code will be:

initialise model
initialise instance
instance enter model.default
instance enter model.default.initial
instance leave model.default.initial
instance enter model.default.stateA
instance evaluate message: move
instance leave model.default.stateA
instance enter model.default.stateB
{"name":"instance","children":[{"name":"default","children":[],"current":"stateB","lastKnownState":"stateB"}]}

License

MIT License

Copyright (c) 2014-8 David Mesquita-Morris