import { SYMBOL_DISPOSE } from "../signals/base/disposeSymbol.js"; import type { ReadonlySignal } from "../signals/types/index.js"; import { type MachineDefinition } from "./MachineDefinition.js"; import type { EventObject, MachineContext, MachineSnapshot, StatechartOptionsOrKey, StatechartStatus, StateValue } from "./types/index.js"; /** * Runtime engine of one machine instance: snapshot signal (`State` with Redux * DevTools wiring), event queue, timers (`after` / delayed `raise`), Stately * Inspector wiring and error handling. The pure step algorithm lives in * `core/interpreter.ts`; this class only sequences it. Spec: section 4. * * Prefer the facade `unstable_MachineSignal.state(definition, options)`; use * this class directly for advanced composition. */ export declare class unstable_Statechart implements Disposable { readonly definition: MachineDefinition; /** Tracked/untracked reads of the current snapshot; pushes once per macrostep. */ readonly state: ReadonlySignal>; /** Unique per instance (`"sc:"`); used as the inspector session id. */ readonly sessionId: string; private readonly _model; private readonly _scope; private readonly _options; private readonly _state$; /** Allocated only for keyless instances; released by `dispose()` / the finalizer. */ private readonly _keySlot; /** Best-effort: set to `null` after the adapter threw once (`_notifyInspector`). */ private _actorHandle; private _machineState; private _snapshot; private _status; /** Re-entrancy guard: true while a burst (a macrostep or `start()`, plus everything it triggers) runs; nested sends are queued. */ private _processing; private _disposeRequested; private _restartRequested; /** Error raised inside the guarded section, reported once the batch has finished. */ private _failure; /** Events sent before `start()` and re-entrant sends (FIFO). */ private readonly _queue; /** Executor calls captured while not running (initial actions), flushed by `start()`. */ private readonly _deferred; private readonly _timers; private _timerSequence; /** `options` may be just the Redux DevTools key, like `Signal.state(value, "key")`. */ constructor(definition: MachineDefinition, options?: StatechartOptionsOrKey); /** `"idle"` before `start()`, `"running"`, `"stopped"` after `stop()` / done / error, `"disposed"`. */ get status(): StatechartStatus; /** * Commits the initial snapshot and flushes its deferred effects (or * re-initializes a stopped/done/errored machine). Moves straight to * `stopped` when the initial snapshot is already done/error. Throws after * `dispose()`. Called from inside a burst (an action, a synchronous * subscriber or an effect reacting to the done / error / stop snapshot) * the restart is deferred until the burst has finished. */ start(): void; /** * Processes `xstate.stop`, cancels timers and drops queued events. No-op * unless running; called from inside a burst (an action, a subscriber or * an effect) it is deferred to the end of the current macrostep (XState * behaviour). */ stop(): void; /** * Synchronous: a full macrostep inside `Batcher.run`, one snapshot push. * Re-entrant calls (from actions, subscribers and effects) and events sent * before `start()` are queued; events after done/error/stop/dispose are * ignored. */ send(event: TEvent): void; matches(stateValue: StateValue): boolean; /** Pure query on the current snapshot (XState `snapshot.can`), independent of the engine status; `false` once disposed. */ can(event: TEvent): boolean; /** Alias of `state.peek()`. */ getSnapshot(): MachineSnapshot; /** * Stops if running, completes the signal (drops the DevTools entry), * releases the inspector actor. Idempotent; called from inside a burst it * finishes after the current macrostep. */ dispose(): void; [SYMBOL_DISPOSE](): void; /** * Runs the initial macrostep with the executor in deferred mode. A throw * (builtin at init) yields an error state built from the pre-initial * state; the caller decides where to report it. */ private _computeInitialState; /** * The state before the initial transition, used as the base of an * init-time error snapshot. A `context` factory is not invoked again (it * may be the very thing that threw, and it may have side effects); an * object context is the shared object anyway. */ private _createPreInitialState; private _createErrorState; /** * The single re-entrancy-guarded section: `Batcher.run` around the burst * (a macrostep plus every re-entrant send), `processing` set so that * nested `send()` / `stop()` / `dispose()` / `start()` calls are queued or * deferred, deferred teardown / restart in the tail, and the failure (if * any) reported only after the batch has flushed — throwing out of * `Batcher.run` would drop the scheduled derived updates. * * `Batcher.run` flushes the scheduled Computed/Effect work after the body * returned; an effect reacting to the new snapshot may itself `send()` * (queued, because the guard is still set). Those events are drained in * further rounds — each one a `Batcher.run` of its own, so its effects are * flushed too — until nothing new arrives. */ private _runGuarded; /** Entry point of every event while running: direct processing or FIFO when re-entrant. */ private _receive; private _drain; /** One macrostep: inspector event, `step`, halt when finished, commit (when changed), inspector snapshot. */ private _process; /** Keeps `_snapshot` in sync with `_machineState`: a new snapshot object only when the state changed. */ private _setMachineState; /** * Pushes the current snapshot (actionName = event type) and notifies the * inspector. `State.set` dedupes by identity, so an unchanged macrostep * (and the initial commit of `start()`) costs no emission; the inspector * is told about every macrostep regardless. */ private _commit; /** Error policy (spec 4.5): error snapshot on top of the last good state, halt, report after the batch. */ private _fail; /** Constructor-time init failure: the error snapshot is already the initial value; no macrostep to commit. */ private _haltAfterFailure; /** The machine is finished (done / error / stopped): no timers, no pending events, engine stopped. */ private _halt; private _report; private _finishDispose; private _createActorHandle; /** * The inspector is best-effort: an adapter that throws must break neither * the engine's invariants (halt, queue, timers) nor the batch. The first * failure is logged and disables the handle for this instance. */ private _notifyInspector; private _createExecutor; /** Deferred while not running (initial macrostep), synchronous inside a running macrostep. */ private _effect; private _scheduleTimer; private _cancelTimer; private _cancelAllTimers; /** Releases the inspector actor and the default key slot of an engine that was garbage-collected without `dispose()`. */ private static _finalizationRegistry; /** Callable read-only view of the internal `State`: never exposes `set` / `update`. */ private static _createReadonlySignal; }