UNPKG

57.3 kBTypeScriptView Raw
1import type { MachineSnapshot } from "./State.js";
2import type { StateMachine } from "./StateMachine.js";
3import type { StateNode } from "./StateNode.js";
4import { AssignArgs } from "./actions/assign.js";
5import { PromiseActorLogic } from "./actors/promise.js";
6import { Guard, GuardPredicate, UnknownGuard } from "./guards.js";
7import type { Actor } from "./createActor.js";
8import { Spawner } from "./spawn.js";
9import { AnyActorSystem, Clock } from './system.js';
10import { InspectionEvent } from "./inspection.js";
11import { ExecutableRaiseAction } from "./actions/raise.js";
12import { ExecutableSendToAction } from "./actions/send.js";
13export type Identity<T> = {
14 [K in keyof T]: T[K];
15};
16export type HomomorphicPick<T, K extends keyof any> = {
17 [P in keyof T as P & K]: T[P];
18};
19export type HomomorphicOmit<T, K extends keyof any> = {
20 [P in keyof T as Exclude<P, K>]: T[P];
21};
22export type Invert<T extends Record<PropertyKey, PropertyKey>> = {
23 [K in keyof T as T[K]]: K;
24};
25export type GetParameterizedParams<T extends ParameterizedObject | undefined> = T extends any ? ('params' extends keyof T ? T['params'] : undefined) : never;
26/**
27 * @remarks
28 * `T | unknown` reduces to `unknown` and that can be problematic when it comes
29 * to contextual typing. It especially is a problem when the union has a
30 * function member, like here:
31 *
32 * ```ts
33 * declare function test(
34 * cbOrVal: ((arg: number) => unknown) | unknown
35 * ): void;
36 * test((arg) => {}); // oops, implicit any
37 * ```
38 *
39 * This type can be used to avoid this problem. This union represents the same
40 * value space as `unknown`.
41 */
42export type NonReducibleUnknown = {} | null | undefined;
43export type AnyFunction = (...args: any[]) => any;
44type ReturnTypeOrValue<T> = T extends AnyFunction ? ReturnType<T> : T;
45export type IsNever<T> = [T] extends [never] ? true : false;
46export type IsNotNever<T> = [T] extends [never] ? false : true;
47export type Compute<A> = {
48 [K in keyof A]: A[K];
49} & unknown;
50export type Prop<T, K> = K extends keyof T ? T[K] : never;
51export type Values<T> = T[keyof T];
52export type Elements<T> = T[keyof T & `${number}`];
53export type Merge<M, N> = Omit<M, keyof N> & N;
54export type IndexByProp<T extends Record<P, string>, P extends keyof T> = {
55 [E in T as E[P]]: E;
56};
57export type IndexByType<T extends {
58 type: string;
59}> = IndexByProp<T, 'type'>;
60export type Equals<A1, A2> = (<A>() => A extends A2 ? true : false) extends <A>() => A extends A1 ? true : false ? true : false;
61export type IsAny<T> = Equals<T, any>;
62export type Cast<A, B> = A extends B ? A : B;
63export type DoNotInfer<T> = [T][T extends any ? 0 : any];
64/** @deprecated Use the built-in `NoInfer` type instead */
65export type NoInfer<T> = DoNotInfer<T>;
66export type LowInfer<T> = T & NonNullable<unknown>;
67export type MetaObject = Record<string, any>;
68export type Lazy<T> = () => T;
69export type MaybeLazy<T> = T | Lazy<T>;
70/** The full definition of an event, with a string `type`. */
71export type EventObject = {
72 /** The type of event that is sent. */
73 type: string;
74};
75export interface AnyEventObject extends EventObject {
76 [key: string]: any;
77}
78export interface ParameterizedObject {
79 type: string;
80 params?: NonReducibleUnknown;
81}
82export interface UnifiedArg<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject> {
83 context: TContext;
84 event: TExpressionEvent;
85 self: ActorRef<MachineSnapshot<TContext, TEvent, Record<string, AnyActorRef | undefined>, // TODO: this should be replaced with `TChildren`
86 StateValue, string, unknown, TODO, // TMeta
87 TODO>, TEvent, AnyEventObject>;
88 system: AnyActorSystem;
89}
90export type MachineContext = Record<string, any>;
91export interface ActionArgs<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject> extends UnifiedArg<TContext, TExpressionEvent, TEvent> {
92}
93export type InputFrom<T> = T extends StateMachine<infer _TContext, infer _TEvent, infer _TChildren, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TStateValue, infer _TTag, infer TInput, infer _TOutput, infer _TEmitted, infer _TMeta, infer _TStateSchema> ? TInput : T extends ActorLogic<infer _TSnapshot, infer _TEvent, infer TInput, infer _TSystem, infer _TEmitted> ? TInput : never;
94export type OutputFrom<T> = T extends ActorLogic<infer TSnapshot, infer _TEvent, infer _TInput, infer _TSystem, infer _TEmitted> ? (TSnapshot & {
95 status: 'done';
96})['output'] : T extends ActorRef<infer TSnapshot, infer _TEvent, infer _TEmitted> ? (TSnapshot & {
97 status: 'done';
98})['output'] : never;
99export type ActionFunction<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject> = {
100 (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams): void;
101 _out_TEvent?: TEvent;
102 _out_TActor?: TActor;
103 _out_TAction?: TAction;
104 _out_TGuard?: TGuard;
105 _out_TDelay?: TDelay;
106 _out_TEmitted?: TEmitted;
107};
108export type NoRequiredParams<T extends ParameterizedObject> = T extends any ? undefined extends T['params'] ? T['type'] : never : never;
109export type ConditionalRequired<T, Condition extends boolean> = Condition extends true ? Required<T> : T;
110export type WithDynamicParams<TContext extends MachineContext, TExpressionEvent extends EventObject, T extends ParameterizedObject> = T extends any ? ConditionalRequired<{
111 type: T['type'];
112 params?: T['params'] | (({ context, event }: {
113 context: TContext;
114 event: TExpressionEvent;
115 }) => T['params']);
116}, undefined extends T['params'] ? false : true> : never;
117export type Action<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject> = NoRequiredParams<TAction> | WithDynamicParams<TContext, TExpressionEvent, TAction> | ActionFunction<TContext, TExpressionEvent, TEvent, TParams, TActor, TAction, TGuard, TDelay, TEmitted>;
118export type UnknownAction = Action<MachineContext, EventObject, EventObject, ParameterizedObject['params'] | undefined, ProvidedActor, ParameterizedObject, ParameterizedObject, string, EventObject>;
119export type Actions<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject> = SingleOrArray<Action<TContext, TExpressionEvent, TEvent, TParams, TActor, TAction, TGuard, TDelay, TEmitted>>;
120export type StateKey = string | AnyMachineSnapshot;
121export interface StateValueMap {
122 [key: string]: StateValue | undefined;
123}
124/**
125 * The string or object representing the state value relative to the parent
126 * state node.
127 *
128 * @remarks
129 * - For a child atomic state node, this is a string, e.g., `"pending"`.
130 * - For complex state nodes, this is an object, e.g., `{ success:
131 * "someChildState" }`.
132 */
133export type StateValue = string | StateValueMap;
134export type TransitionTarget = SingleOrArray<string>;
135export interface TransitionConfig<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject = EventObject, TMeta extends MetaObject = MetaObject> {
136 guard?: Guard<TContext, TExpressionEvent, undefined, TGuard>;
137 actions?: Actions<TContext, TExpressionEvent, TEvent, undefined, TActor, TAction, TGuard, TDelay, TEmitted>;
138 reenter?: boolean;
139 target?: TransitionTarget | undefined;
140 meta?: TMeta;
141 description?: string;
142}
143export interface InitialTransitionConfig<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> extends TransitionConfig<TContext, TEvent, TEvent, TActor, TAction, TGuard, TDelay, TODO, // TEmitted
144TODO> {
145 target: string;
146}
147export type AnyTransitionConfig = TransitionConfig<any, // TContext
148any, // TExpressionEvent
149any, // TEvent
150any, // TActor
151any, // TAction
152any, // TGuard
153any, // TDelay
154any, // TEmitted
155any>;
156export interface InvokeDefinition<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject, TMeta extends MetaObject> {
157 id: string;
158 systemId: string | undefined;
159 /** The source of the actor logic to be invoked */
160 src: AnyActorLogic | string;
161 input?: Mapper<TContext, TEvent, NonReducibleUnknown, TEvent> | NonReducibleUnknown;
162 /**
163 * The transition to take upon the invoked child machine reaching its final
164 * top-level state.
165 */
166 onDone?: string | SingleOrArray<TransitionConfig<TContext, DoneActorEvent<unknown>, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
167 /**
168 * The transition to take upon the invoked child machine sending an error
169 * event.
170 */
171 onError?: string | SingleOrArray<TransitionConfig<TContext, ErrorActorEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
172 onSnapshot?: string | SingleOrArray<TransitionConfig<TContext, SnapshotEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
173 toJSON: () => Omit<InvokeDefinition<TContext, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>, 'onDone' | 'onError' | 'toJSON'>;
174}
175type Delay<TDelay extends string> = TDelay | number;
176export type DelayedTransitions<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string> = {
177 [K in Delay<TDelay>]?: string | SingleOrArray<TransitionConfig<TContext, TEvent, TEvent, TActor, TAction, TGuard, TDelay, TODO, // TEmitted
178 TODO>>;
179};
180export type StateTypes = 'atomic' | 'compound' | 'parallel' | 'final' | 'history' | ({} & string);
181export type SingleOrArray<T> = readonly T[] | T;
182export type StateNodesConfig<TContext extends MachineContext, TEvent extends EventObject> = {
183 [K in string]: StateNode<TContext, TEvent>;
184};
185export type StatesConfig<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TOutput, TEmitted extends EventObject, TMeta extends MetaObject> = {
186 [K in string]: StateNodeConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TOutput, TEmitted, TMeta>;
187};
188export type StatesDefinition<TContext extends MachineContext, TEvent extends EventObject> = {
189 [K in string]: StateNodeDefinition<TContext, TEvent>;
190};
191export type TransitionConfigTarget = string | undefined;
192export type TransitionConfigOrTarget<TContext extends MachineContext, TExpressionEvent extends EventObject, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject, TMeta extends MetaObject> = SingleOrArray<TransitionConfigTarget | TransitionConfig<TContext, TExpressionEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
193export type TransitionsConfig<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject, TMeta extends MetaObject> = {
194 [K in EventDescriptor<TEvent>]?: TransitionConfigOrTarget<TContext, ExtractEvent<TEvent, K>, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>;
195};
196type PartialEventDescriptor<TEventType extends string> = TEventType extends `${infer TLeading}.${infer TTail}` ? `${TLeading}.*` | `${TLeading}.${PartialEventDescriptor<TTail>}` : never;
197export type EventDescriptor<TEvent extends EventObject> = TEvent['type'] | PartialEventDescriptor<TEvent['type']> | '*';
198type NormalizeDescriptor<TDescriptor extends string> = TDescriptor extends '*' ? string : TDescriptor extends `${infer TLeading}.*` ? `${TLeading}.${string}` : TDescriptor;
199export type IsLiteralString<T extends string> = string extends T ? false : true;
200type DistributeActors<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject, TMeta extends MetaObject, TSpecificActor extends ProvidedActor> = TSpecificActor extends {
201 src: infer TSrc;
202} ? Compute<{
203 systemId?: string;
204 /** The source of the machine to be invoked, or the machine itself. */
205 src: TSrc;
206 /**
207 * The unique identifier for the invoked machine. If not specified,
208 * this will be the machine's own `id`, or the URL (from `src`).
209 */
210 id?: TSpecificActor['id'];
211 input?: Mapper<TContext, TEvent, InputFrom<TSpecificActor['logic']>, TEvent> | InputFrom<TSpecificActor['logic']>;
212 /**
213 * The transition to take upon the invoked child machine reaching
214 * its final top-level state.
215 */
216 onDone?: string | SingleOrArray<TransitionConfigOrTarget<TContext, DoneActorEvent<OutputFrom<TSpecificActor['logic']>>, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
217 /**
218 * The transition to take upon the invoked child machine sending an
219 * error event.
220 */
221 onError?: string | SingleOrArray<TransitionConfigOrTarget<TContext, ErrorActorEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
222 onSnapshot?: string | SingleOrArray<TransitionConfigOrTarget<TContext, SnapshotEvent<SnapshotFrom<TSpecificActor['logic']>>, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
223} & {
224 [K in RequiredActorOptions<TSpecificActor>]: unknown;
225}> | {
226 id?: never;
227 systemId?: string;
228 src: AnyActorLogic;
229 input?: Mapper<TContext, TEvent, NonReducibleUnknown, TEvent> | NonReducibleUnknown;
230 onDone?: string | SingleOrArray<TransitionConfigOrTarget<TContext, DoneActorEvent<unknown>, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
231 onError?: string | SingleOrArray<TransitionConfigOrTarget<TContext, ErrorActorEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
232 onSnapshot?: string | SingleOrArray<TransitionConfigOrTarget<TContext, SnapshotEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
233} : never;
234export type InvokeConfig<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TEmitted extends EventObject, TMeta extends MetaObject> = IsLiteralString<TActor['src']> extends true ? DistributeActors<TContext, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta, TActor> : {
235 /**
236 * The unique identifier for the invoked machine. If not specified, this
237 * will be the machine's own `id`, or the URL (from `src`).
238 */
239 id?: string;
240 systemId?: string;
241 /** The source of the machine to be invoked, or the machine itself. */
242 src: AnyActorLogic | string;
243 input?: Mapper<TContext, TEvent, NonReducibleUnknown, TEvent> | NonReducibleUnknown;
244 /**
245 * The transition to take upon the invoked child machine reaching its
246 * final top-level state.
247 */
248 onDone?: string | SingleOrArray<TransitionConfigOrTarget<TContext, DoneActorEvent<any>, // TODO: consider replacing with `unknown`
249 TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
250 /**
251 * The transition to take upon the invoked child machine sending an
252 * error event.
253 */
254 onError?: string | SingleOrArray<TransitionConfigOrTarget<TContext, ErrorActorEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
255 onSnapshot?: string | SingleOrArray<TransitionConfigOrTarget<TContext, SnapshotEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
256};
257export type AnyInvokeConfig = InvokeConfig<any, any, any, any, any, any, any, any>;
258export interface StateNodeConfig<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, _TOutput, TEmitted extends EventObject, TMeta extends MetaObject> {
259 /** The initial state transition. */
260 initial?: InitialTransitionConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay> | string | undefined;
261 /**
262 * The type of this state node:
263 *
264 * - `'atomic'` - no child state nodes
265 * - `'compound'` - nested child state nodes (XOR)
266 * - `'parallel'` - orthogonal nested child state nodes (AND)
267 * - `'history'` - history state node
268 * - `'final'` - final state node
269 */
270 type?: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
271 /**
272 * Indicates whether the state node is a history state node, and what type of
273 * history: shallow, deep, true (shallow), false (none), undefined (none)
274 */
275 history?: 'shallow' | 'deep' | boolean | undefined;
276 /**
277 * The mapping of state node keys to their state node configurations
278 * (recursive).
279 */
280 states?: StatesConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, NonReducibleUnknown, TEmitted, TMeta> | undefined;
281 /**
282 * The services to invoke upon entering this state node. These services will
283 * be stopped upon exiting this state node.
284 */
285 invoke?: SingleOrArray<InvokeConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>>;
286 /** The mapping of event types to their potential transition(s). */
287 on?: TransitionsConfig<TContext, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>;
288 /** The action(s) to be executed upon entering the state node. */
289 entry?: Actions<TContext, TEvent, TEvent, undefined, TActor, TAction, TGuard, TDelay, TEmitted>;
290 /** The action(s) to be executed upon exiting the state node. */
291 exit?: Actions<TContext, TEvent, TEvent, undefined, TActor, TAction, TGuard, TDelay, TEmitted>;
292 /**
293 * The potential transition(s) to be taken upon reaching a final child state
294 * node.
295 *
296 * This is equivalent to defining a `[done(id)]` transition on this state
297 * node's `on` property.
298 */
299 onDone?: string | SingleOrArray<TransitionConfig<TContext, DoneStateEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>> | undefined;
300 /**
301 * The mapping (or array) of delays (in milliseconds) to their potential
302 * transition(s). The delayed transitions are taken after the specified delay
303 * in an interpreter.
304 */
305 after?: DelayedTransitions<TContext, TEvent, TActor, TAction, TGuard, TDelay>;
306 /**
307 * An eventless transition that is always taken when this state node is
308 * active.
309 */
310 always?: TransitionConfigOrTarget<TContext, TEvent, TEvent, TActor, TAction, TGuard, TDelay, TEmitted, TMeta>;
311 parent?: StateNode<TContext, TEvent>;
312 /**
313 * The meta data associated with this state node, which will be returned in
314 * State instances.
315 */
316 meta?: TMeta;
317 /**
318 * The output data sent with the "xstate.done.state._id_" event if this is a
319 * final state node.
320 *
321 * The output data will be evaluated with the current `context` and placed on
322 * the `.data` property of the event.
323 */
324 output?: Mapper<TContext, TEvent, unknown, TEvent> | NonReducibleUnknown;
325 /**
326 * The unique ID of the state node, which can be referenced as a transition
327 * target via the `#id` syntax.
328 */
329 id?: string | undefined;
330 /**
331 * The order this state node appears. Corresponds to the implicit document
332 * order.
333 */
334 order?: number;
335 /**
336 * The tags for this state node, which are accumulated into the `state.tags`
337 * property.
338 */
339 tags?: SingleOrArray<TTag>;
340 /** A text description of the state node */
341 description?: string;
342 /** A default target for a history state */
343 target?: string;
344}
345export type AnyStateNodeConfig = StateNodeConfig<any, any, any, any, any, any, any, any, any, // emitted
346any>;
347export interface StateNodeDefinition<TContext extends MachineContext, TEvent extends EventObject> {
348 id: string;
349 version?: string | undefined;
350 key: string;
351 type: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
352 initial: InitialTransitionDefinition<TContext, TEvent> | undefined;
353 history: boolean | 'shallow' | 'deep' | undefined;
354 states: StatesDefinition<TContext, TEvent>;
355 on: TransitionDefinitionMap<TContext, TEvent>;
356 transitions: Array<TransitionDefinition<TContext, TEvent>>;
357 entry: UnknownAction[];
358 exit: UnknownAction[];
359 meta: any;
360 order: number;
361 output?: StateNodeConfig<TContext, TEvent, ProvidedActor, ParameterizedObject, ParameterizedObject, string, string, unknown, EventObject, // TEmitted
362 any>['output'];
363 invoke: Array<InvokeDefinition<TContext, TEvent, TODO, TODO, TODO, TODO, TODO, // TEmitted
364 TODO>>;
365 description?: string;
366 tags: string[];
367}
368export interface StateMachineDefinition<TContext extends MachineContext, TEvent extends EventObject> extends StateNodeDefinition<TContext, TEvent> {
369}
370export type AnyStateNode = StateNode<any, any>;
371export type AnyStateNodeDefinition = StateNodeDefinition<any, any>;
372export type AnyMachineSnapshot = MachineSnapshot<any, any, any, any, any, any, any, any>;
373/** @deprecated Use `AnyMachineSnapshot` instead */
374export type AnyState = AnyMachineSnapshot;
375export type AnyStateMachine = StateMachine<any, // context
376any, // event
377any, // children
378any, // actor
379any, // action
380any, // guard
381any, // delay
382any, // state value
383any, // tag
384any, // input
385any, // output
386any, // emitted
387any, // TMeta
388any>;
389export type AnyStateConfig = StateConfig<any, AnyEventObject>;
390export interface AtomicStateNodeConfig<TContext extends MachineContext, TEvent extends EventObject> extends StateNodeConfig<TContext, TEvent, TODO, TODO, TODO, TODO, TODO, TODO, TODO, // emitted
391TODO> {
392 initial?: undefined;
393 parallel?: false | undefined;
394 states?: undefined;
395 onDone?: undefined;
396}
397export interface HistoryStateNodeConfig<TContext extends MachineContext, TEvent extends EventObject> extends AtomicStateNodeConfig<TContext, TEvent> {
398 history: 'shallow' | 'deep' | true;
399 target: string | undefined;
400}
401export type SimpleOrStateNodeConfig<TContext extends MachineContext, TEvent extends EventObject> = AtomicStateNodeConfig<TContext, TEvent> | StateNodeConfig<TContext, TEvent, TODO, TODO, TODO, TODO, TODO, TODO, TODO, // emitted
402TODO>;
403export type ActionFunctionMap<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, TDelay extends string = string, TEmitted extends EventObject = EventObject> = {
404 [K in TAction['type']]?: ActionFunction<TContext, TEvent, TEvent, GetParameterizedParams<TAction extends {
405 type: K;
406 } ? TAction : never>, TActor, TAction, TGuard, TDelay, TEmitted>;
407};
408type GuardMap<TContext extends MachineContext, TEvent extends EventObject, TGuard extends ParameterizedObject> = {
409 [K in TGuard['type']]?: GuardPredicate<TContext, TEvent, GetParameterizedParams<TGuard extends {
410 type: K;
411 } ? TGuard : never>, TGuard>;
412};
413export type DelayFunctionMap<TContext extends MachineContext, TEvent extends EventObject, TAction extends ParameterizedObject> = Record<string, DelayConfig<TContext, TEvent, TAction['params'], TEvent>>;
414export type DelayConfig<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject> = number | DelayExpr<TContext, TExpressionEvent, TParams, TEvent>;
415/** @ignore */
416export interface MachineImplementationsSimplified<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject> {
417 guards: GuardMap<TContext, TEvent, TGuard>;
418 actions: ActionFunctionMap<TContext, TEvent, TActor, TAction>;
419 actors: Record<string, AnyActorLogic | {
420 src: AnyActorLogic;
421 input: Mapper<TContext, TEvent, unknown, TEvent> | NonReducibleUnknown;
422 }>;
423 delays: DelayFunctionMap<TContext, TEvent, TAction>;
424}
425type MachineImplementationsActions<TTypes extends StateMachineTypes> = {
426 [K in TTypes['actions']['type']]?: ActionFunction<TTypes['context'], TTypes['events'], TTypes['events'], GetConcreteByKey<TTypes['actions'], 'type', K>['params'], TTypes['actors'], TTypes['actions'], TTypes['guards'], TTypes['delays'], TTypes['emitted']>;
427};
428type MachineImplementationsActors<TTypes extends StateMachineTypes> = {
429 [K in TTypes['actors']['src']]?: GetConcreteByKey<TTypes['actors'], 'src', K>['logic'];
430};
431type MachineImplementationsDelays<TTypes extends StateMachineTypes> = {
432 [K in TTypes['delays']]?: DelayConfig<TTypes['context'], TTypes['events'], undefined, TTypes['events']>;
433};
434type MachineImplementationsGuards<TTypes extends StateMachineTypes> = {
435 [K in TTypes['guards']['type']]?: Guard<TTypes['context'], TTypes['events'], GetConcreteByKey<TTypes['guards'], 'type', K>['params'], TTypes['guards']>;
436};
437export type InternalMachineImplementations<TTypes extends StateMachineTypes> = {
438 actions?: MachineImplementationsActions<TTypes>;
439 actors?: MachineImplementationsActors<TTypes>;
440 delays?: MachineImplementationsDelays<TTypes>;
441 guards?: MachineImplementationsGuards<TTypes>;
442};
443type InitialContext<TContext extends MachineContext, TActor extends ProvidedActor, TInput, TEvent extends EventObject> = TContext | ContextFactory<TContext, TActor, TInput, TEvent>;
444export type ContextFactory<TContext extends MachineContext, TActor extends ProvidedActor, TInput, TEvent extends EventObject = EventObject> = ({ spawn, input, self }: {
445 spawn: Spawner<TActor>;
446 input: TInput;
447 self: ActorRef<MachineSnapshot<TContext, TEvent, Record<string, AnyActorRef | undefined>, // TODO: this should be replaced with `TChildren`
448 StateValue, string, unknown, TODO, // TMeta
449 TODO>, TEvent, AnyEventObject>;
450}) => TContext;
451export type MachineConfig<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, TDelay extends string = string, TTag extends string = string, TInput = any, TOutput = unknown, TEmitted extends EventObject = EventObject, TMeta extends MetaObject = MetaObject> = (Omit<StateNodeConfig<DoNotInfer<TContext>, DoNotInfer<TEvent>, DoNotInfer<TActor>, DoNotInfer<TAction>, DoNotInfer<TGuard>, DoNotInfer<TDelay>, DoNotInfer<TTag>, DoNotInfer<TOutput>, DoNotInfer<TEmitted>, DoNotInfer<TMeta>>, 'output'> & {
452 /** The initial context (extended state) */
453 /** The machine's own version. */
454 version?: string;
455 output?: Mapper<TContext, DoneStateEvent, TOutput, TEvent> | TOutput;
456}) & (MachineContext extends TContext ? {
457 context?: InitialContext<LowInfer<TContext>, TActor, TInput, TEvent>;
458} : {
459 context: InitialContext<LowInfer<TContext>, TActor, TInput, TEvent>;
460});
461export type UnknownMachineConfig = MachineConfig<MachineContext, EventObject>;
462export interface ProvidedActor {
463 src: string;
464 logic: UnknownActorLogic;
465 id?: string;
466}
467export interface SetupTypes<TContext extends MachineContext, TEvent extends EventObject, TChildrenMap extends Record<string, string>, TTag extends string, TInput, TOutput, TEmitted extends EventObject, TMeta extends MetaObject> {
468 context?: TContext;
469 events?: TEvent;
470 children?: TChildrenMap;
471 tags?: TTag;
472 input?: TInput;
473 output?: TOutput;
474 emitted?: TEmitted;
475 meta?: TMeta;
476}
477export interface MachineTypes<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TInput, TOutput, TEmitted extends EventObject, TMeta extends MetaObject> extends SetupTypes<TContext, TEvent, never, TTag, TInput, TOutput, TEmitted, TMeta> {
478 actors?: TActor;
479 actions?: TAction;
480 guards?: TGuard;
481 delays?: TDelay;
482 meta?: TMeta;
483}
484export interface HistoryStateNode<TContext extends MachineContext> extends StateNode<TContext> {
485 history: 'shallow' | 'deep';
486 target: string | undefined;
487}
488export type HistoryValue<TContext extends MachineContext, TEvent extends EventObject> = Record<string, Array<StateNode<TContext, TEvent>>>;
489export type AnyHistoryValue = HistoryValue<any, any>;
490export type StateFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = T extends AnyStateMachine ? ReturnType<T['transition']> : T extends (...args: any[]) => AnyStateMachine ? ReturnType<ReturnType<T>['transition']> : never;
491export type Transitions<TContext extends MachineContext, TEvent extends EventObject> = Array<TransitionDefinition<TContext, TEvent>>;
492export interface DoneActorEvent<TOutput = unknown, TId extends string = string> extends EventObject {
493 type: `xstate.done.actor.${TId}`;
494 output: TOutput;
495 actorId: TId;
496}
497export interface ErrorActorEvent<TErrorData = unknown, TId extends string = string> extends EventObject {
498 type: `xstate.error.actor.${TId}`;
499 error: TErrorData;
500 actorId: TId;
501}
502export interface SnapshotEvent<TSnapshot extends Snapshot<unknown> = Snapshot<unknown>> extends EventObject {
503 type: `xstate.snapshot.${string}`;
504 snapshot: TSnapshot;
505}
506export interface DoneStateEvent<TOutput = unknown> extends EventObject {
507 type: `xstate.done.state.${string}`;
508 output: TOutput;
509}
510export type DelayExpr<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject> = (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams) => number;
511export type LogExpr<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject> = (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams) => unknown;
512export type SendExpr<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TSentEvent extends EventObject, TEvent extends EventObject> = (args: ActionArgs<TContext, TExpressionEvent, TEvent>, params: TParams) => TSentEvent;
513export declare enum SpecialTargets {
514 Parent = "#_parent",
515 Internal = "#_internal"
516}
517export interface SendToActionOptions<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TDelay extends string> extends RaiseActionOptions<TContext, TExpressionEvent, TParams, TEvent, TDelay> {
518}
519export interface RaiseActionOptions<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TDelay extends string> {
520 id?: string;
521 delay?: Delay<TDelay> | DelayExpr<TContext, TExpressionEvent, TParams, TEvent>;
522}
523export interface RaiseActionParams<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TDelay extends string> extends RaiseActionOptions<TContext, TExpressionEvent, TParams, TEvent, TDelay> {
524 event: TEvent | SendExpr<TContext, TExpressionEvent, TParams, TEvent, TEvent>;
525}
526export interface SendToActionParams<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TSentEvent extends EventObject, TEvent extends EventObject, TDelay extends string> extends SendToActionOptions<TContext, TExpressionEvent, TParams, TEvent, TDelay> {
527 event: TSentEvent | SendExpr<TContext, TExpressionEvent, TParams, TSentEvent, TEvent>;
528}
529export type Assigner<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor> = (args: AssignArgs<TContext, TExpressionEvent, TEvent, TActor>, params: TParams) => Partial<TContext>;
530export type PartialAssigner<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor, TKey extends keyof TContext> = (args: AssignArgs<TContext, TExpressionEvent, TEvent, TActor>, params: TParams) => TContext[TKey];
531export type PropertyAssigner<TContext extends MachineContext, TExpressionEvent extends EventObject, TParams extends ParameterizedObject['params'] | undefined, TEvent extends EventObject, TActor extends ProvidedActor> = {
532 [K in keyof TContext]?: PartialAssigner<TContext, TExpressionEvent, TParams, TEvent, TActor, K> | TContext[K];
533};
534export type Mapper<TContext extends MachineContext, TExpressionEvent extends EventObject, TResult, TEvent extends EventObject> = (args: {
535 context: TContext;
536 event: TExpressionEvent;
537 self: ActorRef<MachineSnapshot<TContext, TEvent, Record<string, AnyActorRef>, // TODO: this should be replaced with `TChildren`
538 StateValue, string, unknown, TODO, // TMeta
539 TODO>, TEvent, AnyEventObject>;
540}) => TResult;
541export interface TransitionDefinition<TContext extends MachineContext, TEvent extends EventObject> extends Omit<TransitionConfig<TContext, TEvent, TEvent, TODO, TODO, TODO, TODO, TODO, // TEmitted
542TODO>, 'target' | 'guard'> {
543 target: ReadonlyArray<StateNode<TContext, TEvent>> | undefined;
544 source: StateNode<TContext, TEvent>;
545 actions: readonly UnknownAction[];
546 reenter: boolean;
547 guard?: UnknownGuard;
548 eventType: EventDescriptor<TEvent>;
549 toJSON: () => {
550 target: string[] | undefined;
551 source: string;
552 actions: readonly UnknownAction[];
553 guard?: UnknownGuard;
554 eventType: EventDescriptor<TEvent>;
555 meta?: Record<string, any>;
556 };
557}
558export type AnyTransitionDefinition = TransitionDefinition<any, any>;
559export interface InitialTransitionDefinition<TContext extends MachineContext, TEvent extends EventObject> extends TransitionDefinition<TContext, TEvent> {
560 target: ReadonlyArray<StateNode<TContext, TEvent>>;
561 guard?: never;
562}
563export type TransitionDefinitionMap<TContext extends MachineContext, TEvent extends EventObject> = {
564 [K in EventDescriptor<TEvent>]: Array<TransitionDefinition<TContext, ExtractEvent<TEvent, K>>>;
565};
566export interface DelayedTransitionDefinition<TContext extends MachineContext, TEvent extends EventObject> extends TransitionDefinition<TContext, TEvent> {
567 delay: number | string | DelayExpr<TContext, TEvent, undefined, TEvent>;
568}
569export interface StateLike<TContext extends MachineContext> {
570 value: StateValue;
571 context: TContext;
572 event: EventObject;
573}
574export interface StateConfig<TContext extends MachineContext, TEvent extends EventObject> {
575 context: TContext;
576 historyValue?: HistoryValue<TContext, TEvent>;
577 children: Record<string, AnyActorRef>;
578 status: SnapshotStatus;
579 output?: any;
580 error?: unknown;
581 machine?: StateMachine<TContext, TEvent, any, any, any, any, any, any, any, any, any, any, any, // TMeta
582 any>;
583}
584export interface ActorOptions<TLogic extends AnyActorLogic> {
585 /**
586 * The clock that is responsible for setting and clearing timeouts, such as
587 * delayed events and transitions.
588 *
589 * @remarks
590 * You can create your own “clock”. The clock interface is an object with two
591 * functions/methods:
592 *
593 * - `setTimeout` - same arguments as `window.setTimeout(fn, timeout)`
594 * - `clearTimeout` - same arguments as `window.clearTimeout(id)`
595 *
596 * By default, the native `setTimeout` and `clearTimeout` functions are used.
597 *
598 * For testing, XState provides `SimulatedClock`.
599 * @see {@link Clock}
600 * @see {@link SimulatedClock}
601 */
602 clock?: Clock;
603 /**
604 * Specifies the logger to be used for `log(...)` actions. Defaults to the
605 * native `console.log(...)` method.
606 */
607 logger?: (...args: any[]) => void;
608 parent?: AnyActorRef;
609 /** The custom `id` for referencing this service. */
610 id?: string;
611 /** @deprecated Use `inspect` instead. */
612 devTools?: never;
613 /** The system ID to register this actor under. */
614 systemId?: string;
615 /** The input data to pass to the actor. */
616 input?: InputFrom<TLogic>;
617 /**
618 * Initializes actor logic from a specific persisted internal state.
619 *
620 * @remarks
621 * If the state is compatible with the actor logic, when the actor is started
622 * it will be at that persisted state. Actions from machine actors will not be
623 * re-executed, because they are assumed to have been already executed.
624 * However, invocations will be restarted, and spawned actors will be restored
625 * recursively.
626 *
627 * Can be generated with {@link Actor.getPersistedSnapshot}.
628 * @see https://stately.ai/docs/persistence
629 */
630 snapshot?: Snapshot<unknown>;
631 /** @deprecated Use `snapshot` instead. */
632 state?: Snapshot<unknown>;
633 /** The source actor logic. */
634 src?: string | AnyActorLogic;
635 /**
636 * A callback function or observer object which can be used to inspect actor
637 * system updates.
638 *
639 * @remarks
640 * If a callback function is provided, it can accept an inspection event
641 * argument. The types of inspection events that can be observed include:
642 *
643 * - `@xstate.actor` - An actor ref has been created in the system
644 * - `@xstate.event` - An event was sent from a source actor ref to a target
645 * actor ref in the system
646 * - `@xstate.snapshot` - An actor ref emitted a snapshot due to a received
647 * event
648 *
649 * @example
650 *
651 * ```ts
652 * import { createMachine } from 'xstate';
653 *
654 * const machine = createMachine({
655 * // ...
656 * });
657 *
658 * const actor = createActor(machine, {
659 * inspect: (inspectionEvent) => {
660 * if (inspectionEvent.actorRef === actor) {
661 * // This event is for the root actor
662 * }
663 *
664 * if (inspectionEvent.type === '@xstate.actor') {
665 * console.log(inspectionEvent.actorRef);
666 * }
667 *
668 * if (inspectionEvent.type === '@xstate.event') {
669 * console.log(inspectionEvent.sourceRef);
670 * console.log(inspectionEvent.actorRef);
671 * console.log(inspectionEvent.event);
672 * }
673 *
674 * if (inspectionEvent.type === '@xstate.snapshot') {
675 * console.log(inspectionEvent.actorRef);
676 * console.log(inspectionEvent.event);
677 * console.log(inspectionEvent.snapshot);
678 * }
679 * }
680 * });
681 * ```
682 *
683 * Alternately, an observer object (`{ next?, error?, complete? }`) can be
684 * provided:
685 *
686 * @example
687 *
688 * ```ts
689 * const actor = createActor(machine, {
690 * inspect: {
691 * next: (inspectionEvent) => {
692 * if (inspectionEvent.actorRef === actor) {
693 * // This event is for the root actor
694 * }
695 *
696 * if (inspectionEvent.type === '@xstate.actor') {
697 * console.log(inspectionEvent.actorRef);
698 * }
699 *
700 * if (inspectionEvent.type === '@xstate.event') {
701 * console.log(inspectionEvent.sourceRef);
702 * console.log(inspectionEvent.actorRef);
703 * console.log(inspectionEvent.event);
704 * }
705 *
706 * if (inspectionEvent.type === '@xstate.snapshot') {
707 * console.log(inspectionEvent.actorRef);
708 * console.log(inspectionEvent.event);
709 * console.log(inspectionEvent.snapshot);
710 * }
711 * }
712 * }
713 * });
714 * ```
715 */
716 inspect?: Observer<InspectionEvent> | ((inspectionEvent: InspectionEvent) => void);
717}
718export type AnyActor = Actor<any>;
719/** @deprecated Use `AnyActor` instead. */
720export type AnyInterpreter = AnyActor;
721export type Observer<T> = {
722 next?: (value: T) => void;
723 error?: (err: unknown) => void;
724 complete?: () => void;
725};
726export interface Subscription {
727 unsubscribe(): void;
728}
729export interface InteropObservable<T> {
730 [Symbol.observable]: () => InteropSubscribable<T>;
731}
732export interface InteropSubscribable<T> {
733 subscribe(observer: Observer<T>): Subscription;
734}
735export interface Subscribable<T> extends InteropSubscribable<T> {
736 subscribe(observer: Observer<T>): Subscription;
737 subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;
738}
739type EventDescriptorMatches<TEventType extends string, TNormalizedDescriptor> = TEventType extends TNormalizedDescriptor ? true : false;
740export type ExtractEvent<TEvent extends EventObject, TDescriptor extends EventDescriptor<TEvent>> = string extends TEvent['type'] ? TEvent : NormalizeDescriptor<TDescriptor> extends infer TNormalizedDescriptor ? TEvent extends any ? true extends EventDescriptorMatches<TEvent['type'], TNormalizedDescriptor> ? TEvent : never : never : never;
741export interface BaseActorRef<TEvent extends EventObject> {
742 send: (event: TEvent) => void;
743}
744export interface ActorLike<TCurrent, TEvent extends EventObject> extends Subscribable<TCurrent> {
745 send: (event: TEvent) => void;
746}
747export interface ActorRef<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TEmitted extends EventObject = EventObject> extends Subscribable<TSnapshot>, InteropObservable<TSnapshot> {
748 /** The unique identifier for this actor relative to its parent. */
749 id: string;
750 sessionId: string;
751 send: (event: TEvent) => void;
752 start: () => void;
753 getSnapshot: () => TSnapshot;
754 getPersistedSnapshot: () => Snapshot<unknown>;
755 stop: () => void;
756 toJSON?: () => any;
757 _parent?: AnyActorRef;
758 system: AnyActorSystem;
759 src: string | AnyActorLogic;
760 on: <TType extends TEmitted['type'] | '*'>(type: TType, handler: (emitted: TEmitted & (TType extends '*' ? unknown : {
761 type: TType;
762 })) => void) => Subscription;
763}
764export type AnyActorRef = ActorRef<any, any, // TODO: shouldn't this be AnyEventObject?
765any>;
766export type ActorRefLike = Pick<AnyActorRef, 'sessionId' | 'send' | 'getSnapshot'>;
767export type UnknownActorRef = ActorRef<Snapshot<unknown>, EventObject>;
768export type ActorLogicFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<any, any, any, any, any, any, any, any, any, any, any, any, any, // TMeta
769any> ? R : R extends Promise<infer U> ? PromiseActorLogic<U> : never : never;
770export type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, infer TEvent, infer TChildren, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer TStateValue, infer TTag, infer _TInput, infer TOutput, infer TEmitted, infer TMeta, infer TStateSchema> ? ActorRef<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, TEvent, TEmitted> : R extends Promise<infer U> ? ActorRefFrom<PromiseActorLogic<U>> : R extends ActorLogic<infer TSnapshot, infer TEvent, infer _TInput, infer _TSystem, infer TEmitted> ? ActorRef<TSnapshot, TEvent, TEmitted> : never : never;
771export type ActorRefFromLogic<T extends AnyActorLogic> = ActorRef<SnapshotFrom<T>, EventFromLogic<T>, EmittedFrom<T>>;
772export type DevToolsAdapter = (service: AnyActor) => void;
773/** @deprecated Use `Actor<T>` instead. */
774export type InterpreterFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, infer TEvent, infer TChildren, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer TStateValue, infer TTag, infer TInput, infer TOutput, infer TEmitted, infer TMeta, infer TStateSchema> ? Actor<ActorLogic<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, TEvent, TInput, AnyActorSystem, TEmitted>> : never;
775export type MachineImplementationsFrom<T extends AnyStateMachine | ((...args: any[]) => AnyStateMachine)> = ReturnTypeOrValue<T> extends StateMachine<infer TContext, infer TEvent, infer _TChildren, infer TActor, infer TAction, infer TGuard, infer TDelay, infer _TStateValue, infer TTag, infer _TInput, infer _TOutput, infer TEmitted, infer _TMeta, infer _TStateSchema> ? InternalMachineImplementations<ResolvedStateMachineTypes<TContext, TEvent, TActor, TAction, TGuard, TDelay, TTag, TEmitted>> : never;
776export interface ActorScope<TSnapshot extends Snapshot<unknown>, TEvent extends EventObject, TSystem extends AnyActorSystem = AnyActorSystem, TEmitted extends EventObject = EventObject> {
777 self: ActorRef<TSnapshot, TEvent, TEmitted>;
778 id: string;
779 sessionId: string;
780 logger: (...args: any[]) => void;
781 defer: (fn: () => void) => void;
782 emit: (event: TEmitted) => void;
783 system: TSystem;
784 stopChild: (child: AnyActorRef) => void;
785 actionExecutor: ActionExecutor;
786}
787export type AnyActorScope = ActorScope<any, // TSnapshot
788any, // TEvent
789AnyActorSystem, any>;
790export type SnapshotStatus = 'active' | 'done' | 'error' | 'stopped';
791export type Snapshot<TOutput> = {
792 status: 'active';
793 output: undefined;
794 error: undefined;
795} | {
796 status: 'done';
797 output: TOutput;
798 error: undefined;
799} | {
800 status: 'error';
801 output: undefined;
802 error: unknown;
803} | {
804 status: 'stopped';
805 output: undefined;
806 error: undefined;
807};
808/**
809 * Represents logic which can be used by an actor.
810 *
811 * @template TSnapshot - The type of the snapshot.
812 * @template TEvent - The type of the event object.
813 * @template TInput - The type of the input.
814 * @template TSystem - The type of the actor system.
815 */
816export interface ActorLogic<in out TSnapshot extends Snapshot<unknown>, // it's invariant because it's also part of `ActorScope["self"]["getSnapshot"]`
817in out TEvent extends EventObject, // it's invariant because it's also part of `ActorScope["self"]["send"]`
818in TInput = NonReducibleUnknown, TSystem extends AnyActorSystem = AnyActorSystem, in out TEmitted extends EventObject = EventObject> {
819 /** The initial setup/configuration used to create the actor logic. */
820 config?: unknown;
821 /**
822 * Transition function that processes the current state and an incoming event
823 * to produce a new state.
824 *
825 * @param snapshot - The current state.
826 * @param event - The incoming event.
827 * @param actorScope - The actor scope.
828 * @returns The new state.
829 */
830 transition: (snapshot: TSnapshot, event: TEvent, actorScope: ActorScope<TSnapshot, TEvent, TSystem, TEmitted>) => TSnapshot;
831 /**
832 * Called to provide the initial state of the actor.
833 *
834 * @param actorScope - The actor scope.
835 * @param input - The input for the initial state.
836 * @returns The initial state.
837 */
838 getInitialSnapshot: (actorScope: ActorScope<TSnapshot, TEvent, TSystem, TEmitted>, input: TInput) => TSnapshot;
839 /**
840 * Called when Actor is created to restore the internal state of the actor
841 * given a persisted state. The persisted state can be created by
842 * `getPersistedSnapshot`.
843 *
844 * @param persistedState - The persisted state to restore from.
845 * @param actorScope - The actor scope.
846 * @returns The restored state.
847 */
848 restoreSnapshot?: (persistedState: Snapshot<unknown>, actorScope: ActorScope<TSnapshot, TEvent, AnyActorSystem, TEmitted>) => TSnapshot;
849 /**
850 * Called when the actor is started.
851 *
852 * @param snapshot - The starting state.
853 * @param actorScope - The actor scope.
854 */
855 start?: (snapshot: TSnapshot, actorScope: ActorScope<TSnapshot, TEvent, AnyActorSystem, TEmitted>) => void;
856 /**
857 * Obtains the internal state of the actor in a representation which can be be
858 * persisted. The persisted state can be restored by `restoreSnapshot`.
859 *
860 * @param snapshot - The current state.
861 * @returns The a representation of the internal state to be persisted.
862 */
863 getPersistedSnapshot: (snapshot: TSnapshot, options?: unknown) => Snapshot<unknown>;
864}
865export type AnyActorLogic = ActorLogic<any, // snapshot
866any, // event
867any, // input
868any, // system
869any>;
870export type UnknownActorLogic = ActorLogic<any, // snapshot
871any, // event
872any, // input
873AnyActorSystem, any>;
874export type SnapshotFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends ActorRef<infer TSnapshot, infer _, infer __> ? TSnapshot : R extends Actor<infer TLogic> ? SnapshotFrom<TLogic> : R extends ActorLogic<infer _TSnapshot, infer _TEvent, infer _TInput, infer _TEmitted, infer _TSystem> ? ReturnType<R['transition']> : R extends ActorScope<infer TSnapshot, infer _TEvent, infer _TEmitted, infer _TSystem> ? TSnapshot : never : never;
875export type EventFromLogic<TLogic extends AnyActorLogic> = TLogic extends ActorLogic<infer _TSnapshot, infer TEvent, infer _TInput, infer _TEmitted, infer _TSystem> ? TEvent : never;
876export type EmittedFrom<TLogic extends AnyActorLogic> = TLogic extends ActorLogic<infer _TSnapshot, infer _TEvent, infer _TInput, infer _TSystem, infer TEmitted> ? TEmitted : never;
877type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer _TContext, infer TEvent, infer _TChildren, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TStateValue, infer _TTag, infer _TInput, infer _TOutput, infer _TEmitted, infer _TMeta, infer _TStateSchema> ? TEvent : R extends MachineSnapshot<infer _TContext, infer TEvent, infer _TChildren, infer _TStateValue, infer _TTag, infer _TOutput, infer _TMeta, infer _TStateSchema> ? TEvent : R extends ActorRef<infer _TSnapshot, infer TEvent, infer _TEmitted> ? TEvent : never : never;
878export type EventFrom<T, K extends Prop<TEvent, 'type'> = never, TEvent extends EventObject = ResolveEventType<T>> = IsNever<K> extends true ? TEvent : ExtractEvent<TEvent, K>;
879export type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R ? R extends StateMachine<infer TContext, infer _TEvent, infer _TChildren, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TStateValue, infer _TTag, infer _TInput, infer _TOutput, infer _TEmitted, infer _TMeta, infer _TStateSchema> ? TContext : R extends MachineSnapshot<infer TContext, infer _TEvent, infer _TChildren, infer _TStateValue, infer _TTag, infer _TOutput, infer _TMeta, infer _TStateSchema> ? TContext : R extends Actor<infer TActorLogic> ? TActorLogic extends StateMachine<infer TContext, infer _TEvent, infer _TChildren, infer _TActor, infer _TAction, infer _TGuard, infer _TDelay, infer _TStateValue, infer _TTag, infer _TInput, infer _TOutput, infer _TEmitted, infer _TMeta, infer _TStateSchema> ? TContext : never : never : never;
880export type InferEvent<E extends EventObject> = {
881 [T in E['type']]: {
882 type: T;
883 } & Extract<E, {
884 type: T;
885 }>;
886}[E['type']];
887export type TODO = any;
888export type StateValueFrom<TMachine extends AnyStateMachine> = Parameters<StateFrom<TMachine>['matches']>[0];
889export type TagsFrom<TMachine extends AnyStateMachine> = Parameters<StateFrom<TMachine>['hasTag']>[0];
890export interface ActorSystemInfo {
891 actors: Record<string, AnyActorRef>;
892}
893export type RequiredActorOptions<TActor extends ProvidedActor> = (undefined extends TActor['id'] ? never : 'id') | (undefined extends InputFrom<TActor['logic']> ? never : 'input');
894type ExtractLiteralString<T extends string | undefined> = T extends string ? string extends T ? never : T : never;
895type ToConcreteChildren<TActor extends ProvidedActor> = {
896 [A in TActor as ExtractLiteralString<A['id']>]?: ActorRefFromLogic<A['logic']>;
897};
898export type ToChildren<TActor extends ProvidedActor> = string extends TActor['src'] ? Record<string, AnyActorRef> : Compute<ToConcreteChildren<TActor> & {
899 include: {
900 [id: string]: TActor extends any ? ActorRefFromLogic<TActor['logic']> | undefined : never;
901 };
902 exclude: unknown;
903}[undefined extends TActor['id'] ? 'include' : string extends TActor['id'] ? 'include' : 'exclude']>;
904export type StateSchema = {
905 id?: string;
906 states?: Record<string, StateSchema>;
907 type?: unknown;
908 invoke?: unknown;
909 on?: unknown;
910 entry?: unknown;
911 exit?: unknown;
912 onDone?: unknown;
913 after?: unknown;
914 always?: unknown;
915 meta?: unknown;
916 output?: unknown;
917 tags?: unknown;
918 description?: unknown;
919};
920export type StateId<TSchema extends StateSchema, TKey extends string = '(machine)', TParentKey extends string | null = null> = (TSchema extends {
921 id: string;
922} ? TSchema['id'] : TParentKey extends null ? TKey : `${TParentKey}.${TKey}`) | (TSchema['states'] extends Record<string, any> ? Values<{
923 [K in keyof TSchema['states'] & string]: StateId<TSchema['states'][K], K, TParentKey extends string ? `${TParentKey}.${TKey}` : TSchema['id'] extends string ? TSchema['id'] : TKey>;
924}> : never);
925export interface StateMachineTypes {
926 context: MachineContext;
927 events: EventObject;
928 actors: ProvidedActor;
929 actions: ParameterizedObject;
930 guards: ParameterizedObject;
931 delays: string;
932 tags: string;
933 emitted: EventObject;
934}
935/** @deprecated */
936export interface ResolvedStateMachineTypes<TContext extends MachineContext, TEvent extends EventObject, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, TEmitted extends EventObject = EventObject> {
937 context: TContext;
938 events: TEvent;
939 actors: TActor;
940 actions: TAction;
941 guards: TGuard;
942 delays: TDelay;
943 tags: TTag;
944 emitted: TEmitted;
945}
946export type GetConcreteByKey<T, TKey extends keyof T, TValue extends T[TKey]> = T & Record<TKey, TValue>;
947type _GroupStateKeys<T extends StateSchema, S extends keyof T['states']> = S extends any ? T['states'][S] extends {
948 type: 'history';
949} ? [never, never] : T extends {
950 type: 'parallel';
951} ? [S, never] : 'states' extends keyof T['states'][S] ? [S, never] : [never, S] : never;
952type GroupStateKeys<T extends StateSchema, S extends keyof T['states']> = {
953 nonLeaf: _GroupStateKeys<T, S & string>[0];
954 leaf: _GroupStateKeys<T, S & string>[1];
955};
956export type ToStateValue<T extends StateSchema> = T extends {
957 states: Record<infer S, any>;
958} ? IsNever<S> extends true ? {} : GroupStateKeys<T, S>['leaf'] | (IsNever<GroupStateKeys<T, S>['nonLeaf']> extends false ? T extends {
959 type: 'parallel';
960} ? {
961 [K in GroupStateKeys<T, S>['nonLeaf']]: ToStateValue<T['states'][K]>;
962} : Compute<Values<{
963 [K in GroupStateKeys<T, S>['nonLeaf']]: {
964 [StateKey in K]: ToStateValue<T['states'][K]>;
965 };
966}>> : never) : {};
967export interface ExecutableActionObject {
968 type: string;
969 info: ActionArgs<MachineContext, EventObject, EventObject>;
970 params: NonReducibleUnknown;
971 exec: ((info: ActionArgs<any, any, any>, params: unknown) => void) | undefined;
972}
973export interface ToExecutableAction<T extends ParameterizedObject> extends ExecutableActionObject {
974 type: T['type'];
975 params: T['params'];
976 exec: undefined;
977}
978export interface ExecutableSpawnAction extends ExecutableActionObject {
979 type: 'xstate.spawnChild';
980 info: ActionArgs<MachineContext, EventObject, EventObject>;
981 params: {
982 id: string;
983 actorRef: AnyActorRef | undefined;
984 src: string | AnyActorLogic;
985 };
986}
987export type SpecialExecutableAction = ExecutableSpawnAction | ExecutableRaiseAction | ExecutableSendToAction;
988export type ExecutableActionsFrom<T extends AnyActorLogic> = T extends StateMachine<infer _TContext, infer _TEvent, infer _TChildren, infer _TActor, infer TAction, infer _TGuard, infer _TDelay, infer _TStateValue, infer _TTag, infer _TInput, infer _TOutput, infer _TEmitted, infer _TMeta, infer _TConfig> ? SpecialExecutableAction | (string extends TAction['type'] ? never : ToExecutableAction<TAction>) : never;
989export type ActionExecutor = (actionToExecute: ExecutableActionObject) => void;
990export type BuiltinActionResolution = [
991 AnyMachineSnapshot,
992 NonReducibleUnknown,
993 // params
994 UnknownAction[] | undefined
995];
996export {};