Function: defineMachine()
function defineMachine(def: MachineDef): CompiledMachine;Defined in: shared/lifecycle/define-machine.ts:43
Compile and validate a flat lifecycle machine definition.
Validation runs fail-fast IN THIS ORDER (the order matters — wildcard expansion happens first so every later check sees concrete source states):
- Expand
from: "*"to all NON-TERMINAL states. - Every
from/to/initial/terminalreferences a declared state. - No terminal state has an outgoing edge.
- No duplicate
(from, event)pair (determinism). - No
to === fromunless the edge setsallowSelf(NoOpTransition policy). - Warn (do not throw) on unreachable states.
Also computes a normalized content hash (machineVersion) over the sorted
states / events / transitions / guard names so it is reproducible across
deploys and machines compiled in different declaration orders.
From the validated edge list we build an XState v5 machine that is the
runtime authority for the GRAPH: resolve/eventsFrom consult it for
legality and targets (setup().createMachine() + transition(), the same
pattern the bookings composer uses). XState is passed NO guards and NO actions
— it never evaluates a guard or runs an effect. The engine-owned metadata
XState can't hold (guards / effects / allowSelf) lives in a sidecar Map
keyed by ${from} ${event}; resolve merges XState's to with that
metadata. Compile-time validation stays here (XState would silently merge
duplicate/self edges).
Parameters
| Parameter | Type |
|---|---|
def | MachineDef |
Returns
Throws
with a LIFECYCLE_DEFINE_* prefix on any structural error.