Class StateMachine

Represents a state machine, where each state has a name, and is represented by an chip. Only one state is active at a time. The state machine has one starting state, but can have multiple ending states. When the machine reaches an ending state, it terminates with a name equal to the name of the ending state. By default, the state machine begins at the state called "start", and stops at "end".

When the active state chip terminates, the state machine transitions to another. To determine the next state, it first looks if there is a corresponding entry in the signals table, which can be either a state name or a function that takes (ChipContext, Signal) and returns a signal. If there is nothing in the signal table for the state, it next looks if the terminating signal is the name of another state, in which case it switches directly to that state,

If you want to create embedded signal tables, try the makeSignalTable() function.

Hierarchy (view full)

Constructors

Properties

_activeChildChip: Chip
_childChips: Record<string, Chip>
_chipContext: Readonly<Record<string, any>>
_eventListeners: IEventListener[] = []
_inputSignal: Signal
_lastSignal: Signal
_lastTickInfo: TickInfo
_outputSignal: Signal
_reloadMemento?: ReloadMemento
_signals: SignalTable = {}
_startingState: SignalDescriptor
_state: ChipState = "inactive"
_states: StateTable = {}
_visitedStates: Signal[]
prefixed: string | boolean

Accessors

  • get children(): Record<string, Chip>
  • Children of this chip, if any

    Returns Record<string, Chip>

  • get chipContext(): Readonly<Record<string, any>>
  • Returns Readonly<Record<string, any>>

  • get lastSignal(): Signal
  • The last signal used by the machine. If the machine is running, this describes the current state

    Returns Signal

  • get outputSignal(): Signal
  • Once the chip is terminated, contains the signal

    Returns Signal

  • get visitedStates(): Signal[]
  • An array of all the signals the machine has gone through, in order. It may contain duplicates

    Returns Signal[]

Methods

  • Start listening to events of a certain type emitted by an object. The callback will be called with the chip as this. Works by default for both NodeJS- and DOM-style events. If you are interfacing with a different event system, you can provide a subscriptionHandler that knows how to handle it.

    Parameters

    • emitter: object
    • event: string
    • cb: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional subscriptionHandler: SubscriptionHandler

    Returns void

  • Listen to a single event emitted by an object, then stop. The callback will be called with the chip as this. Works by default for both NodeJS- and DOM-style events. If you are interfacing with a different event system, you can provide a subscriptionHandler that knows how to handle them.

    Parameters

    • emitter: object
    • event: string
    • cb: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional subscriptionHandler: SubscriptionHandler

    Returns void

  • Unsubscribe to a set of events. By default, unsubscribes to everything. If emitter, event, or cb is provided, unsubscribe only to those.

    Parameters

    • Optional emitter: object
    • Optional event: string
    • Optional cb: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: unknown[]

          Returns void

    Returns void

  • Activate the chip, with a provided context and input signal. Should only be called from an inactive state

    Parameters

    Returns void

  • Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns this

  • Switch directly to a new state, terminating the current one

    Parameters

    Returns void

  • Calls each of the listeners registered for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Rest ...args: any[]

    Returns boolean

  • Return an array listing the events for which the emitter has registered listeners.

    Returns (string | symbol)[]

  • Return the number of listeners listening to a given event.

    Parameters

    • event: string | symbol

    Returns number

  • Return the listeners registered for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T

    Returns ((...args) => void)[]

  • Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any
    • Optional once: boolean

    Returns this

  • Add a listener for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns this

  • Add a one-time listener for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns this

  • Pause the chip, informing it that it won't receive ticks for a while

    Parameters

    Returns void

  • Remove all listeners, or those of the specified event.

    Parameters

    • Optional event: string | symbol

    Returns this

  • Remove the listeners of a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any
    • Optional once: boolean

    Returns this

  • Terminate the chip. Should only be called from an active or paused state

    Parameters

    Returns void

  • By default, updates all child chips and remove those that have a signal Overload this method in subclasses to change the behavior

    Parameters

    Returns void

Generated using TypeDoc