state
Executable finite state machine for TypeScript and JavaScript.
If you like @steelbreeze/state, please star it...
Note: v8 is now live and contains breaking changes but offers a further simplified code base performance improvements. See the release notes for more information.
Warning: v8 does not yet contain any support for serialization due to the challanges brought by the introduction of deferred events which are cached within the state machine instance alongside the active state configuration.
Install
npm i @steelbreeze/state
Usage
The API is broken up into two distinct parts:
- A set of classes that represent a state machine model (State, PseudoState, Region, etc.);
- An class managing the active state configuration of a state machine instance at runtime (Instance).
Together, they enable multiple instances of the same state machine model.
The full API reference can be found here.
TypeScript
import * as state from "@steelbreeze/state";
class MyEvent {
public constructor(public fieldA: string, public fieldB: number) { }
public toString(): string {
return JSON.stringify(this);
}
}
state.log.add(message => console.info(message), state.log.Entry | state.log.Exit | state.log.Evaluate);
const model = new state.State("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);
initial.to(stateA);
stateA.on(MyEvent).when(myEvent => myEvent.fieldB > 2).to(stateB);
let instance = new state.Instance("instance", model);
instance.evaluate(new MyEvent("test", 1));
instance.evaluate(new MyEvent("test", 3));
JavaScript (ECMAScript 2015)
var state = require("@steelbreeze/state");
class MyEvent {
constructor(fieldA, fieldB) { this.fieldA = fieldA; this.fieldB = fieldB; }
toString() { return JSON.stringify(this); }
}
state.log.add(message => console.info(message), state.log.Entry | state.log.Exit | state.log.Evaluate);
const model = new state.State("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);
initial.to(stateA);
stateA.on(MyEvent).when(myEvent => myEvent.fieldB > 2).to(stateB);
let instance = new state.Instance("instance", model);
instance.evaluate(new MyEvent("test", 1));
instance.evaluate(new MyEvent("test", 3));
Output
The output of the above code will be:
instance enter model
instance enter model.default
instance enter model.default.initial
instance leave model.default.initial
instance enter model.default.stateA
instance evaluate {"fieldA":"test","fieldB":1}
instance evaluate {"fieldA":"test","fieldB":3}
instance leave model.default.stateA
instance enter model.default.stateB
Note that in the example above, a default region is inserted as a child of model
and parent of initial
, stateA
and stateB
; the name of default regions copy their parent state hence seeing model.model
in the output above.
License
MIT License
Copyright (c) 2022 David Mesquita-Morris