xstate 5.32.2 → 6.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +25 -24
  2. package/dist/StateMachine-0aa98f54.development.esm.js +746 -0
  3. package/dist/StateMachine-aef700df.cjs.js +732 -0
  4. package/dist/StateMachine-d4931336.esm.js +729 -0
  5. package/dist/StateMachine-da872dde.development.cjs.js +749 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +40 -26
  8. package/dist/declarations/src/StateNode.d.ts +18 -54
  9. package/dist/declarations/src/actions.d.ts +14 -9
  10. package/dist/declarations/src/actors/callback.d.ts +42 -16
  11. package/dist/declarations/src/actors/index.d.ts +9 -6
  12. package/dist/declarations/src/actors/listener.d.ts +18 -0
  13. package/dist/declarations/src/actors/logic.d.ts +105 -0
  14. package/dist/declarations/src/actors/observable.d.ts +72 -27
  15. package/dist/declarations/src/actors/promise.d.ts +133 -48
  16. package/dist/declarations/src/actors/subscription.d.ts +22 -0
  17. package/dist/declarations/src/createActor.d.ts +33 -34
  18. package/dist/declarations/src/createMachine.d.ts +38 -7
  19. package/dist/declarations/src/createMachineFromConfig.d.ts +177 -0
  20. package/dist/declarations/src/graph/graph.d.ts +6 -4
  21. package/dist/declarations/src/graph/shortestPaths.d.ts +2 -2
  22. package/dist/declarations/src/graph/simplePaths.d.ts +2 -2
  23. package/dist/declarations/src/index.d.ts +13 -9
  24. package/dist/declarations/src/inspection.d.ts +84 -30
  25. package/dist/declarations/src/mapState.d.ts +4 -2
  26. package/dist/declarations/src/schema.types.d.ts +90 -0
  27. package/dist/declarations/src/serialize.d.ts +50 -0
  28. package/dist/declarations/src/setup.d.ts +396 -136
  29. package/dist/declarations/src/spawn.d.ts +7 -32
  30. package/dist/declarations/src/stateUtils.d.ts +22 -19
  31. package/dist/declarations/src/system.d.ts +8 -7
  32. package/dist/declarations/src/transition.d.ts +8 -5
  33. package/dist/declarations/src/transitionActions.d.ts +4 -0
  34. package/dist/declarations/src/types.d.ts +494 -429
  35. package/dist/declarations/src/types.v6.d.ts +423 -0
  36. package/dist/declarations/src/utils.d.ts +5 -3
  37. package/dist/index-219cb621.development.cjs.js +4476 -0
  38. package/dist/index-2f2fbd9b.development.esm.js +4421 -0
  39. package/dist/index-559ceab3.cjs.js +4437 -0
  40. package/dist/index-8828376f.esm.js +4382 -0
  41. package/dist/xstate-actors.cjs.js +12 -838
  42. package/dist/xstate-actors.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.cjs.js +12 -838
  44. package/dist/xstate-actors.development.cjs.mjs +10 -5
  45. package/dist/xstate-actors.development.esm.js +1 -836
  46. package/dist/xstate-actors.esm.js +1 -836
  47. package/dist/xstate-actors.umd.min.js +1 -1
  48. package/dist/xstate-actors.umd.min.js.map +1 -1
  49. package/dist/xstate-graph.cjs.js +18 -29
  50. package/dist/xstate-graph.development.cjs.js +18 -29
  51. package/dist/xstate-graph.development.esm.js +15 -26
  52. package/dist/xstate-graph.esm.js +15 -26
  53. package/dist/xstate-graph.umd.min.js +1 -1
  54. package/dist/xstate-graph.umd.min.js.map +1 -1
  55. package/dist/xstate.cjs.js +1477 -122
  56. package/dist/xstate.cjs.mjs +19 -22
  57. package/dist/xstate.development.cjs.js +1477 -122
  58. package/dist/xstate.development.cjs.mjs +19 -22
  59. package/dist/xstate.development.esm.js +1442 -86
  60. package/dist/xstate.esm.js +1442 -86
  61. package/dist/xstate.umd.min.js +1 -1
  62. package/dist/xstate.umd.min.js.map +1 -1
  63. package/package.json +3 -47
  64. package/dist/StateMachine-23c204bb.development.esm.js +0 -566
  65. package/dist/StateMachine-67a0f463.development.cjs.js +0 -569
  66. package/dist/StateMachine-94d215e3.cjs.js +0 -563
  67. package/dist/StateMachine-d1d80308.esm.js +0 -560
  68. package/dist/assign-43b1b558.development.esm.js +0 -133
  69. package/dist/assign-54b312bc.esm.js +0 -127
  70. package/dist/assign-b5bc78f7.cjs.js +0 -129
  71. package/dist/assign-f8f66278.development.cjs.js +0 -135
  72. package/dist/declarations/src/actions/assign.d.ts +0 -45
  73. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  74. package/dist/declarations/src/actions/emit.d.ts +0 -44
  75. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  76. package/dist/declarations/src/actions/log.d.ts +0 -16
  77. package/dist/declarations/src/actions/raise.d.ts +0 -21
  78. package/dist/declarations/src/actions/send.d.ts +0 -42
  79. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  80. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  81. package/dist/declarations/src/actors/transition.d.ts +0 -143
  82. package/dist/declarations/src/dev/index.d.ts +0 -14
  83. package/dist/declarations/src/guards.d.ts +0 -128
  84. package/dist/log-9b02c355.esm.js +0 -401
  85. package/dist/log-c94995c7.cjs.js +0 -409
  86. package/dist/log-d9f3f459.development.cjs.js +0 -437
  87. package/dist/log-db444cad.development.esm.js +0 -429
  88. package/dist/raise-235108c6.esm.js +0 -2762
  89. package/dist/raise-45f11c00.development.esm.js +0 -2824
  90. package/dist/raise-c445379d.cjs.js +0 -2817
  91. package/dist/raise-eca73993.development.cjs.js +0 -2879
  92. package/dist/xstate-actions.cjs.d.mts +0 -2
  93. package/dist/xstate-actions.cjs.d.ts +0 -2
  94. package/dist/xstate-actions.cjs.js +0 -23
  95. package/dist/xstate-actions.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.cjs.js +0 -23
  97. package/dist/xstate-actions.development.cjs.mjs +0 -14
  98. package/dist/xstate-actions.development.esm.js +0 -4
  99. package/dist/xstate-actions.esm.js +0 -4
  100. package/dist/xstate-actions.umd.min.js +0 -2
  101. package/dist/xstate-actions.umd.min.js.map +0 -1
  102. package/dist/xstate-dev.cjs.d.mts +0 -2
  103. package/dist/xstate-dev.cjs.d.ts +0 -2
  104. package/dist/xstate-dev.cjs.js +0 -48
  105. package/dist/xstate-dev.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.cjs.js +0 -51
  107. package/dist/xstate-dev.development.cjs.mjs +0 -5
  108. package/dist/xstate-dev.development.esm.js +0 -45
  109. package/dist/xstate-dev.esm.js +0 -42
  110. package/dist/xstate-dev.umd.min.js +0 -2
  111. package/dist/xstate-dev.umd.min.js.map +0 -1
  112. package/dist/xstate-guards.cjs.d.mts +0 -2
  113. package/dist/xstate-guards.cjs.d.ts +0 -2
  114. package/dist/xstate-guards.cjs.js +0 -14
  115. package/dist/xstate-guards.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.cjs.js +0 -14
  117. package/dist/xstate-guards.development.cjs.mjs +0 -7
  118. package/dist/xstate-guards.development.esm.js +0 -2
  119. package/dist/xstate-guards.esm.js +0 -2
  120. package/dist/xstate-guards.umd.min.js +0 -2
  121. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -1,144 +1,404 @@
1
+ import { StandardSchemaV1 } from "./schema.types.js";
1
2
  import { StateMachine } from "./StateMachine.js";
2
- import { assign } from "./actions/assign.js";
3
- import { cancel } from "./actions/cancel.js";
4
- import { emit } from "./actions/emit.js";
5
- import { enqueueActions } from "./actions/enqueueActions.js";
6
- import { log } from "./actions/log.js";
7
- import { raise } from "./actions/raise.js";
8
- import { sendTo } from "./actions/send.js";
9
- import { spawnChild } from "./actions/spawnChild.js";
10
- import { stopChild } from "./actions/stopChild.js";
11
- import { GuardPredicate } from "./guards.js";
12
- import { ActionFunction, AnyActorRef, AnyEventObject, Cast, DelayConfig, EventObject, Invert, IsNever, MachineConfig, MachineContext, MetaObject, NonReducibleUnknown, ParameterizedObject, RoutableStateId, SetupTypes, StateNodeConfig, StateSchema, ToChildren, ToStateValue, UnknownActorLogic, Values } from "./types.js";
13
- type ToParameterizedObject<TParameterizedMap extends Record<string, ParameterizedObject['params'] | undefined>> = Values<{
14
- [K in keyof TParameterizedMap as K & string]: {
15
- type: K & string;
16
- params: TParameterizedMap[K];
17
- };
18
- }>;
19
- type ToProvidedActor<TChildrenMap extends Record<string, string>, TActors extends Record<string, UnknownActorLogic>> = Values<{
20
- [K in keyof TActors as K & string]: {
21
- src: K & string;
22
- logic: TActors[K];
23
- id: IsNever<TChildrenMap> extends true ? string | undefined : K extends keyof Invert<TChildrenMap> ? Invert<TChildrenMap>[K] & string : string | undefined;
24
- };
25
- }>;
26
- type ToStateSchema<TSchema extends StateSchema> = {
27
- -readonly [K in keyof TSchema as K & ('id' | 'states')]: K extends 'states' ? {
28
- [SK in keyof TSchema['states']]: ToStateSchema<NonNullable<TSchema['states'][SK]>>;
29
- } : TSchema[K];
30
- };
31
- type RequiredSetupKeys<TChildrenMap> = IsNever<keyof TChildrenMap> extends true ? never : 'actors';
32
- type ExtractInvokeEntry<T> = T extends {
33
- id: infer TId extends string;
34
- src: infer TSrc extends string;
35
- } ? {
36
- [K in TId]: TSrc;
37
- } : {};
38
- type ExtractInvokeChildren<T> = T extends readonly (infer E)[] ? UnionToIntersection<ExtractInvokeEntry<E>> : ExtractInvokeEntry<T>;
39
- type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
40
- type ExtractConfigChildren<TConfig> = TConfig extends {
3
+ import { type Actor, type RequiredActorOptionsKeys } from "./createActor.js";
4
+ import { AnyActorRef, AnyActorLogic, ActorRefFromLogic, AnyStateNode, EventObject, AnyEventObject, EventDescriptor, ExtractEvent, MachineContext, ProvidedActor, RoutableStateId, StateSchema, StateValue, ToChildren, MetaObject, Cast, Compute, EnqueueObject, SystemRegistry, RegistryKeyForLogic, ActorOptions, Observer, Subscription, OutputArg } from "./types.js";
5
+ import { AnyActorSystem } from "./system.js";
6
+ import { InspectionEvent } from "./inspection.js";
7
+ import { DelayMapFromNames, InferChildren, Implementations, InferOutput, InferEvents, Next_MachineConfig, Next_StateNodeConfig, WithDefault } from "./types.v6.js";
8
+ export type SetupConfig<TSchemas extends SetupSchemas, TStates extends Record<string, SetupStateSchema>, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays']> = {
9
+ schemas?: TSchemas & SetupSchemas;
10
+ states?: TStates;
11
+ actions?: TActionMap;
12
+ actorSources?: TActorMap;
13
+ guards?: TGuardMap;
14
+ delays?: TDelayMap;
15
+ };
16
+ export type AnySetupConfig = SetupConfig<SetupSchemas, Record<string, SetupStateSchema>, Implementations['actions'], Implementations['actorSources'], Implementations['guards'], Implementations['delays']>;
17
+ export type SystemConfig<TSystemRegistry extends SystemRegistry> = {
18
+ registry?: TSystemRegistry;
19
+ };
20
+ export type SystemActorMap<TSystemRegistry extends SystemRegistry> = {
21
+ [K in keyof TSystemRegistry & string]: ActorRefFromLogic<TSystemRegistry[K]>;
22
+ };
23
+ export type SystemRuntime<TSystemRegistry extends SystemRegistry> = Omit<AnyActorSystem, 'get' | 'getAll'> & {
24
+ get<K extends keyof SystemActorMap<TSystemRegistry> & string>(key: K): SystemActorMap<TSystemRegistry>[K] | undefined;
25
+ getAll(): Partial<SystemActorMap<TSystemRegistry>>;
26
+ };
27
+ type LogicMatchesRegistryKey<TLogic, TSystemLogic> = TLogic extends AnyActorLogic ? TSystemLogic extends AnyActorLogic ? [TLogic] extends [TSystemLogic] ? true : [TSystemLogic] extends [TLogic] ? true : ActorRefFromLogic<TLogic> extends ActorRefFromLogic<TSystemLogic> ? true : false : false : false;
28
+ type RegistryKeyMatchesSrc<TKey extends string, TSrc, TSystemRegistry extends SystemRegistry, TActorMap extends Implementations['actorSources']> = TKey extends keyof TSystemRegistry & string ? TSrc extends keyof TActorMap & string ? LogicMatchesRegistryKey<TActorMap[TSrc], TSystemRegistry[TKey]> : TSrc extends AnyActorLogic ? LogicMatchesRegistryKey<TSrc, TSystemRegistry[TKey]> : true : false;
29
+ type ValidateSystemInvoke<TInvoke, TSystemRegistry extends SystemRegistry, TActorMap extends Implementations['actorSources']> = TInvoke extends readonly unknown[] ? {
30
+ [K in keyof TInvoke]: TInvoke[K] & ValidateSystemInvoke<TInvoke[K], TSystemRegistry, TActorMap>;
31
+ } : TInvoke extends {
32
+ registryKey: infer TKey;
33
+ } ? TKey extends string ? TInvoke extends {
34
+ src: infer TSrc;
35
+ } ? RegistryKeyMatchesSrc<TKey, TSrc, TSystemRegistry, TActorMap> extends true ? unknown : {
36
+ registryKey: never;
37
+ } : TKey extends keyof TSystemRegistry & string ? unknown : {
38
+ registryKey: never;
39
+ } : {
40
+ registryKey: never;
41
+ } : unknown;
42
+ type ValidateRegistryKeys<TConfig, TSystemRegistry extends SystemRegistry, TActorMap extends Implementations['actorSources']> = string extends keyof TSystemRegistry ? unknown : (TConfig extends {
41
43
  invoke: infer TInvoke;
42
- } ? ExtractInvokeChildren<TInvoke> : {};
43
- type MergeChildrenMap<TExplicit extends Record<string, string>, TInferred extends Record<string, string>> = IsNever<keyof TExplicit> extends true ? TInferred : TExplicit;
44
- export type SetupReturn<TContext extends MachineContext, TEvent extends AnyEventObject, TActors extends Record<string, UnknownActorLogic>, TChildrenMap extends Record<string, string>, TActions extends Record<string, ParameterizedObject['params'] | undefined>, TGuards extends Record<string, ParameterizedObject['params'] | undefined>, TDelay extends string, TTag extends string, TInput, TOutput extends NonReducibleUnknown, TEmitted extends EventObject, TMeta extends MetaObject> = {
45
- extend: <TExtendActions extends Record<string, ParameterizedObject['params'] | undefined> = {}, TExtendGuards extends Record<string, ParameterizedObject['params'] | undefined> = {}, TExtendDelays extends string = never>({ actions, guards, delays }: {
46
- actions?: {
47
- [K in keyof TExtendActions]: ActionFunction<TContext, TEvent, TEvent, TExtendActions[K], ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions & TExtendActions>, ToParameterizedObject<TGuards & TExtendGuards>, TDelay | TExtendDelays, TEmitted>;
48
- };
49
- guards?: {
50
- [K in keyof TExtendGuards]: GuardPredicate<TContext, TEvent, TExtendGuards[K], ToParameterizedObject<TGuards & TExtendGuards>>;
51
- };
52
- delays?: {
53
- [K in TExtendDelays]: DelayConfig<TContext, TEvent, ToParameterizedObject<TActions & TExtendActions>['params'], TEvent>;
54
- };
55
- }) => SetupReturn<TContext, TEvent, TActors, TChildrenMap, TActions & TExtendActions, TGuards & TExtendGuards, TDelay | TExtendDelays, TTag, TInput, TOutput, TEmitted, TMeta>;
56
- /**
57
- * Creates a state config that is strongly typed. This state config can be
58
- * used to create a machine.
59
- *
60
- * @example
61
- *
62
- * ```ts
63
- * const lightMachineSetup = setup({
64
- * // ...
65
- * });
66
- *
67
- * const green = lightMachineSetup.createStateConfig({
68
- * on: {
69
- * timer: {
70
- * actions: 'doSomething'
71
- * }
72
- * }
73
- * });
74
- *
75
- * const machine = lightMachineSetup.createMachine({
76
- * initial: 'green',
77
- * states: {
78
- * green,
79
- * yellow,
80
- * red
81
- * }
82
- * });
83
- * ```
84
- */
85
- createStateConfig: <TStateConfig extends StateNodeConfig<TContext, TEvent, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TTag, unknown, TEmitted, TMeta>>(config: TStateConfig) => TStateConfig;
86
- /**
87
- * Creates a type-safe action.
88
- *
89
- * @example
90
- *
91
- * ```ts
92
- * const machineSetup = setup({
93
- * // ...
94
- * });
95
- *
96
- * const action = machineSetup.createAction(({ context, event }) => {
97
- * console.log(context.count, event.value);
98
- * });
99
- *
100
- * const incrementAction = machineSetup.createAction(
101
- * assign({ count: ({ context }) => context.count + 1 })
102
- * );
103
- *
104
- * const machine = machineSetup.createMachine({
105
- * context: { count: 0 },
106
- * entry: [action, incrementAction]
107
- * });
108
- * ```
109
- */
110
- createAction: (action: ActionFunction<TContext, TEvent, TEvent, unknown, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TEmitted>) => typeof action;
111
- createMachine: <const TConfig extends MachineConfig<TContext, TEvent, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TTag, TInput, TOutput, TEmitted, TMeta>, TResolvedChildren extends Record<string, string> = MergeChildrenMap<TChildrenMap, Cast<ExtractConfigChildren<TConfig>, Record<string, string>>>>(config: TConfig) => StateMachine<TContext, TEvent | ([RoutableStateId<TConfig>] extends [never] ? never : {
112
- type: 'xstate.route';
113
- to: RoutableStateId<TConfig>;
114
- }), Cast<ToChildren<ToProvidedActor<TResolvedChildren, TActors>>, Record<string, AnyActorRef | undefined>>, ToProvidedActor<TResolvedChildren, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, ToStateValue<TConfig>, TTag, TInput, TOutput, TEmitted, TMeta, ToStateSchema<TConfig>>;
115
- assign: typeof assign<TContext, TEvent, undefined, TEvent, ToProvidedActor<TChildrenMap, TActors>>;
116
- sendTo: <TTargetActor extends AnyActorRef>(...args: Parameters<typeof sendTo<TContext, TEvent, undefined, TTargetActor, TEvent, TDelay, TDelay>>) => ReturnType<typeof sendTo<TContext, TEvent, undefined, TTargetActor, TEvent, TDelay, TDelay>>;
117
- raise: typeof raise<TContext, TEvent, TEvent, undefined, TDelay, TDelay>;
118
- log: typeof log<TContext, TEvent, undefined, TEvent>;
119
- cancel: typeof cancel<TContext, TEvent, undefined, TEvent>;
120
- stopChild: typeof stopChild<TContext, TEvent, undefined, TEvent>;
121
- enqueueActions: typeof enqueueActions<TContext, TEvent, undefined, TEvent, ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TEmitted>;
122
- emit: typeof emit<TContext, TEvent, undefined, TEvent, TEmitted>;
123
- spawnChild: typeof spawnChild<TContext, TEvent, undefined, TEvent, ToProvidedActor<TChildrenMap, TActors>>;
124
- };
125
- export declare function setup<TContext extends MachineContext, TEvent extends AnyEventObject, // TODO: consider using a stricter `EventObject` here
126
- TActors extends Record<string, UnknownActorLogic> = {}, TChildrenMap extends Record<string, string> = {}, TActions extends Record<string, ParameterizedObject['params'] | undefined> = {}, TGuards extends Record<string, ParameterizedObject['params'] | undefined> = {}, TDelay extends string = never, TTag extends string = string, TInput = NonReducibleUnknown, TOutput extends NonReducibleUnknown = NonReducibleUnknown, TEmitted extends EventObject = EventObject, TMeta extends MetaObject = MetaObject>({ schemas, actors, actions, guards, delays }: {
127
- schemas?: unknown;
128
- types?: SetupTypes<TContext, TEvent, TChildrenMap, TTag, TInput, TOutput, TEmitted, TMeta>;
129
- actors?: {
130
- [K in keyof TActors | Values<TChildrenMap>]: K extends keyof TActors ? TActors[K] : never;
44
+ } ? {
45
+ invoke: TInvoke & ValidateSystemInvoke<TInvoke, TSystemRegistry, TActorMap>;
46
+ } : unknown) & (TConfig extends {
47
+ states: infer TStates;
48
+ } ? {
49
+ states: {
50
+ [K in keyof TStates]: TStates[K] & ValidateRegistryKeys<TStates[K], TSystemRegistry, TActorMap>;
51
+ };
52
+ } : unknown);
53
+ export type SetupStateSchemas = {
54
+ context?: StandardSchemaV1;
55
+ input?: StandardSchemaV1;
56
+ };
57
+ export type SetupSchemas = {
58
+ context?: StandardSchemaV1;
59
+ events?: Record<string, StandardSchemaV1>;
60
+ emitted?: Record<string, StandardSchemaV1>;
61
+ input?: StandardSchemaV1;
62
+ output?: StandardSchemaV1;
63
+ meta?: StandardSchemaV1;
64
+ tags?: StandardSchemaV1;
65
+ children?: Record<string, StandardSchemaV1>;
66
+ };
67
+ /** State schema with optional schemas.input and nested states */
68
+ export interface SetupStateSchema {
69
+ schemas?: SetupStateSchemas;
70
+ states?: Record<string, SetupStateSchema>;
71
+ }
72
+ type SetupSchema<TSchemas, TKey extends keyof SetupSchemas> = TKey extends keyof TSchemas ? TSchemas[TKey] extends StandardSchemaV1 ? TSchemas[TKey] : never : never;
73
+ type SetupSchemaMap<TSchemas, TKey extends 'events' | 'emitted' | 'children'> = TKey extends keyof TSchemas ? TSchemas[TKey] extends Record<string, StandardSchemaV1> ? TSchemas[TKey] : never : never;
74
+ type SetupOrConfigSchema<TSchemas, TKey extends Exclude<keyof SetupSchemas, 'events' | 'emitted'>, TConfigSchema extends StandardSchemaV1> = [SetupSchema<TSchemas, TKey>] extends [never] ? TConfigSchema : SetupSchema<TSchemas, TKey>;
75
+ type SetupOrConfigSchemaMap<TSchemas, TKey extends 'events' | 'emitted' | 'children', TConfigSchemaMap extends Record<string, StandardSchemaV1>> = [SetupSchemaMap<TSchemas, TKey>] extends [never] ? TConfigSchemaMap : SetupSchemaMap<TSchemas, TKey>;
76
+ type SetupStateKeys<TStateSchemas extends Record<string, SetupStateSchema>> = keyof TStateSchemas & string;
77
+ type SetupStateKey<TStateSchemas extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStateSchemas> ? string : [SetupStateKeys<TStateSchemas>] extends [never] ? string : SetupStateKeys<TStateSchemas>;
78
+ type SetupStateTarget<TStateSchemas extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStateSchemas> ? string : [SetupStateKeys<TStateSchemas>] extends [never] ? string : SetupStateKeys<TStateSchemas> | `.${string}` | `#${string}`;
79
+ type InvalidSetupStateKeys<TConfig, TStateSchemas extends Record<string, SetupStateSchema>> = string extends SetupStateKeys<TStateSchemas> ? never : [SetupStateKeys<TStateSchemas>] extends [never] ? never : TConfig extends {
80
+ states: infer TStates;
81
+ } ? Exclude<keyof TStates & string, SetupStateKeys<TStateSchemas>> : never;
82
+ type ValidateSetupStateKeys<TConfig, TStateSchemas extends Record<string, SetupStateSchema>> = [InvalidSetupStateKeys<TConfig, TStateSchemas>] extends [never] ? unknown : {
83
+ states: {
84
+ [K in InvalidSetupStateKeys<TConfig, TStateSchemas>]: never;
131
85
  };
132
- actions?: {
133
- [K in keyof TActions]: ActionFunction<TContext, TEvent, TEvent, TActions[K], ToProvidedActor<TChildrenMap, TActors>, ToParameterizedObject<TActions>, ToParameterizedObject<TGuards>, TDelay, TEmitted>;
86
+ };
87
+ type ValidateNestedSetupStateKeys<TConfig, TStateSchemas extends Record<string, SetupStateSchema>> = TConfig extends {
88
+ states: infer TConfigStates;
89
+ } ? TConfigStates extends Record<string, unknown> ? {
90
+ states: {
91
+ [K in keyof TConfigStates & keyof TStateSchemas & string]: TStateSchemas[K] extends {
92
+ states: infer TChildStateSchemas;
93
+ } ? TChildStateSchemas extends Record<string, SetupStateSchema> ? TConfigStates[K] & ValidateSetupStateKeys<TConfigStates[K], TChildStateSchemas> & ValidateNestedSetupStateKeys<TConfigStates[K], TChildStateSchemas> : TConfigStates[K] : TConfigStates[K];
134
94
  };
135
- guards?: {
136
- [K in keyof TGuards]: GuardPredicate<TContext, TEvent, TGuards[K], ToParameterizedObject<TGuards>>;
95
+ } : unknown : unknown;
96
+ type SetupContext<TSchemas, TContextSchema extends StandardSchemaV1> = [
97
+ SetupSchema<TSchemas, 'context'>
98
+ ] extends [never] ? unknown extends StandardSchemaV1.InferOutput<TContextSchema> ? MachineContext : StandardSchemaV1.InferOutput<TContextSchema> & MachineContext : StandardSchemaV1.InferOutput<SetupSchema<TSchemas, 'context'>> & MachineContext;
99
+ type SetupContextShape<TSchemas, TContextSchema extends StandardSchemaV1, TFallbackContext> = [SetupSchema<TSchemas, 'context'>] extends [never] ? unknown extends StandardSchemaV1.InferOutput<TContextSchema> ? TFallbackContext : StandardSchemaV1.InferOutput<TContextSchema> : StandardSchemaV1.InferOutput<SetupSchema<TSchemas, 'context'>>;
100
+ type SetupContextRequired<TSchemas, TContextSchema extends StandardSchemaV1> = [
101
+ SetupSchema<TSchemas, 'context'>
102
+ ] extends [never] ? unknown extends StandardSchemaV1.InferOutput<TContextSchema> ? false : true : true;
103
+ type SetupEvents<TSchemas, TEventSchemaMap extends Record<string, StandardSchemaV1>> = [SetupSchemaMap<TSchemas, 'events'>] extends [never] ? InferEvents<TEventSchemaMap> : InferEvents<SetupSchemaMap<TSchemas, 'events'>>;
104
+ type SetupTags<TSchemas, TTagSchema extends StandardSchemaV1> = [
105
+ SetupSchema<TSchemas, 'tags'>
106
+ ] extends [never] ? StandardSchemaV1.InferOutput<TTagSchema> & string : StandardSchemaV1.InferOutput<SetupSchema<TSchemas, 'tags'>> & string;
107
+ type SetupInput<TSchemas, TInputSchema extends StandardSchemaV1> = [
108
+ SetupSchema<TSchemas, 'input'>
109
+ ] extends [never] ? InferOutput<TInputSchema, unknown> : InferOutput<SetupSchema<TSchemas, 'input'>, unknown>;
110
+ type SetupOutput<TSchemas, TOutputSchema extends StandardSchemaV1> = [
111
+ SetupSchema<TSchemas, 'output'>
112
+ ] extends [never] ? InferOutput<TOutputSchema, unknown> : InferOutput<SetupSchema<TSchemas, 'output'>, unknown>;
113
+ type SetupEmitted<TSchemas, TEmittedSchemaMap extends Record<string, StandardSchemaV1>> = [SetupSchemaMap<TSchemas, 'emitted'>] extends [never] ? WithDefault<InferEvents<TEmittedSchemaMap>, AnyEventObject> : WithDefault<InferEvents<SetupSchemaMap<TSchemas, 'emitted'>>, AnyEventObject>;
114
+ type SetupMeta<TSchemas, TMetaSchema extends StandardSchemaV1> = [
115
+ SetupSchema<TSchemas, 'meta'>
116
+ ] extends [never] ? InferOutput<TMetaSchema, MetaObject> : InferOutput<SetupSchema<TSchemas, 'meta'>, MetaObject>;
117
+ type SetupChildren<TSchemas, TChildrenSchemaMap extends Record<string, StandardSchemaV1>> = [SetupSchemaMap<TSchemas, 'children'>] extends [never] ? InferChildren<TChildrenSchemaMap> : InferChildren<SetupSchemaMap<TSchemas, 'children'>>;
118
+ type MergeChildren<TChildren extends Record<string, AnyActorRef | undefined>, TActor extends ProvidedActor> = [keyof TChildren] extends [never] ? Compute<ToChildren<TActor>> : Compute<TChildren>;
119
+ type MergeImplementationMaps<TBase extends Record<string, unknown>, TExtension extends Record<string, unknown>> = Compute<TBase & TExtension>;
120
+ type DelayNamesFromConfigOrString<TConfig> = TConfig extends {
121
+ delays: infer TDelays;
122
+ } ? Extract<keyof TDelays, string> : string;
123
+ type DelayNamesFromConfig<TConfig> = TConfig extends {
124
+ delays: infer TDelays;
125
+ } ? Extract<keyof TDelays, string> : never;
126
+ type InvalidDelayReferences<TConfig, TDelays extends string> = (TConfig extends {
127
+ after: infer TAfter;
128
+ } ? Exclude<Extract<keyof TAfter, string>, TDelays> : never) | (TConfig extends {
129
+ timeout: infer TTimeout;
130
+ } ? TTimeout extends string ? TTimeout extends TDelays ? never : TTimeout : never : never) | (TConfig extends {
131
+ states: infer TStates;
132
+ } ? TStates extends Record<string, unknown> ? {
133
+ [K in keyof TStates]: InvalidDelayReferences<TStates[K], TDelays>;
134
+ }[keyof TStates] : never : never);
135
+ type ValidateSetupDelayReferences<TConfig, TSetupDelays extends string> = string extends ([
136
+ TSetupDelays
137
+ ] extends [never] ? DelayNamesFromConfigOrString<TConfig> : TSetupDelays | DelayNamesFromConfig<TConfig>) ? unknown : InvalidDelayReferences<TConfig, [
138
+ TSetupDelays
139
+ ] extends [never] ? DelayNamesFromConfigOrString<TConfig> : TSetupDelays | DelayNamesFromConfig<TConfig>> extends never ? unknown : never;
140
+ /** Extracts input type from a state schema */
141
+ type StateInput<TStateSchema extends SetupStateSchema> = TStateSchema['schemas'] extends {
142
+ input: infer TInputSchema;
143
+ } ? TInputSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TInputSchema> : undefined : undefined;
144
+ type StateContext<TStateSchema extends SetupStateSchema, TFallbackContext extends MachineContext> = TStateSchema['schemas'] extends {
145
+ context: infer TContextSchema;
146
+ } ? TContextSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TContextSchema> & MachineContext : TFallbackContext : TFallbackContext;
147
+ type StateContextShape<TStateSchema extends SetupStateSchema, TFallbackContext> = TStateSchema['schemas'] extends {
148
+ context: infer TContextSchema;
149
+ } ? TContextSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TContextSchema> : TFallbackContext : TFallbackContext;
150
+ type WithNestedStates<TConfig, TNestedStates> = TConfig extends {
151
+ type: 'choice';
152
+ } ? TConfig : Omit<TConfig, 'states'> & {
153
+ states?: TNestedStates;
154
+ };
155
+ /**
156
+ * Converts SetupStateSchema to StateSchema with input types included. This
157
+ * allows getInputs() to be strongly typed.
158
+ */
159
+ type SetupStateSchemaToStateSchema<TSetupSchema extends SetupStateSchema> = {
160
+ input: StateInput<TSetupSchema>;
161
+ contextSchema: TSetupSchema['schemas'] extends {
162
+ context: infer TContextSchema;
163
+ } ? TContextSchema extends StandardSchemaV1 ? TContextSchema : undefined : undefined;
164
+ states: TSetupSchema['states'] extends Record<string, SetupStateSchema> ? {
165
+ [K in keyof TSetupSchema['states'] & string]: SetupStateSchemaToStateSchema<TSetupSchema['states'][K]>;
166
+ } : undefined;
167
+ };
168
+ /** Converts the root setup states config to a StateSchema. */
169
+ type SetupStatesToStateSchema<TStates extends Record<string, SetupStateSchema>> = {
170
+ states: {
171
+ [K in keyof TStates & string]: SetupStateSchemaToStateSchema<TStates[K]>;
137
172
  };
173
+ };
174
+ type EmptyStateSchema = {
175
+ input: undefined;
176
+ states: undefined;
177
+ };
178
+ type StateSchemaInput<TConfig extends StateSchema, TSetup extends StateSchema> = TSetup extends {
179
+ input: infer TInput;
180
+ } ? TInput : TConfig extends {
181
+ input: infer TInput;
182
+ } ? TInput : undefined;
183
+ type StateSchemaContextSchema<TConfig extends StateSchema, TSetup extends StateSchema> = TSetup extends {
184
+ contextSchema: infer TContextSchema;
185
+ } ? TContextSchema extends StandardSchemaV1 ? TContextSchema : undefined : TConfig extends {
186
+ contextSchema: infer TContextSchema;
187
+ } ? TContextSchema extends StandardSchemaV1 ? TContextSchema : undefined : undefined;
188
+ type StateSchemaChild<TSetup extends StateSchema, K extends string> = TSetup extends {
189
+ states: infer TStates;
190
+ } ? K extends keyof TStates ? Cast<TStates[K], StateSchema> : EmptyStateSchema : EmptyStateSchema;
191
+ type MergeStateSchema<TConfig extends StateSchema, TSetup extends StateSchema> = Omit<TConfig, 'contextSchema' | 'input' | 'states'> & {
192
+ contextSchema: StateSchemaContextSchema<TConfig, TSetup>;
193
+ input: StateSchemaInput<TConfig, TSetup>;
194
+ states: TConfig extends {
195
+ states: infer TStates;
196
+ } ? TStates extends Record<string, StateSchema> ? {
197
+ [K in keyof TStates & string]: MergeStateSchema<TStates[K], StateSchemaChild<TSetup, K>>;
198
+ } : undefined : undefined;
199
+ };
200
+ /** Machine config with typed state input */
201
+ type SetupMachineConfig<TStateSchemas extends Record<string, SetupStateSchema>, TSchemas extends SetupSchemas, TContextSchema extends StandardSchemaV1, TEventSchemaMap extends Record<string, StandardSchemaV1>, TEmittedSchemaMap extends Record<string, StandardSchemaV1>, TInputSchema extends StandardSchemaV1, TOutputSchema extends StandardSchemaV1, TMetaSchema extends StandardSchemaV1, TTagSchema extends StandardSchemaV1, TChildrenSchemaMap extends Record<string, StandardSchemaV1>, TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TDelays extends string, TTag extends string, TEmitted extends EventObject, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry, TContextRequired extends boolean, TRootDelays extends string = TDelays, TRootActionMap extends Implementations['actions'] = TActionMap, TRootActorMap extends Implementations['actorSources'] = TActorMap, TRootGuardMap extends Implementations['guards'] = TGuardMap> = Omit<Next_MachineConfig<SetupOrConfigSchema<TSchemas, 'context', TContextSchema>, SetupOrConfigSchemaMap<TSchemas, 'events', TEventSchemaMap>, SetupOrConfigSchemaMap<TSchemas, 'emitted', TEmittedSchemaMap>, SetupOrConfigSchema<TSchemas, 'input', TInputSchema>, SetupOrConfigSchema<TSchemas, 'output', TOutputSchema>, SetupOrConfigSchema<TSchemas, 'meta', TMetaSchema>, SetupOrConfigSchema<TSchemas, 'tags', TTagSchema>, SetupOrConfigSchemaMap<TSchemas, 'children', TChildrenSchemaMap>, TContext, TEvent, TChildren, TDelays, TTag, TActionMap, TActorMap, TGuardMap, TDelayMap, TContextRequired, TSystemRegistry>, 'states' | 'initial' | 'on' | 'always' | 'actions' | 'actorSources' | 'guards' | 'delays'> & {
202
+ actions?: TRootActionMap;
203
+ actorSources?: TRootActorMap;
204
+ guards?: TRootGuardMap;
138
205
  delays?: {
139
- [K in TDelay]: DelayConfig<TContext, TEvent, ToParameterizedObject<TActions>['params'], TEvent>;
206
+ [K in TRootDelays | number]?: number | (({ context, event, stateNode }: {
207
+ context: TContext;
208
+ event: TEvent;
209
+ stateNode: AnyStateNode;
210
+ }) => number);
140
211
  };
141
- } & {
142
- [K in RequiredSetupKeys<TChildrenMap>]: unknown;
143
- }): SetupReturn<TContext, TEvent, TActors, TChildrenMap, TActions, TGuards, TDelay, TTag, TInput, TOutput, TEmitted, TMeta>;
212
+ initial?: SetupStateKey<TStateSchemas> | InitialTransitionWithInput<TStateSchemas, TContext, TEvent> | undefined;
213
+ on?: StateTransitions<TStateSchemas, TContext, SetupContextShape<TSchemas, TContextSchema, TContext>, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
214
+ always?: StateTransitionConfigOrTarget<TStateSchemas, TContext, SetupContextShape<TSchemas, TContextSchema, TContext>, TEvent, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
215
+ states?: StatesWithInput<TStateSchemas, TStateSchemas, TContext, SetupContextShape<TSchemas, TContextSchema, TContext>, TEvent, TChildren, TDelays, TTag, TEmitted, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
216
+ };
217
+ /** States config type that provides typed input for known states */
218
+ type StatesWithInput<TRootStateSchemas extends Record<string, SetupStateSchema>, TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TDelays extends string, TTag extends string, TEmitted extends EventObject, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = {
219
+ [K in keyof TStateSchemas & string]?: StateNodeConfigWithNestedInput<TRootStateSchemas, TStateSchemas[K], TContext, TContextShape, TEvent, TChildren, TDelays, TTag, TEmitted, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
220
+ };
221
+ /** State node config that recursively applies typed input for nested states */
222
+ type StateNodeConfigWithNestedInput<TSiblingStateSchemas extends Record<string, SetupStateSchema>, TStateSchema extends SetupStateSchema, TContext extends MachineContext, TContextShape, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TDelays extends string, TTag extends string, TEmitted extends EventObject, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = WithNestedStates<Omit<Next_StateNodeConfig<StateContext<TStateSchema, TContext>, TEvent, TDelays, TTag, any, TEmitted, TMeta, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, StateInput<TStateSchema>, Record<string, unknown>, TSystemRegistry>, 'on' | 'always' | 'initial'> & {
223
+ initial?: TStateSchema['states'] extends Record<string, SetupStateSchema> ? SetupStateKey<TStateSchema['states']> | InitialTransitionWithInput<TStateSchema['states'], StateContext<TStateSchema, TContext>, TEvent> : string | {
224
+ target: string;
225
+ input?: Record<string, unknown>;
226
+ } | undefined;
227
+ on?: StateTransitions<TSiblingStateSchemas, StateContext<TStateSchema, TContext>, StateContextShape<TStateSchema, TContextShape>, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
228
+ always?: StateTransitionConfigOrTarget<TSiblingStateSchemas, StateContext<TStateSchema, TContext>, StateContextShape<TStateSchema, TContextShape>, TEvent, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
229
+ }, TStateSchema['states'] extends Record<string, SetupStateSchema> ? StatesWithInput<TStateSchema['states'], TStateSchema['states'], TContext, StateContextShape<TStateSchema, TContextShape>, TEvent, TChildren, TDelays, TTag, TEmitted, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry> : {
230
+ [K in string]?: Next_StateNodeConfig<TContext, TEvent, TDelays, TTag, any, TEmitted, TMeta, TChildren, TActionMap, TActorMap, TGuardMap, TDelayMap, undefined, Record<string, unknown>, TSystemRegistry>;
231
+ }>;
232
+ type StateTransitions<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = {
233
+ [K in EventDescriptor<TEvent>]?: StateTransitionConfigOrTarget<TStateSchemas, TContext, TContextShape, ExtractEvent<TEvent, K>, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
234
+ };
235
+ type StateTransitionConfigOrTarget<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = undefined | StateTransitionObjectConfig<TStateSchemas, TContext, TContextShape, TExpressionEvent, TMeta> | StateTransitionFunction<TStateSchemas, TContext, TContextShape, TExpressionEvent, TEvent, TEmitted, TChildren, TMeta, TActionMap, TActorMap, TGuardMap, TDelayMap, TSystemRegistry>;
236
+ type StateTransitionObjectConfig<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TMeta extends MetaObject> = (StateTransitionResult<TStateSchemas, TContext, TContextShape, TMeta> & {
237
+ description?: string;
238
+ }) | {
239
+ target: SetupStateTarget<TStateSchemas>[];
240
+ context?: ContextPatch<TContextShape, TContextShape, TContext>;
241
+ description?: string;
242
+ reenter?: boolean;
243
+ meta?: TMeta;
244
+ input?: Record<string, unknown> | ((args: {
245
+ context: TContext;
246
+ event: TExpressionEvent;
247
+ } & OutputArg<TExpressionEvent>) => Record<string, unknown>);
248
+ };
249
+ type StateTransitionFunction<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TExpressionEvent extends EventObject, TEvent extends EventObject, TEmitted extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TMeta extends MetaObject, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actorSources'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays'], TSystemRegistry extends SystemRegistry> = (args: {
250
+ context: TContext;
251
+ event: TExpressionEvent;
252
+ self: AnyActorRef;
253
+ parent: AnyActorRef | undefined;
254
+ value: StateValue;
255
+ children: TChildren;
256
+ system: SystemRuntime<TSystemRegistry>;
257
+ actions: TActionMap;
258
+ actorSources: TActorMap;
259
+ guards: TGuardMap;
260
+ delays: TDelayMap;
261
+ } & OutputArg<TExpressionEvent>, enq: EnqueueObject<TEvent, TEmitted, TSystemRegistry>) => StateTransitionResult<TStateSchemas, TContext, TContextShape, TMeta> | void;
262
+ type StateTransitionResult<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TContextShape, TMeta extends MetaObject> = {
263
+ target?: never;
264
+ context?: ContextPatch<TContextShape, TContextShape, TContext>;
265
+ reenter?: boolean;
266
+ meta?: TMeta;
267
+ } | {
268
+ [K in keyof TStateSchemas & string]: {
269
+ target: K;
270
+ reenter?: boolean;
271
+ meta?: TMeta;
272
+ input?: StateInput<TStateSchemas[K]> | ((args: {
273
+ context: TContext;
274
+ event: EventObject;
275
+ } & OutputArg<EventObject>) => StateInput<TStateSchemas[K]>);
276
+ } & ([TContextShape] extends [
277
+ StateContextShape<TStateSchemas[K], TContextShape>
278
+ ] ? {
279
+ context?: ContextPatch<TContextShape, StateContextShape<TStateSchemas[K], TContextShape>, StateContext<TStateSchemas[K], TContext>>;
280
+ } : {
281
+ context: ContextPatch<TContextShape, StateContextShape<TStateSchemas[K], TContextShape>, StateContext<TStateSchemas[K], TContext>>;
282
+ });
283
+ }[keyof TStateSchemas & string] | {
284
+ target: Exclude<SetupStateTarget<TStateSchemas>, keyof TStateSchemas & string>;
285
+ context?: ContextPatch<TContextShape, TContextShape, TContext>;
286
+ reenter?: boolean;
287
+ meta?: TMeta;
288
+ };
289
+ type ContextPatch<TCurrentContext, TTargetContext, TResolvedTargetContext extends MachineContext> = Compute<Partial<TResolvedTargetContext> & Pick<TResolvedTargetContext, Extract<RequiredContextKeys<TCurrentContext, TTargetContext>, string>>>;
290
+ type RequiredContextKeys<TCurrentContext, TTargetContext> = {
291
+ [K in keyof TTargetContext]-?: K extends keyof TCurrentContext ? [TCurrentContext[K]] extends [TTargetContext[K]] ? never : K : K;
292
+ }[keyof TTargetContext];
293
+ /** Initial transition with typed input based on target state */
294
+ type InitialTransitionWithInput<TStateSchemas extends Record<string, SetupStateSchema>, TContext extends MachineContext, TEvent extends EventObject> = {
295
+ [K in keyof TStateSchemas & string]: {
296
+ target: K;
297
+ input?: StateInput<TStateSchemas[K]> | ((args: {
298
+ context: TContext;
299
+ event: TEvent;
300
+ }) => StateInput<TStateSchemas[K]>);
301
+ };
302
+ }[keyof TStateSchemas & string];
303
+ /** Return type of setup() */
304
+ export interface SetupReturn<TStates extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>, TSchemas extends SetupSchemas = {}, TSetupActionMap extends Implementations['actions'] = {}, TSetupActorMap extends Implementations['actorSources'] = {}, TSetupGuardMap extends Implementations['guards'] = {}, TSetupDelayMap extends Implementations['delays'] = {}, TSetupDelays extends string = Extract<keyof TSetupDelayMap, string>, TSystemRegistry extends SystemRegistry = SystemRegistry> {
305
+ /** Extends the setup configuration */
306
+ extend<const TExtendSchemas extends SetupSchemas = {}, const TExtendStates extends Record<string, SetupStateSchema> = {}, TExtendActionMap extends Implementations['actions'] = {}, TExtendActorMap extends Implementations['actorSources'] = {}, TExtendGuardMap extends Implementations['guards'] = {}, TExtendDelayMap extends Implementations['delays'] = {}>(config: SetupConfig<TExtendSchemas, TExtendStates, TExtendActionMap, TExtendActorMap, TExtendGuardMap, TExtendDelayMap>): SetupReturn<MergeImplementationMaps<TStates, TExtendStates>, MergeImplementationMaps<TSchemas, TExtendSchemas>, MergeImplementationMaps<TSetupActionMap, TExtendActionMap>, MergeImplementationMaps<TSetupActorMap, TExtendActorMap>, MergeImplementationMaps<TSetupGuardMap, TExtendGuardMap>, MergeImplementationMaps<TSetupDelayMap, TExtendDelayMap>, TSetupDelays | Extract<keyof TExtendDelayMap, string>, TSystemRegistry>;
307
+ /** Creates a state machine with the setup configuration */
308
+ createMachine<TContextSchema extends StandardSchemaV1 = StandardSchemaV1, const TEventSchemaMap extends Record<string, StandardSchemaV1> = Record<string, StandardSchemaV1>, TEmittedSchemaMap extends Record<string, StandardSchemaV1> = Record<string, StandardSchemaV1>, TInputSchema extends StandardSchemaV1 = StandardSchemaV1, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1, TMetaSchema extends StandardSchemaV1 = StandardSchemaV1, TTagSchema extends StandardSchemaV1 = StandardSchemaV1, const TChildrenSchemaMap extends Record<string, StandardSchemaV1> = Record<string, StandardSchemaV1>, _TEvent extends EventObject = EventObject, TActor extends ProvidedActor = ProvidedActor, TActionMap extends Implementations['actions'] = {}, TActorMap extends Implementations['actorSources'] = {}, TGuardMap extends Implementations['guards'] = {}, TDelayMap extends Implementations['delays'] = {}, TDelays extends string = Extract<keyof TDelayMap, string>, TTag extends SetupTags<TSchemas, TTagSchema> = SetupTags<TSchemas, TTagSchema>, TInput = unknown, TConfig extends SetupMachineConfig<TStates, TSchemas, TContextSchema, TEventSchemaMap, TEmittedSchemaMap, TInputSchema, TOutputSchema, TMetaSchema, TTagSchema, TChildrenSchemaMap, SetupContext<TSchemas, TContextSchema>, SetupEvents<TSchemas, TEventSchemaMap>, Cast<MergeChildren<SetupChildren<TSchemas, TChildrenSchemaMap>, TActor>, Record<string, AnyActorRef | undefined>>, TSetupDelays | TDelays, TTag, SetupEmitted<TSchemas, TEmittedSchemaMap>, SetupMeta<TSchemas, TMetaSchema>, MergeImplementationMaps<TSetupActionMap, TActionMap>, MergeImplementationMaps<TSetupActorMap, TActorMap>, MergeImplementationMaps<TSetupGuardMap, TGuardMap>, MergeImplementationMaps<TSetupDelayMap, TDelayMap>, TSystemRegistry, SetupContextRequired<TSchemas, TContextSchema>, TDelays, TActionMap, TActorMap, TGuardMap> = SetupMachineConfig<TStates, TSchemas, TContextSchema, TEventSchemaMap, TEmittedSchemaMap, TInputSchema, TOutputSchema, TMetaSchema, TTagSchema, TChildrenSchemaMap, SetupContext<TSchemas, TContextSchema>, SetupEvents<TSchemas, TEventSchemaMap>, Cast<MergeChildren<SetupChildren<TSchemas, TChildrenSchemaMap>, TActor>, Record<string, AnyActorRef | undefined>>, TSetupDelays | TDelays, TTag, SetupEmitted<TSchemas, TEmittedSchemaMap>, SetupMeta<TSchemas, TMetaSchema>, MergeImplementationMaps<TSetupActionMap, TActionMap>, MergeImplementationMaps<TSetupActorMap, TActorMap>, MergeImplementationMaps<TSetupGuardMap, TGuardMap>, MergeImplementationMaps<TSetupDelayMap, TDelayMap>, TSystemRegistry, SetupContextRequired<TSchemas, TContextSchema>, TDelays, TActionMap, TActorMap, TGuardMap>>(config: {
309
+ schemas?: {
310
+ events?: TEventSchemaMap;
311
+ context?: TContextSchema;
312
+ emitted?: TEmittedSchemaMap;
313
+ input?: TInputSchema;
314
+ output?: TOutputSchema;
315
+ meta?: TMetaSchema;
316
+ tags?: TTagSchema;
317
+ children?: TChildrenSchemaMap;
318
+ };
319
+ actions?: TActionMap;
320
+ actorSources?: TActorMap;
321
+ guards?: TGuardMap;
322
+ delays?: TDelayMap;
323
+ } & TConfig & ValidateSetupStateKeys<TConfig, TStates> & ValidateNestedSetupStateKeys<TConfig, TStates> & ValidateSetupDelayReferences<TConfig, TSetupDelays> & ValidateRegistryKeys<TConfig, TSystemRegistry, MergeImplementationMaps<TSetupActorMap, TActorMap>>): StateMachine<SetupContext<TSchemas, TContextSchema>, SetupEvents<TSchemas, TEventSchemaMap> | ([RoutableStateId<Cast<TConfig, StateSchema>>] extends [never] ? never : {
324
+ type: 'xstate.route';
325
+ to: RoutableStateId<Cast<TConfig, StateSchema>>;
326
+ }), Cast<MergeChildren<SetupChildren<TSchemas, TChildrenSchemaMap>, TActor>, Record<string, AnyActorRef | undefined>>, StateValue, TTag & string, [
327
+ SetupSchema<TSchemas, 'input'>
328
+ ] extends [never] ? TInput : SetupInput<TSchemas, TInputSchema>, SetupOutput<TSchemas, TOutputSchema>, SetupEmitted<TSchemas, TEmittedSchemaMap>, SetupMeta<TSchemas, TMetaSchema>, MergeStateSchema<Cast<TConfig, StateSchema>, SetupStatesToStateSchema<TStates>>, MergeImplementationMaps<TSetupActionMap, TActionMap>, MergeImplementationMaps<TSetupActorMap, TActorMap>, MergeImplementationMaps<TSetupGuardMap, TGuardMap>, DelayMapFromNames<TSetupDelays | TDelays, MergeImplementationMaps<TSetupDelayMap, TDelayMap>>>;
329
+ /** Creates a state node config with the setup configuration */
330
+ createStateConfig<const TConfig extends StateNodeConfigWithNestedInput<TStates, SetupStateSchema, SetupContext<TSchemas, StandardSchemaV1>, SetupContextShape<TSchemas, StandardSchemaV1, SetupContext<TSchemas, StandardSchemaV1>>, SetupEvents<TSchemas, Record<string, StandardSchemaV1>>, Cast<SetupChildren<TSchemas, Record<string, StandardSchemaV1>>, Record<string, AnyActorRef | undefined>>, string, SetupTags<TSchemas, StandardSchemaV1>, SetupEmitted<TSchemas, Record<string, StandardSchemaV1>>, SetupMeta<TSchemas, StandardSchemaV1>, TSetupActionMap, TSetupActorMap, TSetupGuardMap, TSetupDelayMap, TSystemRegistry>>(config: TConfig): TConfig;
331
+ /** State input schemas from setup config */
332
+ states: TStates;
333
+ }
334
+ type SetupConfigSchemas<TConfig> = TConfig extends {
335
+ schemas?: infer TSchemas;
336
+ } ? TSchemas extends SetupSchemas ? TSchemas : {} : {};
337
+ type SetupConfigStates<TConfig> = TConfig extends {
338
+ states?: infer TStates;
339
+ } ? TStates extends Record<string, SetupStateSchema> ? TStates : Record<string, SetupStateSchema> : Record<string, SetupStateSchema>;
340
+ type SetupConfigActions<TConfig> = TConfig extends {
341
+ actions?: infer TActions;
342
+ } ? TActions extends Implementations['actions'] ? TActions : {} : {};
343
+ type SetupConfigActorSources<TConfig> = TConfig extends {
344
+ actorSources?: infer TActorSources;
345
+ } ? TActorSources extends Implementations['actorSources'] ? TActorSources : {} : {};
346
+ type SetupConfigGuards<TConfig> = TConfig extends {
347
+ guards?: infer TGuards;
348
+ } ? TGuards extends Implementations['guards'] ? TGuards : {} : {};
349
+ type SetupConfigDelays<TConfig> = TConfig extends {
350
+ delays?: infer TDelays;
351
+ } ? TDelays extends Implementations['delays'] ? TDelays : {} : {};
352
+ export type SetupReturnFromConfig<TConfig extends AnySetupConfig> = SetupReturn<SetupConfigStates<TConfig>, SetupConfigSchemas<TConfig>, SetupConfigActions<TConfig>, SetupConfigActorSources<TConfig>, SetupConfigGuards<TConfig>, SetupConfigDelays<TConfig>>;
353
+ /**
354
+ * Sets up a state machine with state input schemas and other configuration.
355
+ *
356
+ * @example
357
+ *
358
+ * ```ts
359
+ * import { setup } from 'xstate';
360
+ * import z from 'zod';
361
+ *
362
+ * const s = setup({
363
+ * states: {
364
+ * loading: {
365
+ * schemas: {
366
+ * input: z.object({
367
+ * userId: z.string()
368
+ * })
369
+ * }
370
+ * }
371
+ * }
372
+ * });
373
+ *
374
+ * const machine = s.createMachine({
375
+ * initial: {
376
+ * target: 'loading',
377
+ * input: { userId: '123' }
378
+ * },
379
+ * states: {
380
+ * loading: {
381
+ * entry: ({ input }) => {
382
+ * console.log(input.userId);
383
+ * }
384
+ * }
385
+ * }
386
+ * });
387
+ * ```
388
+ */
389
+ export declare function setup(): SetupReturn;
390
+ export declare function setup<const TSchemas extends SetupSchemas = {}, const TStates extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>, TActionMap extends Implementations['actions'] = {}, TActorMap extends Implementations['actorSources'] = {}, TGuardMap extends Implementations['guards'] = {}, TDelayMap extends Implementations['delays'] = {}>(config: SetupConfig<TSchemas, TStates, TActionMap, TActorMap, TGuardMap, TDelayMap>): SetupReturn<TStates, TSchemas, TActionMap, TActorMap, TGuardMap, TDelayMap>;
391
+ export declare function setup<const TConfig extends AnySetupConfig>(config: TConfig): SetupReturnFromConfig<TConfig>;
392
+ type SystemBuilder<TSystemRegistry extends SystemRegistry> = {
393
+ createActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: Omit<ActorOptions<TLogic>, 'registryKey'> & {
394
+ registryKey?: RegistryKeyForLogic<TLogic, TSystemRegistry>;
395
+ } & {
396
+ [K in RequiredActorOptionsKeys<TLogic>]: unknown;
397
+ }): Actor<TLogic>;
398
+ get: SystemRuntime<TSystemRegistry>['get'];
399
+ getAll: SystemRuntime<TSystemRegistry>['getAll'];
400
+ inspect(observer: Observer<InspectionEvent> | ((inspectionEvent: InspectionEvent) => void)): Subscription;
401
+ setup<const TSchemas extends SetupSchemas = {}, const TStates extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>, TActionMap extends Implementations['actions'] = {}, TActorMap extends Implementations['actorSources'] = {}, TGuardMap extends Implementations['guards'] = {}, TDelayMap extends Implementations['delays'] = {}>(config?: SetupConfig<TSchemas, TStates, TActionMap, TActorMap, TGuardMap, TDelayMap>): SetupReturn<TStates, TSchemas, TActionMap, TActorMap, TGuardMap, TDelayMap, Extract<keyof TDelayMap, string>, TSystemRegistry>;
402
+ };
403
+ export declare function createSystem<const TSystemRegistry extends SystemRegistry = {}>(_config?: SystemConfig<TSystemRegistry>): SystemBuilder<TSystemRegistry>;
144
404
  export {};
@@ -1,37 +1,12 @@
1
- import { ActorRefFromLogic, AnyActorLogic, AnyActorRef, AnyActorScope, AnyEventObject, AnyMachineSnapshot, ConditionalRequired, GetConcreteByKey, InputFrom, IsLiteralString, IsNotNever, ProvidedActor, RequiredActorOptions, type RequiredLogicInput } from "./types.js";
2
- type SpawnOptions<TActor extends ProvidedActor, TSrc extends TActor['src']> = TActor extends {
3
- src: TSrc;
4
- } ? ConditionalRequired<[
5
- options?: {
6
- id?: TActor['id'];
7
- systemId?: string;
8
- input?: InputFrom<TActor['logic']>;
9
- syncSnapshot?: boolean;
10
- } & {
11
- [K in RequiredActorOptions<TActor>]: unknown;
12
- }
13
- ], IsNotNever<RequiredActorOptions<TActor>>> : never;
14
- export type Spawner<TActor extends ProvidedActor> = IsLiteralString<TActor['src']> extends true ? {
15
- <TSrc extends TActor['src']>(logic: TSrc, ...[options]: SpawnOptions<TActor, TSrc>): ActorRefFromLogic<GetConcreteByKey<TActor, 'src', TSrc>['logic']>;
16
- <TLogic extends AnyActorLogic>(src: TLogic, ...[options]: ConditionalRequired<[
17
- options?: {
18
- id?: never;
19
- systemId?: string;
20
- input?: InputFrom<TLogic>;
21
- syncSnapshot?: boolean;
22
- } & {
23
- [K in RequiredLogicInput<TLogic>]: unknown;
24
- }
25
- ], IsNotNever<RequiredLogicInput<TLogic>>>): ActorRefFromLogic<TLogic>;
26
- } : <TLogic extends AnyActorLogic | string>(src: TLogic, ...[options]: ConditionalRequired<[
1
+ import { ActorFromLogic, AnyActorLogic, AnyActorRef, AnyActorScope, AnyEventObject, AnyMachineSnapshot, ConditionalRequired, InputFrom, IsNotNever, RegistryKeyForLogic, SystemRegistry, type RequiredLogicInput } from "./types.js";
2
+ export type Spawner<TSystemRegistry extends SystemRegistry = SystemRegistry> = <TLogic extends AnyActorLogic>(src: TLogic, ...[options]: ConditionalRequired<[
27
3
  options?: {
28
4
  id?: string;
29
- systemId?: string;
5
+ registryKey?: RegistryKeyForLogic<TLogic, TSystemRegistry>;
30
6
  input?: TLogic extends string ? unknown : InputFrom<TLogic>;
31
7
  syncSnapshot?: boolean;
32
- } & (TLogic extends AnyActorLogic ? {
8
+ } & {
33
9
  [K in RequiredLogicInput<TLogic>]: unknown;
34
- } : {})
35
- ], IsNotNever<TLogic extends AnyActorLogic ? RequiredLogicInput<TLogic> : never>>) => TLogic extends AnyActorLogic ? ActorRefFromLogic<TLogic> : AnyActorRef;
36
- export declare function createSpawner(actorScope: AnyActorScope, { machine, context }: AnyMachineSnapshot, event: AnyEventObject, spawnedChildren: Record<string, AnyActorRef>): Spawner<any>;
37
- export {};
10
+ }
11
+ ], IsNotNever<RequiredLogicInput<TLogic>>>) => ActorFromLogic<TLogic>;
12
+ export declare function createSpawner(actorScope: AnyActorScope, { machine, context }: AnyMachineSnapshot, event: AnyEventObject, spawnedChildren: Record<string, AnyActorRef>): Spawner;