import isDevelopment from '#is-development'; import { xstateError } from './xstateError.ts'; import { NULL_EVENT, STATE_DELIMITER } from './constants.ts'; import type { SetupStateSchemas } from './schema.types.ts'; import { memo } from './memo.ts'; import { evaluateCandidate, formatTransition, getCandidates, getDelayedTransitions, matchesActorSession, type TransitionSelectionResults } from './stateUtils.ts'; import type { DelayedTransitionDefinition, EventObject, TimerKey, InitialTransitionDefinition, MachineContext, Mapper, TransitionDefinition, TransitionDefinitionMap, AnyStateMachine, AnyStateNodeConfig, EventDescriptor, AnyActorScope, AnyStateNode, AnyEventObject, AnyAction, AnyTransitionConfig, AnyTransitionDefinition, AnyMachineSnapshot, AnyInvokeDefinition, InvokeDefinition, MetaObject } from './types.ts'; import type { NonReducibleUnknown } from './typeUtils.ts'; import { createInvokeId, hasUnescapedDot, mapValues, toArray, toTransitionConfigArray } from './utils.ts'; const EMPTY_OBJECT = {}; const CHOICE_CONFIG_KEYS = [ 'invoke', 'after', 'on', 'entry', 'exit', 'always', 'states', 'initial', 'onDone', 'timeout', 'onTimeout', 'history', 'target', 'output' ] as const; interface StateNodeOptions< TStateMeta extends MetaObject, TTransitionMeta extends MetaObject > { _key: string; _parent?: StateNode; _machine: AnyStateMachine; } /** @public */ export class StateNode< TContext extends MachineContext = MachineContext, TEvent extends EventObject = EventObject, TStateMeta extends MetaObject = any, TTransitionMeta extends MetaObject = TStateMeta > { /** * The relative key of the state node, which represents its location in the * overall state value. */ public key: string; /** The unique ID of the state node. */ public id: string; /** * The type of this state node: * * - `'atomic'` - no child state nodes * - `'compound'` - nested child state nodes (XOR) * - `'parallel'` - orthogonal nested child state nodes (AND) * - `'history'` - history state node * - `'choice'` - pure routing decision state node * - `'final'` - final state node */ public type: | 'atomic' | 'compound' | 'parallel' | 'final' | 'history' | 'choice'; /** The string path from the root machine node to this node. */ public path: string[]; /** The child state nodes. */ public states: Record< string, StateNode >; /** * The type of history on this state node. Can be: * * - `'shallow'` - recalls only top-level historical state value * - `'deep'` - recalls historical state value at all levels */ public history: false | 'shallow' | 'deep'; /** The action(s) to be executed upon entering the state node. */ public entry: AnyAction | undefined; /** The action(s) to be executed upon exiting the state node. */ public exit: AnyAction | undefined; /** The parent state node. */ public parent?: StateNode; /** The root machine node. */ public machine: AnyStateMachine; /** * The meta data associated with this state node, which will be returned in * State instances. */ public meta?: TStateMeta; /** * The output data sent with the `xstate.done.state` event if this is a final * state node. */ public output?: | Mapper | NonReducibleUnknown; /** * The order this state node appears. Corresponds to the implicit document * order. */ public order: number = -1; public description?: string; public schemas: SetupStateSchemas | undefined; public tags: string[] = []; public transitions!: Map< string, TransitionDefinition[] >; public always?: Array>; public invoke: Array< InvokeDefinition >; public on!: TransitionDefinitionMap; public after!: Array>; public events!: Array>; public ownEvents!: Array>; private _candidateCache?: Map; constructor( /** The raw config used to create the machine. */ public config: AnyStateNodeConfig, options: StateNodeOptions ) { this.parent = options._parent; this.key = options._key; this.machine = options._machine; if (this.parent && hasUnescapedDot(this.key)) { throw new Error( isDevelopment ? // The suggestion is written as a JS string literal. `State key "${this.key}" contains ".". Nest the states or escape it as "${this.key.replaceAll(/(? { const stateNode = new StateNode< any, any, TStateMeta, TTransitionMeta >(stateConfig, { _parent: this, _key: key, _machine: this.machine }); return stateNode; } ) : EMPTY_OBJECT ) as typeof this.states; if (this.type === 'compound' && !this.config.initial) { throw new Error( isDevelopment ? `No initial state specified for compound state node "#${ this.id }". Try adding { initial: "${firstStateKey}" } to the state config.` : xstateError(59, this.id) ); } // History config this.history = this.config.history === true ? 'shallow' : this.config.history || false; this.entry = this.config.entry as AnyAction | undefined; this.exit = this.config.exit as AnyAction | undefined; this.meta = this.config.meta; this.output = this.type === 'final' || !this.parent ? this.config.output : undefined; this.tags = toArray(config.tags).slice(); this.invoke = toArray(this.config.invoke).map((invokeConfig, i) => { const { src, registryKey } = invokeConfig; const invokeId = createInvokeId(this.id, i); const resolvedId = invokeConfig.id ?? invokeId; return { ...invokeConfig, // Registered (string) sources persist by key; inline logic has no // source key and restores through this invoke definition's id. src: typeof src === 'string' ? src : undefined, logic: src, id: resolvedId, registryKey } as AnyInvokeDefinition; }) as typeof this.invoke; } /** @internal */ public _initialize() { this.after = getDelayedTransitions(this) as any; this.transitions = formatTransitions(this) as typeof this.transitions; if (this.type === 'choice') { this.always = formatChoiceTransitions(this) as typeof this.always; } else if (this.config.always) { this.always = mapTransitionConfigs( this, 'always', this.config.always, (transition) => formatTransition(this, NULL_EVENT, transition) ) as typeof this.always; } for (const key of Object.keys(this.states)) { this.states[key]._initialize(); } this._refreshEventMetadata(); } /** @internal */ public _refreshEventMetadata() { const on = {} as TransitionDefinitionMap; const ownEvents: EventDescriptor[] = []; for (const [descriptor, transitions] of this.transitions) { (on as any)[descriptor] = transitions.slice(); if ( transitions.some( (transition) => transition.target || transition.reenter || transition.to ) ) { ownEvents.push(descriptor); } } this.on = on; this.ownEvents = ownEvents; const events = new Set>(ownEvents); for (const state of Object.values(this.states)) { for (const event of state.events) { events.add(event); } } this.events = Array.from(events); } public get initial(): InitialTransitionDefinition { return memo(this, 'initial', () => formatInitialTransition(this, this.config.initial) ) as InitialTransitionDefinition; } /** @internal */ public next( snapshot: AnyMachineSnapshot, event: AnyEventObject, actorScope: AnyActorScope, selectionResults?: TransitionSelectionResults ): Array | undefined { // Final states are inert: as in SCXML, they take no transitions. if (this.type === 'final') { return undefined; } let candidates = this._candidateCache?.get(event.type); if (!candidates) { candidates = getCandidates(this, event); (this._candidateCache ??= new Map()).set(event.type, candidates); } for (const candidate of candidates) { const guardPassed = evaluateCandidate( candidate, event, snapshot, this, actorScope, selectionResults ); if (guardPassed) { return [candidate]; } } return undefined; } } function validateStateNodeConfig(stateNode: AnyStateNode) { const config = stateNode.config as any; if ( stateNode.type === 'history' && !( (typeof config.target === 'string' && config.target.trim().length > 0) || (Array.isArray(config.target) && config.target.length > 0 && config.target.every( (target: unknown) => typeof target === 'string' && target.trim().length > 0 )) ) ) { throw new Error( isDevelopment ? `History state "${stateNode.id}" must declare a non-empty \`target\`.` : xstateError(60, stateNode.id) ); } if (stateNode.type !== 'choice') { if (isDevelopment && config.choice !== undefined) { throw new Error( `State "${stateNode.id}" has \`choice\`, but \`choice\` can only be used with \`type: 'choice'\`.` ); } return; } if (typeof config.choice !== 'function') { throw new Error( isDevelopment ? `Choice state "${stateNode.id}" must declare a \`choice\` function.` : xstateError(61, stateNode.id) ); } if (isDevelopment) { for (const key of CHOICE_CONFIG_KEYS) { if (config[key] !== undefined) { throw new Error( `Choice state "${stateNode.id}" cannot declare \`${key}\`.` ); } } } } const FINAL_STATE_IGNORED_KEYS = ['invoke', 'on', 'after'] as const; /** * Final states are inert: their `invoke`, `on` and `after` never run. Warn * instead of silently ignoring them. */ function warnOnFinalStateBehavior(stateNode: AnyStateNode): void { if (stateNode.type !== 'final') { return; } const config = stateNode.config as Record; const declared = FINAL_STATE_IGNORED_KEYS.filter( (key) => config[key] !== undefined ); if (declared.length) { console.warn( `State "${stateNode.id}" is final and declares ${declared .map((key) => `"${key}"`) .join(', ')}; final states cannot run actors or take transitions.` ); } } function formatChoiceTransitions( stateNode: AnyStateNode ): AnyTransitionDefinition[] { const choice = (stateNode.config as any).choice; const validateChoiceResult = (result: any): AnyTransitionConfig => { if (!result || result.target === undefined) { throw new Error( isDevelopment ? `Choice state "${stateNode.id}" must resolve to a target.` : xstateError(62, stateNode.id) ); } if (isDevelopment) { for (const key of ['actions', 'to'] as const) { if (result[key] !== undefined) { throw new Error( `Choice state "${stateNode.id}" cannot declare \`${key}\` on a choice.` ); } } } return result; }; return [ formatTransition(stateNode, NULL_EVENT, { to: (args: any) => validateChoiceResult(choice(args)) } as AnyTransitionConfig) ]; } function mapTransitionConfigs( stateNode: AnyStateNode, name: string, transitionsConfig: unknown, mapper: (transition: AnyTransitionConfig) => T ): T[] { const transitionConfigs = toTransitionConfigArray( transitionsConfig as any, name, stateNode.id ); const transitions = new Array(transitionConfigs.length); for (let i = 0; i < transitionConfigs.length; i++) { transitions[i] = mapper(transitionConfigs[i]); } return transitions; } function formatTransitions< TContext extends MachineContext, TEvent extends EventObject >( stateNode: AnyStateNode ): Map[]> { const transitions = new Map< string, TransitionDefinition[] >(); const addTransitions = ( descriptor: string, additions: AnyTransitionDefinition[] ) => { transitions.set(descriptor, [ ...(transitions.get(descriptor) ?? []), ...additions ]); }; if (stateNode.config.on) { for (const descriptor of Object.keys(stateNode.config.on)) { if (descriptor === NULL_EVENT) { throw new Error( isDevelopment ? 'Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.' : xstateError(63, stateNode.id) ); } const transitionsConfig = stateNode.config.on[descriptor]; transitions.set( descriptor, mapTransitionConfigs( stateNode, descriptor, transitionsConfig, (transition) => formatTransition(stateNode, descriptor, transition) ) ); } } if (stateNode.config.onDone) { const descriptor = 'xstate.done.state'; addTransitions( descriptor, mapTransitionConfigs( stateNode, 'onDone', stateNode.config.onDone, (transition) => formatTransition(stateNode, descriptor, { ...transition, matches: { ...transition.matches, stateId: stateNode.id } }) ) ); } if (stateNode.config.onError) { const descriptor = 'xstate.error.*'; transitions.set( descriptor, mapTransitionConfigs( stateNode, 'onError', stateNode.config.onError, (transition) => formatTransition(stateNode, descriptor, transition) ) ); } const createCancelInvokeTimeoutTransition = ( descriptor: string, timeoutKey: TimerKey, actorId: string ): AnyTransitionDefinition => formatTransition(stateNode, descriptor, { matches: { actorId }, _eventMatcher: (event: EventObject, snapshot: AnyMachineSnapshot) => matchesActorSession(event, snapshot, actorId), to: (_args: any, enq: any) => { enq.cancel(timeoutKey); return {}; } } as AnyTransitionConfig); const formatInvokeCompletionTransition = ( descriptor: string, transitionConfig: AnyTransitionConfig, timeoutKey: TimerKey ): AnyTransitionDefinition => { const { target, to, reenter, ...rest } = transitionConfig; return formatTransition(stateNode, descriptor, { ...rest, reenter, to: (args: any, enq: any) => { if (to) { let didEnqueue = false; const trackingEnqueue = new Proxy(enq, { apply(target, thisArg, argArray) { didEnqueue = true; return Reflect.apply(target, thisArg, argArray); }, get(target, prop, receiver) { const value = Reflect.get(target, prop, receiver); if (typeof value !== 'function') { return value; } return (...argArray: any[]) => { didEnqueue = true; return value.apply(target, argArray); }; } }); const result = to(args, trackingEnqueue); if (result !== undefined || didEnqueue) { enq.cancel(timeoutKey); } return result; } enq.cancel(timeoutKey); return { target, reenter }; } } as AnyTransitionConfig); }; for (const invokeDef of stateNode.invoke) { const withInvokeMatch = (transition: AnyTransitionConfig) => ({ ...transition, matches: { ...transition.matches, actorId: invokeDef.id }, _eventMatcher: (event: EventObject, snapshot: AnyMachineSnapshot) => matchesActorSession(event, snapshot, invokeDef.id) }); // Built-in timers have no id; the invoke timeout is keyed by its event. const invokeTimeoutKey: TimerKey | undefined = invokeDef.timeout !== undefined ? { event: { type: 'xstate.timeout.actor', actorId: invokeDef.id } as EventObject } : undefined; if (invokeDef.onDone) { const descriptor = 'xstate.done.actor'; const invokeDoneTransitions = mapTransitionConfigs( stateNode, 'invoke.onDone', invokeDef.onDone, (rawTransition) => { const transition = withInvokeMatch(rawTransition); return invokeTimeoutKey ? formatInvokeCompletionTransition( descriptor, transition, invokeTimeoutKey ) : formatTransition(stateNode, descriptor, transition); } ); if (invokeTimeoutKey) { invokeDoneTransitions.push( createCancelInvokeTimeoutTransition( descriptor, invokeTimeoutKey, invokeDef.id ) ); } addTransitions(descriptor, invokeDoneTransitions); } else if (invokeTimeoutKey) { const descriptor = 'xstate.done.actor'; addTransitions(descriptor, [ createCancelInvokeTimeoutTransition( descriptor, invokeTimeoutKey, invokeDef.id ) ]); } if (invokeDef.onError) { const descriptor = 'xstate.error.actor'; addTransitions( descriptor, mapTransitionConfigs( stateNode, 'invoke.onError', invokeDef.onError, (rawTransition) => { const transition = withInvokeMatch(rawTransition); return invokeTimeoutKey ? formatInvokeCompletionTransition( descriptor, transition, invokeTimeoutKey ) : formatTransition(stateNode, descriptor, transition); } ) ); } if (invokeDef.onSnapshot) { const descriptor = 'xstate.snapshot.actor'; addTransitions( descriptor, mapTransitionConfigs( stateNode, 'invoke.onSnapshot', invokeDef.onSnapshot, (transition) => formatTransition(stateNode, descriptor, withInvokeMatch(transition)) ) ); } } for (const delayedTransition of stateNode.after) { let existing = transitions.get(delayedTransition.eventType); if (!existing) { existing = []; transitions.set(delayedTransition.eventType, existing); } existing.push( delayedTransition as TransitionDefinition ); } return transitions as Map[]>; } function formatInitialTransition( stateNode: AnyStateNode, _target: | string | { target: string | string[]; input?: any; to?: (...args: any[]) => any; meta?: any; description?: string; } | undefined ): InitialTransitionDefinition { const targetString = typeof _target === 'object' && _target !== null ? _target.target : _target; const input = typeof _target === 'object' && _target !== null ? _target.input : undefined; const to = typeof _target === 'object' && _target !== null ? _target.to : undefined; const meta = typeof _target === 'object' && _target !== null ? _target.meta : undefined; const description = typeof _target === 'object' && _target !== null ? _target.description : undefined; const targetStrings = Array.isArray(targetString) ? targetString : targetString ? [targetString] : []; const resolvedTargets = targetStrings.map((target) => target.startsWith('#') ? stateNode.machine.getStateNodeById(target.slice(1)) : stateNode.states[target] ); if (resolvedTargets.some((target) => !target)) { throw new Error( isDevelopment ? `Initial state node "${targetStrings.find((_, index) => !resolvedTargets[index])}" not found on parent state node #${stateNode.id}` : xstateError( 64, targetStrings.find((_, index) => !resolvedTargets[index]), stateNode.id ) ); } const transition: InitialTransitionDefinition = { source: stateNode, target: resolvedTargets.length ? (resolvedTargets as AnyStateNode[]) : undefined, input, to, meta, description }; return transition; }