A state machine that fires events when state changes.

const transitions = StateMachine.fromList(`a`, `b`, `c`);
const m = new StateMachineWithEvents(transitions);
m.addEventListener(`change`, event => {
console.log(`${event.priorState} -> ${event.newState}`);
});
m.addEventListener(`stop`, event => {
console.log(`State machine has reached final state`);
});

Type Parameters

Hierarchy (view full)

Constructors

Accessors

  • get changedAt(): number
  • Returns timestamp when state was last changed. See also elapsed

    Returns number

  • get elapsed(): number
  • Returns milliseconds elapsed since last state change. See also changedAt

    Returns number

  • get isDisposed(): boolean
  • Returns boolean

  • get isDone(): boolean
  • Returns true if state machine is in its final state

    Returns boolean

  • get state(): string
  • Returns string

  • set state(newState): void
  • Gets or sets state. Throws an error if an invalid transition is attempted. Use isValid() to check validity without changing.

    If newState is the same as current state, the request is ignored silently.

    Parameters

    Returns void

  • get statesDefined(): readonly StateNames<V>[]
  • Return a list of all defined states

    Returns readonly StateNames<V>[]

  • get statesPossible(): readonly (null | StateNames<V>)[]
  • Return a list of possible states from current state.

    If list is empty, no states are possible. Otherwise lists possible states, including 'null' for terminal

    Returns readonly (null | StateNames<V>)[]

Methods

  • Returns true if newState is valid transition from current state. Use validateTransition if you want an explanation for the false results.

    Parameters

    Returns boolean

  • Moves to the next state if possible. If multiple states are possible, it will use the first. If machine is finalised, no error is thrown and null is returned.

    Returns null | string

    Returns new state, or null if machine is finalised

  • Resets machine to initial state

    Returns void

  • Throws if it's not valid to transition to newState

    Parameters

    Returns void