statechart.js
The statechart
module provides a State
object that is an implementation
of a Harel Statechart.
Statecharts are an improvement over state machines because they elegantly solve the state explosion problem that is common with state machines. They do this by adding two additional features to state machines - state clustering and concurrent states. State clustering provides an abstraction over lower level states where events can be handled and transitions made in one place instead of many. Concurrent states essentially allow multiple statecharts to operate independently. The presence of concurrent states means that the current state of a statechart is actually a vector of states whose length is not fixed.
More information on statecharts is available here:
- http://www.wisdom.weizmann.ac.il/~harel/papers/Statecharts.pdf
- http://www.wisdom.weizmann.ac.il/~harel/papers/Statecharts.History.pdf
- http://www.amazon.com/Constructing-User-Interface-Statecharts-Horrocks/dp/0201342782
Installation
npm install --save statechartjs
Usage
var State = State; var app = State;
Examples
Lockable Door
var State = State; var door = State; door;doorcurrent; // => [ '/closed/locked' ]door; // *knock knock*doorcurrent; // => [ '/closed/locked' ]door;doorcurrent; // => [ '/closed/unlocked' ]door; // *knock knock*door;doorcurrent; // => [ '/opened' ]door;doorcurrent; // => [ '/closed/unlocked' ]door;doorcurrent; // => [ '/closed/locked' ]
Shallow History State
var State = State; var sc = State; sc;sccurrent; // => ['/a/a.2'] sc;sccurrent; // => ['/b/b.1'] sc;sccurrent; // => ['/a/a.2']
Deep History State
var State = State; var sc = State; sc;sccurrent; // => ['/a/a.2/a.2.2'] sc;sccurrent; // => ['/b/b.2/b.2.3'] sc;sccurrent; // => ['/a/a.2/a.2.1'] sc;sccurrent; // => ['/b/b.2/b.2.3']
State Concurrency
var State = State; var word = State; word;wordcurrent; // => ['/bold/off', '/underline/off', '/align/left', '/bullets/none'] word;wordcurrent; // => ['/bold/on', '/underline/off', '/align/left', '/bullets/none'] word;wordcurrent; // => ['/bold/on', '/underline/on', '/align/left', '/bullets/none'] word;wordcurrent; // => ['/bold/on', '/underline/on', '/align/right', '/bullets/none'] word;wordcurrent; // => ['/bold/on', '/underline/on', '/align/justify', '/bullets/none'] word;wordcurrent; // => ['/bold/on', '/underline/on', '/align/justify', '/bullets/regular'] word;wordcurrent; // => ['/bold/on', '/underline/on', '/align/justify', '/bullets/none'] word;wordcurrent; // => ['/bold/on', '/underline/on', '/align/justify', '/bullets/number'] word;wordcurrent; // => ['/bold/off', '/underline/off', '/align/left', '/bullets/none']