Skip to content

StateMachine

Defined in: state-machine.ts:26

StateMachine interface defines the contract for all state machine implementations in Matchina. It includes core lifecycle methods and hooks for managing state transitions, event handling, and side effects.

See also:

Lifecycle steps:

  1. send(type, ...params) - Initiates a transition event.
  2. resolveExit(ev) - Determines the target state for the event.
  3. transition(ev) - Triggers the transition lifecycle, handling all steps for processing a change event.
  4. guard(ev) - Checks if the transition is allowed.
  5. handle(ev) - Processes the event, may abort if returns undefined.
  6. before(ev) - Prepares for state change, may abort if returns undefined.
  7. update(ev) - Applies the state update.
  8. effect(ev) - Runs side effects, calls leave/enter hooks.
  9. leave(ev) - Called when leaving the previous state.
  10. enter(ev) - Called when entering the new state.
  11. notify(ev) - Notifies subscribers of the change.
  12. after(ev) - Final hook after transition completes.
Type ParameterDefault type
E extends TransitionEventTransitionEvent

transition(change: E): void

Defined in: event-lifecycle.ts:28

Triggers the transition lifecycle, handling all steps for processing a change event.

ParameterType
changeE

void

EventLifecycle.transition


guard(ev: E): boolean

Defined in: event-lifecycle.ts:32

Checks if a transition event is allowed to proceed. Returns true to continue, false to abort.

ParameterType
evE

boolean

EventLifecycle.guard


handle(ev: E): undefined | E

Defined in: event-lifecycle.ts:36

Processes the event. May abort the transition if returns undefined.

ParameterType
evE

undefined | E

EventLifecycle.handle


before(ev: E): undefined | E

Defined in: event-lifecycle.ts:41

Called before the transition is applied. May abort if returns undefined. (Represents a beforeTransition hook, not state entry/exit.)

ParameterType
evE

undefined | E

EventLifecycle.before


update(ev: E): void

Defined in: event-lifecycle.ts:45

Applies the state update.

ParameterType
evE

void

EventLifecycle.update


effect(ev: E): void

Defined in: event-lifecycle.ts:49

Runs side effects for the transition. By default, calls leave and enter hooks.

ParameterType
evE

void

EventLifecycle.effect


leave(ev: E): void

Defined in: event-lifecycle.ts:53

Called when leaving the previous state.

ParameterType
evE

void

EventLifecycle.leave


enter(ev: E): void

Defined in: event-lifecycle.ts:57

Called when entering the new state.

ParameterType
evE

void

EventLifecycle.enter


notify(ev: E): void

Defined in: event-lifecycle.ts:61

Notifies subscribers of the change.

ParameterType
evE

void

EventLifecycle.notify


after(ev: E): void

Defined in: event-lifecycle.ts:65

Final hook after transition completes. (Represents afterTransition, not state entry/exit.)

ParameterType
evE

void

EventLifecycle.after


getState(): E["to"] | E["from"]

Defined in: state-machine.ts:31

Returns the current state of the machine (the to property of the last change).

E["to"] | E["from"]


getChange(): E

Defined in: state-machine.ts:35

Returns the last change event, including type, from, and to states.

E


resolveExit(ev: ResolveEvent<E>): undefined | E

Defined in: state-machine.ts:45

Determines the target (“to”) state for a new event. Returns undefined for early exit. Typically looks up transitions using machine.transitions[ev.from.key][ev.type].

ParameterType
evResolveEvent<E>

undefined | E

PropertyTypeDescription
send(type: E["type"], …params: E["params"]) => voidPublic API for sending an event to the machine. Parameters are attached to the event and forwarded through the transition lifecycle as .params.