Class StateMachine<States, StateMachineEvents>

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.

Events:

  • stateChange(previousState: Signal, nextState: Signal)

Type Parameters

Hierarchy

Constructors

Properties

_activeChildChip?: Chip
_childChips: Record<string, Chip>
_chipContext: Readonly<Record<string, any>>
_eventListeners: IEventListener[] = []
_inputSignal?: Signal<string>
_lastSignal?: Signal<keyof States & string>
_lastTickInfo: TickInfo
_listeners: Listener<StateMachineEvents, any>[]
_outputSignal?: Signal<string>
_preparation?: ChipResolvable
_reloadMemento?: ReloadMemento
_signals: SignalTable<keyof States & string>
_startingState: SignalDescriptor<keyof States & string>
_state: ChipState = "inactive"
_states: StateTable
_visitedStates: Signal<keyof States & string>[]
options: StateMachineOptions<keyof States & string>

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 defaultChildChipContext(): undefined | ChipContextResolvable
  • Template getter for the chip context provided to children. Overload to add extra attributes to the context.

    Returns undefined | ChipContextResolvable

  • get isReady(): boolean
  • Returns boolean

  • get lastSignal(): undefined | Signal<keyof States & string>
  • The last signal used by the machine. If the machine is running, this describes the current state

    Returns undefined | Signal<keyof States & string>

  • get outputSignal(): undefined | Signal<string>
  • Once the chip is terminated, contains the signal

    Returns undefined | Signal<string>

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

    Returns Signal<keyof States & string>[]

Methods

  • Parameters

    • nextState: Signal<keyof States & string>

    Returns void

  • Template method called by resize()

    Parameters

    • width: number
    • height: number

    Returns void

  • 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.

    Type Parameters

    • Emitter extends object

    • EventNames extends BaseEventNames

    • EventName extends string

    Parameters

    • emitter: Emitter
    • eventName: EventName
    • cb: ((this, ...args) => void)
        • (this, ...args): void
        • Parameters

          • this: StateMachine<States, StateMachineEvents>
          • Rest ...args: EventNames[EventName]

          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.

    Type Parameters

    • Emitter extends object

    • EventNames extends BaseEventNames

    • EventName extends string

    Parameters

    • emitter: Emitter
    • eventName: EventName
    • cb: ((this, ...args) => void)
        • (this, ...args): void
        • Parameters

          • this: StateMachine<States, StateMachineEvents>
          • Rest ...args: EventNames[EventName]

          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: NodeEventSource
    • 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

    • tickInfo: TickInfo
    • chipContext: Readonly<Record<string, any>>
    • Optional inputSignal: Signal<string>
    • Optional reloadMemento: ReloadMemento

    Returns void

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

    Parameters

    • nextState: keyof States & string | Signal<keyof States & string>

    Returns void

  • Type Parameters

    • Name extends string | number | symbol

    Parameters

    • name: Name
    • Rest ...params: StateMachineEvents[Name]

    Returns void

  • Type Parameters

    • Name extends string | number | symbol

    Parameters

    • Optional name: Name
    • Optional run: ListenerFunction<StateMachineEvents, Name>

    Returns void

  • Type Parameters

    • Name extends string | number | symbol

    Parameters

    • name: Name
    • run: ListenerFunction<StateMachineEvents, Name>

    Returns void

  • Type Parameters

    • Name extends string | number | symbol

    Parameters

    • name: Name
    • run: ListenerFunction<StateMachineEvents, Name>

    Returns void

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

    Returns void

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

    Parameters

    • outputSignal: Signal<string> = ...

    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