Metakit / API

@doeixd/metakit/machine

Framework-independent declarative state machine (@doeixd/metakit/machine) built over Transitions: one declaration drives runtime dispatch (TEA update / Sync journal) and per-message read/write footprints; intent and affordances ask what a partial state allows (UI and agents).

Opaque transitions (for arbitrary JS) can coexist beside declarative Transitions without breaking types.

Functions

affordancesEvery tag of machine, as available, disabled, or pending (its guard is undecided), using options for custom operation implementations and the rewrite policy.
attachReturn a new Machine with a resolved lifted child merged under prefix (so dispatch, update, and affordances handle ${prefix}${tag} directly without mutating…
compose
diagramThe state diagram of machine, as Mermaid stateDiagram-v2 text: its start (where it has one), every phase, and one line per edge labelled by its message and,…
eachLift every transition of a child machine over a keyed list list: with each => each.sku, each transition's input gains { sku: Child['sku'] }, targeting the…
intentThe intent of arm, taken from a machine's transitions: its message is tagged with the arm's name, and its can checks the guard against the values you give…
lazyChildWrap a dynamic import of a child machine so callers can check isLoaded(), read current(), or await resolve() (deduplicating concurrent loads and clearing…
liftLift every transition of a child machine along path, as Transition.lift does one: each reads and writes under path, and is refused where the state holds no…
makeConstruct a declarative state machine from a Schema and a map of Transitions. Its transitions are arms: copies named by their keys, so one transition given…
phasesThe phases of the field select picks from state (state => state.phase): one reference per literal, or per variant of a union of tagged structs, as phase,…
phaseTableFor each phase of machine, which messages move it and where, and which are refused: each guard is judged knowing only the phase, so a message whose guard…
reachabilityThe phases of machine its start cannot reach, and those it can never leave. It follows edges between phases and reads no guard, so a phase it calls…
startsOfThe states a reader that needs somewhere to begin (reachability, a walk) starts from: from, each checked as Machine.make checks start, else the machine's…
stepObservedRun one step and return the observed state writes when it succeeds.

Interfaces

EachLiftedA child Machine's transitions, each lifted to run on one keyed element of a list (Machine.each).
FromThe transitions that leave the phases L given to from.
IntentAdmission and client message construction for one arm of a machine.
LazyChildA child machine whose code loads on demand.
LazyMachineLikeStructural machine shape accepted as a lazy child (covariant in its transitions).
LiftedA child Machine's transitions, each lifted to run inside Outer (Machine.lift) and named by its tag.
Machine
PhaseOne phase of a machine: a literal of its phase field, or a variant of the tagged union there, named by the value Machine.phases returned (phase.Editing),…
PhaseEdgeOne edge of a phase transition: the phases it leaves, the phase it enters, and when it is taken.
PhaseEdgesWhat a transition built with from records of its phases.
PhaseRowOne phase's row of Machine.phaseTable.
PhasesThe phases of a field and the builder of transitions between them.
PhaseTableFor each phase of a machine, what each message does.
ReachabilityWhat Machine.reachability found.

Types

AlternativeAn alternative of choose from the phases L, taken where when holds and no earlier one did.
AlternativesThe alternatives of choose: conditions in order, and at most one otherwise, last.
ArmOfAny one arm of a machine whose transitions are Transitions.
ArmsA machine's transitions as it holds them, each an arm: a copy of the transition it was given carrying the tag it was registered under as name.
ComposedThe transitions of a composition: its own, and each child's under its name then the child's tag.
EmitOf
EntryWhat a transition from L into N gives besides the phase: its writes, and, for a variant with fields, those fields as with.
HeldWhat a field holding P holds in the phases named N: those literals, or those variants.
InputOfThe input a transition takes.
InputServicesOfThe services a transition's input Schema needs (Transition's InputServices); unknown where none is recorded.
MessageOfTagged message union derived from a Record<Tag, Transition> map. A transition that declares supplied values carries them in its message, so a replay runs it…
ObservedWriteAn observed change under a state path. A keyed field names the domain key, not a list index.
OtherwiseThe last alternative of choose, taken where no earlier one is.
PhaseEnvWhat a transition's callbacks are given: the paths of { state, input }, and variant, the phase field as it is in the phases the transition leaves. Where…
PhaseFieldWhat a phase field may hold: string literals, or tagged structs.
PhaseNameThe name of each phase a field holding P has: a literal, or a variant's tag.
PhaseNameOfThe phase names of a machine's transitions built with from.
PhaseRefsOne reference per phase of a field holding P, keyed by its name.
PhaseTransitionA transition built with from: an ordinary Transition that also carries its edges.
SharedTagsThe tags two transitions of a composition would share; never where each is its own.
SuppliedOfValues the host supplies to a transition.