Skip to content

@ue-too/being / index / StateMachine

Interface: StateMachine<EventPayloadMapping, Context, States, EventOutputMapping> ​

Defined in: interface.ts:212

Description ​

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"
  • EventOutputMapping: A mapping of events to their output types. Defaults to void for all events.

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.

See ​

Type Parameters ​

EventPayloadMapping ​

EventPayloadMapping

Context ​

Context extends BaseContext

States ​

States extends string = "IDLE"

EventOutputMapping ​

EventOutputMapping extends Partial<Record<keyof EventPayloadMapping, unknown>> = DefaultOutputMapping<EventPayloadMapping>

Properties ​

context? ​

readonly optional context: Context

Defined in: interface.ts:229

Read-only access to the machine's live context object. Optional so existing StateMachine implementations remain valid; TemplateStateMachine always provides it. Intended for tooling/introspection (e.g. visualizers evaluating guards against the current context) — mutate state through events, not through this reference.


currentState ​

currentState: States | "INITIAL" | "TERMINAL"

Defined in: interface.ts:289


possibleStates ​

possibleStates: States[]

Defined in: interface.ts:258


states ​

states: Record<States, State<EventPayloadMapping, Context, string extends States ? string : States, EventOutputMapping>>

Defined in: interface.ts:242

Methods ​

happens() ​

Call Signature ​

happens<K>(...args): EventResult<States, K extends keyof EventOutputMapping ? EventOutputMapping[K<K>] : void>

Defined in: interface.ts:231

Type Parameters ​
K ​

K extends string | number | symbol

Parameters ​
args ​

...EventArgs<EventPayloadMapping, K>

Returns ​

EventResult<States, K extends keyof EventOutputMapping ? EventOutputMapping[K<K>] : void>

Call Signature ​

happens<K>(...args): EventResult<States, unknown>

Defined in: interface.ts:238

Type Parameters ​
K ​

K extends string

Parameters ​
args ​

...EventArgs<EventPayloadMapping, K>

Returns ​

EventResult<States, unknown>


onEventResult()? ​

optional onEventResult(callback): void | () => void

Defined in: interface.ts:283

Subscribe to every event result. Optional so existing StateMachine implementations remain valid; TemplateStateMachine always provides it. Returns a disposer on implementations that support one. Disposing during a dispatch takes effect starting with the next dispatch, not the one in progress — see EventResultCallback for the exact snapshot-iteration semantics.

Parameters ​

callback ​

EventResultCallback<EventPayloadMapping, Context, States>

Returns ​

void | () => void


onHappens() ​

onHappens(callback): void | () => void

Defined in: interface.ts:266

Subscribe to every happens() call, before the state handles it. Returns a disposer on implementations that support one. Disposing during a dispatch takes effect starting with the next dispatch, not the one in progress — see EventResultCallback for the exact snapshot-iteration semantics.

Parameters ​

callback ​

(args, context) => void

Returns ​

void | () => void


onStateChange() ​

onStateChange(callback): void | () => void

Defined in: interface.ts:257

Subscribe to state changes. Returns a disposer on implementations that support one. Disposing during a dispatch takes effect starting with the next dispatch, not the one in progress — see EventResultCallback for the exact snapshot-iteration semantics.

Parameters ​

callback ​

StateChangeCallback<States>

Returns ​

void | () => void


reset() ​

reset(): void

Defined in: interface.ts:286

Returns ​

void


setContext() ​

setContext(context): void

Defined in: interface.ts:241

Parameters ​

context ​

Context

Returns ​

void


start() ​

start(): void

Defined in: interface.ts:287

Returns ​

void


switchTo() ​

switchTo(state): void

Defined in: interface.ts:220

Parameters ​

state ​

States

Returns ​

void


wrapup() ​

wrapup(): void

Defined in: interface.ts:288

Returns ​

void