Skip to content

State machine types

Read as Markdown

Event object with a string type discriminator.

Maps event discriminators to typed transition handlers. Each handler receives its narrowed event.

Actor lifecycle: not-started, active, stopped, or error. A game-over application state can remain active.

Readonly state value, context, lifecycle status, error, and matches(state) predicate, published as one signal value.

Synchronous cleanup function for callback work. Called once when its state exits or its actor stops.

Context, initiating event, immutable actor input, and queued send function supplied to synchronous actions.

Synchronous effect used in entry, exit, or a transition. Use assign for context updates.

One action or a readonly array of actions executed in order.

Entry context, initiating event (undefined initially), actor input, abort signal, and send function supplied to owned work.

Cancelable state-owned function returning a cleanup, a promise, or no result. Promises select completion/error routes; cleanup functions remain owned until exit.

Internal completion event carrying the task output. Observed by its invocation’s onDone handler.

Internal failure event carrying the task error. Observed by its invocation’s onError handler.

Task with optional diagnostic id and onDone/onError transitions. A state may declare one or several invocations.

Optional target, guard, ordered actions, and explicit same-state reentry flag.

A destination name, configured transition, or ordered list of guarded alternatives.

Flat state with optional entry/exit actions, owned invocations, and local event routes.

Reusable initial state, context factory/value, state table, and machine-level fallback routes.

Input captured by the actor at creation. Use { input: undefined } for machines without input.

Execution handle with a readonly snapshot signal and start, send, and terminal idempotent stop methods.

Optional existing playback conflict policy and diagnostic name, imported from the animation adapter entry.

Builds one animation program per state entry from task context, imported from the animation adapter entry.

Options used to create an explicitly owned actor.

interface CreateMachineOptions<Input>

Related API: CreateMachineOptions.

MachineActor

View source — packages/core/src/features/machines/types.ts:172

input
readonly input: Input

Immutable application input available to context factories, actions, and tasks.

View source — packages/core/src/features/machines/types.ts:174

A synchronous effect run during entry, exit, or a transition.

type MachineAction<Context, Event extends MachineEvent | undefined, Input, SentEvent extends MachineEvent = MachineEvent> = {
// Method bivariance lets a reusable event-specific action (such as assign()) run on an initial
// entry as long as that action does not inspect an absent event.
/** Executes one synchronous effect.
* @param context - Current transition values.
*/
action(context: MachineActionContext<Context, Event, Input, SentEvent>): void;
}['action']

Related API: MachineAction, MachineEvent, assign, MachineActionContext.

MachineActionContext

View source — packages/core/src/features/machines/types.ts:57

Values given to state actions. Context reflects all preceding assign actions.

interface MachineActionContext<Context, Event extends MachineEvent | undefined, Input, SentEvent extends MachineEvent = MachineEvent>

Related API: MachineActionContext, MachineEvent.

MachineAction

View source — packages/core/src/features/machines/types.ts:43

context
readonly context: Readonly<Context>

Context as it exists at this point in the transition.

View source — packages/core/src/features/machines/types.ts:45

event
readonly event: Event

Event which selected this transition.

View source — packages/core/src/features/machines/types.ts:47

input
readonly input: Input

Immutable actor input supplied at creation.

View source — packages/core/src/features/machines/types.ts:49

send
send: (event: SentEvent) => void

Queues an event after the current transition has completed.

  • event — Event to process after the active transition.

View source — packages/core/src/features/machines/types.ts:53

One action or actions run in source order.

type MachineActions<Context, Event extends MachineEvent | undefined, Input, SentEvent extends MachineEvent = MachineEvent> = | MachineAction<Context, Event, Input, SentEvent>
| readonly MachineAction<Context, Event, Input, SentEvent>[]

Related API: MachineActions, MachineEvent, MachineAction.

MachineAction

View source — packages/core/src/features/machines/types.ts:67

Explicitly owned machine execution. Call start before sending events and stop to release it.

interface MachineActor<Context, Event extends MachineEvent, _Input, State extends string = string>

Related API: MachineActor, MachineEvent.

MachineDefinition

View source — packages/core/src/features/machines/types.ts:178

snapshot
readonly snapshot: Signal<MachineSnapshot<Context, State>>

Related API: Signal, MachineSnapshot.

Readonly, synchronous signal for the current state and context.

View source — packages/core/src/features/machines/types.ts:180

start
start: () => void

Starts the initial state once. Starting an active or stopped actor does nothing.

View source — packages/core/src/features/machines/types.ts:182

send
send: (event: Event) => void

Queues an event. Events sent during actions run after the current transition.

  • event — Event accepted by this actor.

View source — packages/core/src/features/machines/types.ts:186

stop
stop: () => void

Terminal, idempotent cleanup. Cancels every task owned by the active state.

View source — packages/core/src/features/machines/types.ts:188

Declarative, reusable machine definition. Definitions create no actor or scheduled work.

interface MachineDefinition<Context, Event extends MachineEvent, Input = undefined, State extends string = string>

Related API: MachineDefinition, MachineEvent.

MachineActor

View source — packages/core/src/features/machines/types.ts:157

initial
readonly initial: State

State entered when an actor starts.

View source — packages/core/src/features/machines/types.ts:159

context
readonly context: Context | ((context: Readonly<{ input: Input; }>) => Context)

Initial context or a factory evaluated once for every actor.

  • context — Input used to create this actor’s context.

Initial context for the new actor.

View source — packages/core/src/features/machines/types.ts:164

states
readonly states: Readonly<Record<State, MachineState<Context, Event, Input, State>>>

Related API: MachineState.

State table. This initial implementation supports flat states only.

View source — packages/core/src/features/machines/types.ts:166

on (optional)
readonly on?: MachineEventRoutes<Context, Event, Input, State> | undefined

Related API: MachineEventRoutes.

Fallback routes for events not declared by the active state.

View source — packages/core/src/features/machines/types.ts:168

An event accepted by a machine. The type selects the active state’s handler.

interface MachineEvent

Related API: MachineEvent.

MachineDefinition

View source — packages/core/src/features/machines/types.ts:4

type
readonly type: string

Stable event discriminator.

View source — packages/core/src/features/machines/types.ts:6

Maps each event discriminator to its narrowed event shape.

type MachineEventRoutes<Context, Event extends MachineEvent, Input, State extends string> = {
/** Handler selected for the corresponding event discriminator. */
[type in Event['type']]?: MachineTransition<Context, Extract<Event, { /** Discriminator used to select this event variant. */ readonly type: type }>, Input, State, Event>;
}

Related API: MachineEventRoutes, MachineEvent, MachineTransition.

MachineTransition

View source — packages/core/src/features/machines/types.ts:14

State-owned task and optional completion transitions.

interface MachineInvocation<Context, Event extends MachineEvent, Input, State extends string>

Related API: MachineInvocation, MachineEvent.

MachineTask

View source — packages/core/src/features/machines/types.ts:112

id (optional)
readonly id?: string | undefined

Optional diagnostic identity; IDs are local to one state entry.

View source — packages/core/src/features/machines/types.ts:114

task
readonly task: MachineTask<Context, Event, Input, unknown>

Related API: MachineTask.

Work started after this state’s snapshot publishes.

View source — packages/core/src/features/machines/types.ts:116

onDone (optional)
readonly onDone?: MachineTransition<Context, MachineTaskDoneEvent<unknown>, Input, State, Event> | undefined

Related API: MachineTransition, MachineTaskDoneEvent.

Transition selected if a promise resolves before cancellation.

View source — packages/core/src/features/machines/types.ts:118

onError (optional)
readonly onError?: MachineTransition<Context, MachineTaskErrorEvent, Input, State, Event> | undefined

Related API: MachineTransition, MachineTaskErrorEvent.

Transition selected if a task throws or a promise rejects before cancellation.

View source — packages/core/src/features/machines/types.ts:120

Immutable state published by a running machine actor.

interface MachineSnapshot<Context, State extends string = string>

Related API: MachineSnapshot.

MachineActor

View source — packages/core/src/features/machines/types.ts:23

value
readonly value: State

Active flat state name.

View source — packages/core/src/features/machines/types.ts:25

context
readonly context: Readonly<Context>

Application-owned immutable context for the active state.

View source — packages/core/src/features/machines/types.ts:27

status
readonly status: MachineStatus

Related API: MachineStatus.

Whether the actor may receive events.

View source — packages/core/src/features/machines/types.ts:29

error
readonly error: unknown

Unexpected failure that stopped this actor, if any.

View source — packages/core/src/features/machines/types.ts:31

matches
matches: (state: State) => boolean

Tests whether this flat machine currently occupies state.

  • state — State name to compare with the active value.

True when this is the active state.

View source — packages/core/src/features/machines/types.ts:36

One flat state definition. Local event handlers shadow machine-level handlers.

interface MachineState<Context, Event extends MachineEvent, Input, State extends string>

Related API: MachineState, MachineEvent.

MachineDefinition

View source — packages/core/src/features/machines/types.ts:145

entry (optional)
readonly entry?: MachineActions<Context, Event | undefined, Input, Event> | undefined

Related API: MachineActions.

Effects run whenever this state is entered. The initial entry receives undefined as event.

View source — packages/core/src/features/machines/types.ts:147

exit (optional)
readonly exit?: MachineActions<Context, Event, Input, Event> | undefined

Related API: MachineActions.

Effects run whenever this state is exited.

View source — packages/core/src/features/machines/types.ts:149

invoke (optional)
readonly invoke?: MachineInvocation<Context, Event, Input, State> | readonly MachineInvocation<Context, Event, Input, State>[] | undefined

Related API: MachineInvocation.

State-owned work, canceled before exit effects.

View source — packages/core/src/features/machines/types.ts:151

on (optional)
readonly on?: MachineEventRoutes<Context, Event, Input, State> | undefined

Related API: MachineEventRoutes.

Event routes local to this state. Declaring an event consumes machine-level handling.

View source — packages/core/src/features/machines/types.ts:153

Lifecycle state of a machine actor.

type MachineStatus = 'not-started' | 'active' | 'stopped' | 'error'

Related API: MachineStatus.

MachineActor

View source — packages/core/src/features/machines/types.ts:20

Cancelable work owned by a state entry. Return a cleanup for subscriptions or a promise for finite work. A task is never started until its entry snapshot has been published.

type MachineTask<Context, Event extends MachineEvent, Input, Output = unknown> = (
context: MachineTaskContext<Context, Event, Input>,
) => void | MachineTaskCleanup | Promise<Output>

Related API: MachineTask, MachineEvent, MachineTaskContext, MachineTaskCleanup.

  • context — State-entry values and cancellation signal.

Optional cleanup or a promise for finite work.

MachineInvocation

View source — packages/core/src/features/machines/types.ts:94

Cleanup called when a callback task’s owning state exits or its actor stops.

type MachineTaskCleanup = () => void

Related API: MachineTaskCleanup.

MachineTask

View source — packages/core/src/features/machines/types.ts:40

Values supplied to a state-owned task when that state is entered.

interface MachineTaskContext<Context, Event extends MachineEvent, Input>

Related API: MachineTaskContext, MachineEvent.

MachineTask

View source — packages/core/src/features/machines/types.ts:72

context
readonly context: Readonly<Context>

Context captured when this particular task starts.

View source — packages/core/src/features/machines/types.ts:74

event
readonly event: Event | undefined

Event that entered this state, or undefined for the initial state.

View source — packages/core/src/features/machines/types.ts:76

input
readonly input: Input

Immutable actor input supplied at creation.

View source — packages/core/src/features/machines/types.ts:78

signal
readonly signal: AbortSignal

Related API: signal.

Aborted synchronously when the owning state exits.

View source — packages/core/src/features/machines/types.ts:80

send
send: (event: Event) => void

Queues an event after the current transition, without re-entering the actor.

  • event — Event accepted by the owning actor.

View source — packages/core/src/features/machines/types.ts:84

Completion data supplied to an invocation’s onDone transition.

interface MachineTaskDoneEvent<Output = unknown> extends MachineEvent

Related API: MachineTaskDoneEvent, MachineEvent.

MachineInvocation

View source — packages/core/src/features/machines/types.ts:99

type
readonly type: "@pibbl/machine.done"

Related API: pibbl.

Internal completion discriminator.

View source — packages/core/src/features/machines/types.ts:100

output
readonly output: Output

Value resolved by the task promise.

View source — packages/core/src/features/machines/types.ts:101

Failure data supplied to an invocation’s onError transition.

interface MachineTaskErrorEvent extends MachineEvent

Related API: MachineTaskErrorEvent, MachineEvent.

MachineInvocation

View source — packages/core/src/features/machines/types.ts:105

type
readonly type: "@pibbl/machine.error"

Related API: pibbl.

Internal failure discriminator.

View source — packages/core/src/features/machines/types.ts:106

error
readonly error: unknown

Thrown or rejected task value.

View source — packages/core/src/features/machines/types.ts:107

Shorthand state target, a configured route, or ordered guarded alternatives.

type MachineTransition<Context, Event extends MachineEvent, Input, State extends string, SentEvent extends MachineEvent = Event> = | State
| MachineTransitionConfig<Context, Event, Input, State, SentEvent>
| readonly MachineTransitionConfig<Context, Event, Input, State, SentEvent>[]

Related API: MachineTransition, MachineEvent, MachineTransitionConfig.

MachineTransitionConfig

View source — packages/core/src/features/machines/types.ts:139

A conditional route considered in list order.

interface MachineTransitionConfig<Context, Event extends MachineEvent, Input, State extends string, SentEvent extends MachineEvent = Event>

Related API: MachineTransitionConfig, MachineEvent.

MachineTransition

View source — packages/core/src/features/machines/types.ts:124

target (optional)
readonly target?: State | undefined

Destination state. Omit to run actions without leaving the current state.

View source — packages/core/src/features/machines/types.ts:126

guard (optional)
readonly guard?: ((context: MachineActionContext<Context, Event, Input, SentEvent>) => boolean) | undefined

Related API: MachineActionContext.

Only select this route when it returns true.

  • context — Current transition values.

Whether this route may be selected.

View source — packages/core/src/features/machines/types.ts:131

actions (optional)
readonly actions?: MachineActions<Context, Event, Input, SentEvent> | undefined

Related API: MachineActions.

Effects run after exit and before entry.

View source — packages/core/src/features/machines/types.ts:133

reenter (optional)
readonly reenter?: boolean | undefined

Restarts entry work when targeting the already active state.

View source — packages/core/src/features/machines/types.ts:135

Playback options owned by a machine animation task.

Lifecycle callbacks are deliberately omitted: task completion and failure become the invocation’s onDone and onError transitions instead.

interface MachineAnimationOptions

Related API: MachineAnimationOptions.

playAnimation

View source — packages/core/src/features/machines/animation.ts:23

conflict (optional)
readonly conflict?: "error" | "replace" | undefined

Selects the existing writer-conflict policy for this state-owned playback. Defaults to the animation runtime’s explicit-error policy.

View source — packages/core/src/features/machines/animation.ts:28

debugName (optional)
readonly debugName?: string | undefined

Optional label included in animation scheduler diagnostics.

View source — packages/core/src/features/machines/animation.ts:30

Builds the animation program for one state entry.

The callback runs once when the invocation starts. It receives the entry’s immutable context and event, the machine input, the cancellation signal, and send for application-defined machine events.

type MachineAnimationProgramFactory<Context, Event extends MachineEvent, Input> = (
context: MachineTaskContext<Context, Event, Input>,
) => PibblAnimationProgram

Related API: MachineAnimationProgramFactory, MachineEvent, MachineTaskContext, PibblAnimationProgram.

  • Context — Context captured when this state entry began.

  • Event — Event type accepted by the owning machine.

  • Input — Input captured by the owning machine actor.

  • context — Immutable state-entry values used to create this program.

The program that the machine invocation owns until it finishes or is cancelled.

MachineTaskContext

View source — packages/core/src/features/machines/animation.ts:47

Read the Authoring, signals, and lifecycle companion for ownership, adaptation, failure modes, and verification. Agent start provides the version-selection workflow.

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.