Core 1.0.0-rc.1
Runtime Queue And Observation Hub
Source file: packages/core/src/journey-machine/runtime.ts
This file is the mutable heart of the machine.
It owns the current snapshot, the subscription sets, the lifecycle event stream, and the serialized execution queue used by sends and navigation helpers.
How It Works
snapshotis kept in closure state.getSnapshot()reads it, andsetSnapshot(...)writes it while optionally notifying listeners and forwarding the change to plugin hooks.queue(...)is the concurrency boundary. Every caller gets chained ontoactionQueue, so each operation runs to a stable result before the next one mutates the machine.- The queue captures a
runVersion. IfcancelInFlight()increments the lifecycle version, older async work can still finish locally, but its writes are ignored becauseisRunActive(runVersion)returnsfalse. subscribe(...)stores raw snapshot listeners.subscribeSelector(...)layers on top of that by caching the selected value and skipping listener calls when the equality function says nothing changed.subscribeEvent(...)manages lifecycle listeners and immediately sends the startup event to new subscribers so observers have a consistent starting point.- Listener failures stay isolated. If
onListenerErroris omitted, the runtime reports them through a development-onlyconsole.error(...)fallback instead of swallowing them completely silently. dispose()marks the runtime as closed, cancels queued work, clears every listener set, and forwards disposal to any external cleanup hook.
This file deliberately does not know how transitions are chosen. It only guarantees safe ordering, observable state, and consistent cancellation.
Recommended Reading
- Read Send Pipeline for the code that runs inside this queue.
- Read Async State for the run-version-aware writes used by async guards.
- Read Lifecycle and Snapshot for the public semantics exposed by this runtime.