import type { Actions } from "./actions.js"; import type { DoNotInfer, MachineContext, MetaObject, NonReducibleUnknown, SingleOrArray } from "./common.js"; import type { DoneStateEvent, EventDescriptor, EventObject, ExtractEvent } from "./events.js"; import type { Guard } from "./guards.js"; export type StateNodeType = "atomic" | "compound" | "parallel" | "final" | "history"; export type HistoryType = "shallow" | "deep"; /** A function of `{ context, event }` producing `output` of a final state / the machine. */ export type Mapper = (args: { context: TContext; event: TExpressionEvent; }) => TResult; /** `"sibling"`, `"#id"`, `"#id.child.path"`, `".child"` or several of them (parallel regions only). */ export type TransitionTarget = SingleOrArray; export interface TransitionConfig { target?: TransitionTarget | undefined; actions?: Actions; guard?: Guard; /** Exit and re-enter the source state even when the target is a descendant. @default false */ reenter?: boolean; description?: string; meta?: MetaObject; } /** A target string, a transition object, or an array of those (first enabled wins). */ export type TransitionConfigOrTarget = SingleOrArray>; export type TransitionsConfig = { [K in EventDescriptor]?: TransitionConfigOrTarget, TEvent>; }; /** Keys are milliseconds or named delays resolved through `implementations.delays`. */ export type DelayedTransitions = { [delay: string | number]: string | SingleOrArray> | undefined; }; export interface StateNodeConfig { /** Unique id, referenced by `#id` targets. Defaults to `.`. */ id?: string; /** Inferred from `states` / `history` when omitted. */ type?: StateNodeType; /** Key of the initial child (compound states). */ initial?: string; states?: StatesConfig; on?: TransitionsConfig; /** Eventless transitions, re-evaluated after every microstep. */ always?: TransitionConfigOrTarget; after?: DelayedTransitions; entry?: Actions; exit?: Actions; /** Taken when a final child (compound) / every region (parallel) is reached. */ onDone?: string | SingleOrArray>; /** History pseudo-state kind; `true` means `"shallow"`. */ history?: HistoryType | true; /** Default target of a history state when no history was recorded. */ target?: string; /** Output of a final state (goes into `xstate.done.state.*` and the machine output). */ output?: Mapper | NonReducibleUnknown; tags?: SingleOrArray; description?: string; meta?: MetaObject; } export type StatesConfig = { [key: string]: StateNodeConfig; }; /** * XState v5 typegen-free idiom: `types: { context: {} as Ctx, events: {} as Ev }`. * Ignored at runtime, used only for inference. */ export interface MachineTypes { context?: TContext; events?: TEvent; output?: TOutput; tags?: string; meta?: MetaObject; } export type ContextFactory = () => TContext; type RootStateNodeConfig = Omit, "output" | "history" | "target">; /** * The root config accepted by `createMachine`. `context` is optional only when * `TContext` is the unconstrained `MachineContext`. */ export type MachineConfig = RootStateNodeConfig, DoNotInfer> & { types?: MachineTypes; /** Machine output, resolved from the `xstate.done.state.*` event of the top-level final state. */ output?: Mapper | TOutput; /** * Verbatim text of the `.mmd` file the machine was generated from * (`definition.source`). Not interpreted; the viz renders it instead of * `toMermaid()`, `toXStateSource()` drops it. */ source?: string; } & (MachineContext extends TContext ? { context?: TContext | ContextFactory; } : { context: TContext | ContextFactory; }); export {};