Interface StateMachine<EventPayloadMapping, Context, States>

This is the interface for the state machine. The interface takes in a few generic parameters.

Generic parameters:

  • EventPayloadMapping: A mapping of events to their payloads.
  • Context: The context of the state machine. (which can be used by each state to do calculations that would persist across states)
  • States: All of the possible states that the state machine can be in. e.g. a string literal union like "IDLE" | "SELECTING" | "PAN" | "ZOOM"

You can probably get by using the TemplateStateMachine class. The naming is that an event would "happen" and the state of the state machine would "handle" it.

interface StateMachine<
    EventPayloadMapping,
    Context extends BaseContext,
    States extends string = "IDLE",
> {
    possibleStates: States[];
    states: Record<
        States,
        State<
            EventPayloadMapping,
            Context,
            string extends States ? string : States,
        >,
    >;
    happens<K extends string | number | symbol>(
        event: K,
        payload: EventPayloadMapping[K],
        context: Context,
    ): States;
    onHappens(
        callback: (
            event: keyof EventPayloadMapping,
            payload: EventPayloadMapping[keyof EventPayloadMapping],
            context: Context,
        ) => void,
    ): void;
    onStateChange(callback: StateChangeCallback<States>): void;
    setContext(context: Context): void;
    switchTo(state: States): void;
}

Type Parameters

  • EventPayloadMapping
  • Context extends BaseContext
  • States extends string = "IDLE"

Implemented by

Properties

possibleStates: States[]
states: Record<
    States,
    State<
        EventPayloadMapping,
        Context,
        string extends States ? string : States,
    >,
>

Methods