Class StateMachine<StateT, EventT>

State machine class.

This class manages state machines and their events emits.

The state machines emit 4 event types:

  • onEnterState(enteredState: StateT) - Emitted when the state machine enters a new state. If the entered state has subStates, the event will be emitted for each subStateMachine, from top to the bottom.
  • onLeaveState(leftState: StateT) - Emitted when the state machine leaves a state. If the left state has subStates, the event will be emitted for each subStateMachine, from the bottom to the top.
  • onStateChange(srcState: StateT, dstState: StateT, event: EventT) - Emitted when a transition happens. The source state and the destination state represent the deepest states of the transition.
  • srcStates: StateT[], dstStates: StateT[], event: EventT) - Emitted when a transition happens. The source states and the destination states contain all the subStates of the source and destination states, from top to the bottom.

The history configuration gives the possibility to specify if leaving a state will reset the sub state machine to the initial state or not. If the history flag is set to true, the sub state machine is not reset and when the parent state machine enters the state back, the old state of the sub state machine is restored. The history configuration can be optionally set through the setInitialState call. Default value is 'false'.

Transitions between current state machine and states of the subStateMachines can be defined, only if the source state or the destination state represent states of the state machine for which the transition is defined.

// Example (see StateMachine uml from Usage):
stateMachine.addTransition('eventName', 'NotInitialized', 'Initialized') // -> OK, both states are state of stateMachine
stateMachine.addTransition('eventName', 'NotListening', 'Listening') // -> throws, none of the states is a state of stateMachine
stateMachine.addTransition('eventName', 'NotInitialized', 'Listening') // -> OK, 'NotInitialized' is a state of stateMachine and 'Listening' is a sub state
stateMachine.addTransition('eventName', 'NotListening', 'NotInitialized') // -> OK, 'NotInitialized' is a state of stateMachine and 'NotListening' is a sub state

The following state machine will be created in typescript: hide empty description [] -> NotInitialized Initialized -left-> NotInitialized : shutdown NotInitialized -right-> Initialized[H] : init state Initialized { [] --> NotListening NotListening -right-> Listening : startListen Listening -left-> NotListening : stopListen }

// The state machine must be created from bottom to the top. Therefore the sub state machine is created first.
const subStateMachine = new StateMachine<SMStates, SMEvents>();
subStateMachine.addState('Listening');
subStateMachine.addState('NotListening');
// history configuration is set through 'setInitialState'
subStateMachine.setInitialState('NotListening',true);
subStateMachine.addTransition('startListen','NotListening','Listening');
subStateMachine.addTransition('stopListen', 'Listening', 'NotListening');

// Create top state machine and set the subState machine with addState.
const stateMachine = new StateMachine<SMStates, SMEvents>();
stateMachine.addState('NotInitialized');
// set the subStateMachine for the 'Initialized' state
stateMachine.addState('Initialized', subStateMachine);
stateMachine.setInitialState('shutdown');
stateMachine.addTransition('init','NotInitialized','Initialized');
stateMachine.addTransition('shutdown', 'Initialized', 'NotInitialized');

// listen for events
stateMachine.onEnterState( enteredState => {
//...
}

// trigger events
stateMachine.triggerEvent('init');
stateMachine.triggerEvent('startListen');

Type Parameters

  • StateT extends string
  • EventT

Constructors

Properties

onEnterState: OEvent<((enteredState: StateT) => void)> = ...

Emitted when the state machine enters a state.

onLeaveState: OEvent<((leftState: StateT) => void)> = ...

Emitted when the state machine leaves a state.

onStateChange: OEvent<((srcState: StateT, dstState: StateT, event: EventT) => void)> = ...

Emitted when the state machine executes a transition. The srcState and the dstState values represent the deepest source state and destination state respectively.

onStatesChange: OEvent<((srcStates: StateT[], dstStates: StateT[], event: EventT) => void)> = ...

Emitted when the state machine executes a transition. The srcStates and the dstStates arrays contain the full state hierarchy, from top to the bottom.

Accessors

  • get currentStates(): StateT[]
  • Current state of the state machine as an array, including all subStateMachines current states, from top to the bottom.

    Returns StateT[]

Methods

  • Add a new state to the state machine. If the subStateMachine parameter is present, it means the given state has subStates, represented by the given subStateMachine.

    Parameters

    Returns void

  • Add a transition to the state machine.

    Parameters

    • event: EventT

      The event which triggers the transition.

    • srcState: StateT

      The source state of the transition. It must be either a state of the current state machine or a sub state, only if dstState is a state of the current state machine.

    • dstState: StateT

      The destination state of the transition. It must be either a state of the current state machine or a sub state, only if srcState is a state of the current machine.

    Returns void

  • Checks if the current state is the one passed, if not, throws an error.

    Parameters

    • state: StateT

      The state to check the current state.

    Returns void

  • Search for the state in the subStateMachines.

    Returns an array of states, from top to the bottom, the last state in the array being the state given as parameter.

    Parameters

    • state: StateT

      The state to be located. Will be the last in the array.

    Returns StateT[]

  • Reset to the initial state the stateMachine and its subStateMachines, if they don't have history.

    Parameters

    • event: EventT

      The event which triggered the reset.

    Returns void

  • Set the initial state and the history of the state machine.

    Parameters

    • state: StateT

      the initial state.

    • hasHistory: boolean = false

      rather the state machine has history or not. Defaults to false.

    Returns void

  • Triggers the given event.

    • If the event maps to a transition in the state machine, it will execute the transition, otherwise the event is propagated to the subStateMachines.
    • If the given event doesn't map to a transition in the state machine or its subStateMachines, it will be ignored.

    Parameters

    • event: EventT

      The triggered event.

    Returns void