Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FSM

Minimal finite state machine implementation. Allows directed transitions between states.

States are provided as an object, with each key being single distinct state. Each state's value within the object should be an array of strings, which dictates the available states that are legal to transition to.

If a state may transition to any other state, the string '*' may be provided instead of an array.

If a state is terminal, it should simply provide an empty array.

Example:

// Create a state machine with three states:
// 'a' may transition to 'b' or 'c'
// 'b' is a terminal state
// 'c' may transition to any state

var states = {
    a : ['b', 'c'],
    b : [],
    c : '*'
};

var fsm = new FSM('a', states);

states.change('c'); // => true
states.change('a'); // => true
states.change('b'); // => true
states.change('c'); // => false
states.change('a'); // => false

Hierarchy

Implements

Index

Constructors

Properties

Methods

Constructors

constructor

  • new FSM(initial: string, states: {}): FSM
  • Create a new finite state machine

    Parameters

    • initial: string

      the initial state that this FSM should be in

    • states: {}

      the set of possible states to transition between

      • [state: string]: "*" | string[]

    Returns FSM

Properties

Private current

current: string | string[]

The currently allowed transitions

Private emitter

emitter: Emitter

Emitter for notifying of state changes

state

state: string

The current state

Private states

states: {}

The states and their allowed transitions

Type declaration

  • [state: string]: "*" | string[]

Methods

change

  • change(state: string): boolean
  • Change the state. Will return whether the transition was allowed or not.

    Parameters

    • state: string

      the new state

    Returns boolean

    true is the change of state was successful

close

  • inheritdoc

    Will cause closeCallback to be called.

    Parameters

    • Optional reason: any

      the reason for closing the stream. This can be of any type. The type of the close reason should be documented in the class extending StreamImpl.

    Returns Stream

error

  • Close the stream. This will emit a 'close' event to any assigned listeners. No further events will be emitted.

    Parameters

    Returns Stream

off

  • Remove a listener from a specified event.

    Example:

    // Bind a single listener to the 'foo' event and then deregister it
    var listener = function() {};
    stream.on('foo', listener);
    stream.off('foo', listener);
    

    Example:

    // Bind a listener to the 'foo' event and deregister all listeners
    var listener = function() {};
    stream.on('foo', listener);
    stream.off('foo');
    

    Parameters

    • events: string | CallbackMap
    • Optional listener: StreamCallback

      the listener to remove. All listeners for the event are removed if this is not specified. This argument is ignored if the first argument is a CallbackMap.

    Returns Stream

    this stream.

on

  • Register listeners against events.

    A single listener may be bound to an event by passing the event name and listener function.

    Multiple listeners may be bound by passing in a CallbackMap, mapping event names to listener functions.

    Example:

    // Bind a single listener to the 'foo' event
    stream.on('foo', function(arg1, arg2) {
        console.log("Called for 'foo' event", arg1, arg2);
    });
    

    Example:

    // Bind multiple listeners
    stream.on({
        foo : function() { ... },
        bar : function() { ... },
        baz : function() { ... }
    });
    

    It is possible to bind multiple listeners to the same event. In this case, the listeners will be called in the order they were bound.

    Parameters

    • events: string | CallbackMap

      the event name or CallbackMap mapping event names to listeners

    • Optional listener: StreamCallback

      the listener to bind to the event, if passed as string. This argument is ignored if the first argument is a CallbackMap.

    Returns Stream

    this stream.

Copyright © 2025 DiffusionData Ltd. All Rights Reserved.