xstate 5.32.1 → 6.0.0-alpha.2

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 (119) hide show
  1. package/README.md +7 -8
  2. package/dist/StateMachine-2dcdb1f5.esm.js +720 -0
  3. package/dist/StateMachine-2ea0a7fa.development.esm.js +737 -0
  4. package/dist/StateMachine-64813f46.cjs.js +723 -0
  5. package/dist/StateMachine-8d22a79c.development.cjs.js +740 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +27 -21
  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/atom.d.ts +81 -0
  18. package/dist/declarations/src/createActor.d.ts +31 -33
  19. package/dist/declarations/src/createMachine.d.ts +35 -7
  20. package/dist/declarations/src/createMachineFromConfig.d.ts +176 -0
  21. package/dist/declarations/src/graph/graph.d.ts +4 -2
  22. package/dist/declarations/src/index.d.ts +12 -8
  23. package/dist/declarations/src/inspection.d.ts +84 -30
  24. package/dist/declarations/src/mapState.d.ts +4 -2
  25. package/dist/declarations/src/schema.types.d.ts +90 -0
  26. package/dist/declarations/src/serialize.d.ts +44 -0
  27. package/dist/declarations/src/setup.d.ts +251 -135
  28. package/dist/declarations/src/spawn.d.ts +6 -31
  29. package/dist/declarations/src/stateUtils.d.ts +21 -19
  30. package/dist/declarations/src/system.d.ts +8 -7
  31. package/dist/declarations/src/transition.d.ts +5 -5
  32. package/dist/declarations/src/types.d.ts +405 -405
  33. package/dist/declarations/src/types.v6.d.ts +392 -0
  34. package/dist/declarations/src/utils.d.ts +4 -3
  35. package/dist/index-93edd3bd.esm.js +4909 -0
  36. package/dist/index-9cb3b3a0.cjs.js +4968 -0
  37. package/dist/index-e61170ba.development.esm.js +4948 -0
  38. package/dist/index-f6ef20d1.development.cjs.js +5007 -0
  39. package/dist/xstate-actors.cjs.js +12 -838
  40. package/dist/xstate-actors.cjs.mjs +10 -5
  41. package/dist/xstate-actors.development.cjs.js +12 -838
  42. package/dist/xstate-actors.development.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.esm.js +1 -836
  44. package/dist/xstate-actors.esm.js +1 -836
  45. package/dist/xstate-actors.umd.min.js +1 -1
  46. package/dist/xstate-actors.umd.min.js.map +1 -1
  47. package/dist/xstate-graph.cjs.js +18 -29
  48. package/dist/xstate-graph.development.cjs.js +18 -29
  49. package/dist/xstate-graph.development.esm.js +15 -26
  50. package/dist/xstate-graph.esm.js +15 -26
  51. package/dist/xstate-graph.umd.min.js +1 -1
  52. package/dist/xstate-graph.umd.min.js.map +1 -1
  53. package/dist/xstate.cjs.js +1316 -112
  54. package/dist/xstate.cjs.mjs +22 -22
  55. package/dist/xstate.development.cjs.js +1316 -112
  56. package/dist/xstate.development.cjs.mjs +22 -22
  57. package/dist/xstate.development.esm.js +1278 -76
  58. package/dist/xstate.esm.js +1278 -76
  59. package/dist/xstate.umd.min.js +1 -1
  60. package/dist/xstate.umd.min.js.map +1 -1
  61. package/package.json +3 -47
  62. package/dist/StateMachine-45e79c35.esm.js +0 -560
  63. package/dist/StateMachine-be2e51a3.development.cjs.js +0 -569
  64. package/dist/StateMachine-e0178d48.development.esm.js +0 -566
  65. package/dist/StateMachine-e6732977.cjs.js +0 -563
  66. package/dist/assign-2aa58daa.esm.js +0 -127
  67. package/dist/assign-927227d6.development.cjs.js +0 -135
  68. package/dist/assign-e8f2bd75.cjs.js +0 -129
  69. package/dist/assign-f338cee3.development.esm.js +0 -133
  70. package/dist/declarations/src/actions/assign.d.ts +0 -45
  71. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  72. package/dist/declarations/src/actions/emit.d.ts +0 -44
  73. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  74. package/dist/declarations/src/actions/log.d.ts +0 -16
  75. package/dist/declarations/src/actions/raise.d.ts +0 -21
  76. package/dist/declarations/src/actions/send.d.ts +0 -42
  77. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  78. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  79. package/dist/declarations/src/actors/transition.d.ts +0 -143
  80. package/dist/declarations/src/dev/index.d.ts +0 -14
  81. package/dist/declarations/src/guards.d.ts +0 -128
  82. package/dist/log-410e5e55.development.esm.js +0 -429
  83. package/dist/log-7776fcf2.cjs.js +0 -409
  84. package/dist/log-c2e11c01.development.cjs.js +0 -437
  85. package/dist/log-fa75ec2d.esm.js +0 -401
  86. package/dist/raise-74097812.development.esm.js +0 -2823
  87. package/dist/raise-7c948725.cjs.js +0 -2816
  88. package/dist/raise-964cd4e9.development.cjs.js +0 -2878
  89. package/dist/raise-e974d1c9.esm.js +0 -2761
  90. package/dist/xstate-actions.cjs.d.mts +0 -2
  91. package/dist/xstate-actions.cjs.d.ts +0 -2
  92. package/dist/xstate-actions.cjs.js +0 -23
  93. package/dist/xstate-actions.cjs.mjs +0 -14
  94. package/dist/xstate-actions.development.cjs.js +0 -23
  95. package/dist/xstate-actions.development.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.esm.js +0 -4
  97. package/dist/xstate-actions.esm.js +0 -4
  98. package/dist/xstate-actions.umd.min.js +0 -2
  99. package/dist/xstate-actions.umd.min.js.map +0 -1
  100. package/dist/xstate-dev.cjs.d.mts +0 -2
  101. package/dist/xstate-dev.cjs.d.ts +0 -2
  102. package/dist/xstate-dev.cjs.js +0 -48
  103. package/dist/xstate-dev.cjs.mjs +0 -5
  104. package/dist/xstate-dev.development.cjs.js +0 -51
  105. package/dist/xstate-dev.development.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.esm.js +0 -45
  107. package/dist/xstate-dev.esm.js +0 -42
  108. package/dist/xstate-dev.umd.min.js +0 -2
  109. package/dist/xstate-dev.umd.min.js.map +0 -1
  110. package/dist/xstate-guards.cjs.d.mts +0 -2
  111. package/dist/xstate-guards.cjs.d.ts +0 -2
  112. package/dist/xstate-guards.cjs.js +0 -14
  113. package/dist/xstate-guards.cjs.mjs +0 -7
  114. package/dist/xstate-guards.development.cjs.js +0 -14
  115. package/dist/xstate-guards.development.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.esm.js +0 -2
  117. package/dist/xstate-guards.esm.js +0 -2
  118. package/dist/xstate-guards.umd.min.js +0 -2
  119. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -1,15 +1,8 @@
1
- import type { StateNode } from "./StateNode.js";
2
- import type { StateMachine } from "./StateMachine.js";
3
- import type { ProvidedActor, AnyMachineSnapshot, AnyStateMachine, EventObject, HistoryValue, MachineContext, StateConfig, StateValue, AnyActorRef, Snapshot, ParameterizedObject, IsNever, MetaObject, StateSchema, StateId, SnapshotStatus } from "./types.js";
4
- type ToTestStateValue<TStateValue extends StateValue> = TStateValue extends string ? TStateValue : IsNever<keyof TStateValue> extends true ? never : keyof TStateValue | {
5
- [K in keyof TStateValue]?: ToTestStateValue<NonNullable<TStateValue[K]>>;
6
- };
1
+ import type { AnyMachineSnapshot, AnyStateMachine, EventObject, HistoryValue, MachineContext, StateConfig, StateValue, AnyActorRef, Snapshot, MetaObject, StateSchema, StateId, StateIdInputs, StateContextFromStateValue, SnapshotStatus, AnyStateNode } from "./types.js";
7
2
  export declare function isMachineSnapshot(value: unknown): value is AnyMachineSnapshot;
8
- interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta, TStateSchema extends StateSchema = StateSchema> {
3
+ interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, _TOutput, TMeta extends MetaObject, TStateSchema extends StateSchema = StateSchema> {
9
4
  /** The state machine that produced this state snapshot. */
10
- machine: StateMachine<TContext, TEvent, TChildren, ProvidedActor, ParameterizedObject, ParameterizedObject, string, TStateValue, TTag, unknown, TOutput, EventObject, // TEmitted
11
- any, // TMeta
12
- TStateSchema>;
5
+ machine: AnyStateMachine;
13
6
  /** The tags of the active state nodes that represent the current state value. */
14
7
  tags: Set<string>;
15
8
  /**
@@ -38,9 +31,9 @@ interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends Ev
38
31
  status: SnapshotStatus;
39
32
  error: unknown;
40
33
  context: TContext;
41
- historyValue: Readonly<HistoryValue<TContext, TEvent>>;
34
+ historyValue: Readonly<HistoryValue>;
42
35
  /** The enabled state nodes representative of the state value. */
43
- _nodes: Array<StateNode<TContext, TEvent>>;
36
+ _nodes: Array<AnyStateNode>;
44
37
  /** An object mapping actor names to spawned/invoked actors. */
45
38
  children: TChildren;
46
39
  /**
@@ -49,7 +42,7 @@ interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends Ev
49
42
  *
50
43
  * @param partialStateValue
51
44
  */
52
- matches: (partialStateValue: ToTestStateValue<TStateValue>) => boolean;
45
+ matches<const TTestStateValue extends StateValue>(partialStateValue: TTestStateValue): this is MachineSnapshot<StateContextFromStateValue<TStateSchema, TContext, TTestStateValue>, TEvent, TChildren, TStateValue, TTag, _TOutput, TMeta, TStateSchema>;
53
46
  /**
54
47
  * Whether the current state nodes has a state node with the specified `tag`.
55
48
  *
@@ -66,6 +59,11 @@ interface MachineSnapshotBase<TContext extends MachineContext, TEvent extends Ev
66
59
  */
67
60
  can: (event: TEvent) => boolean;
68
61
  getMeta: () => Record<StateId<TStateSchema> & string, TMeta | undefined>;
62
+ /**
63
+ * Returns the inputs for the current active state nodes, keyed by state node
64
+ * id.
65
+ */
66
+ getInputs: () => StateIdInputs<TStateSchema>;
69
67
  toJSON: () => unknown;
70
68
  }
71
69
  interface ActiveMachineSnapshot<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TOutput, TMeta extends MetaObject, TStateSchema extends StateSchema> extends MachineSnapshotBase<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema> {
@@ -1,29 +1,31 @@
1
1
  import { MachineSnapshot } from "./State.js";
2
2
  import { StateNode } from "./StateNode.js";
3
3
  import { AnyActorSystem } from "./system.js";
4
- import type { ActorLogic, ActorScope, AnyActorRef, AnyActorScope, DoNotInfer, Equals, EventDescriptor, EventObject, HistoryValue, InternalMachineImplementations, MachineConfig, MachineContext, MachineImplementationsSimplified, MachineOptions, MetaObject, ParameterizedObject, ProvidedActor, Snapshot, StateMachineDefinition, StateValue, TransitionDefinition, ResolvedStateMachineTypes, StateSchema, SnapshotStatus } from "./types.js";
5
- export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, TStateValue extends StateValue, TTag extends string, TInput, TOutput, TEmitted extends EventObject, TMeta extends MetaObject, TStateSchema extends StateSchema> implements ActorLogic<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, TEvent, TInput, AnyActorSystem, TEmitted> {
4
+ import type { ActorLogic, ActorScope, AnyActor, AnyActorRef, AnyActorScope, AnyTransitionDefinition, Equals, EventDescriptor, EventObject, HistoryValue, MachineContext, MetaObject, Snapshot, StateValue, StateSchema, SnapshotStatus } from "./types.js";
5
+ import { Implementations, Next_MachineConfig, MachineOptions } from "./types.v6.js";
6
+ export declare class StateMachine<TContext extends MachineContext, TEvent extends EventObject, TChildren extends Record<string, AnyActorRef | undefined>, TStateValue extends StateValue, TTag extends string, TInput, TOutput, TEmitted extends EventObject, TMeta extends MetaObject, TConfig extends StateSchema, TActionMap extends Implementations['actions'], TActorMap extends Implementations['actors'], TGuardMap extends Implementations['guards'], TDelayMap extends Implementations['delays']> implements ActorLogic<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, TInput, AnyActorSystem, TEmitted> {
6
7
  /** The raw config used to create the machine. */
7
- config: MachineConfig<TContext, TEvent, any, any, any, any, any, any, TOutput, any, // TEmitted
8
- any> & {
8
+ config: Next_MachineConfig<any, any, any, any, any, any, any, any, any, any> & {
9
9
  schemas?: unknown;
10
+ internalEvents?: readonly string[];
10
11
  };
11
12
  /** The machine's own version. */
12
13
  version?: string;
13
14
  schemas: unknown;
14
- implementations: MachineImplementationsSimplified<TContext, TEvent>;
15
+ implementations: Implementations;
15
16
  /** Runtime options for machine execution. */
16
17
  options: MachineOptions;
17
18
  root: StateNode<TContext, TEvent>;
18
19
  id: string;
19
20
  states: StateNode<TContext, TEvent>['states'];
20
21
  events: Array<EventDescriptor<TEvent>>;
22
+ internalEventDescriptors: ReadonlyArray<string>;
21
23
  constructor(
22
24
  /** The raw config used to create the machine. */
23
- config: MachineConfig<TContext, TEvent, any, any, any, any, any, any, TOutput, any, // TEmitted
24
- any> & {
25
+ config: Next_MachineConfig<any, any, any, any, any, any, any, any, any, any> & {
25
26
  schemas?: unknown;
26
- }, implementations?: MachineImplementationsSimplified<TContext, TEvent>);
27
+ internalEvents?: readonly string[];
28
+ }, implementations?: Implementations);
27
29
  /**
28
30
  * Clones this state machine with the provided implementations.
29
31
  *
@@ -31,17 +33,22 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
31
33
  * recursively merge with the existing options.
32
34
  * @returns A new `StateMachine` instance with the provided implementations.
33
35
  */
34
- provide(implementations: InternalMachineImplementations<ResolvedStateMachineTypes<TContext, DoNotInfer<TEvent>, TActor, TAction, TGuard, TDelay, TTag, TEmitted>>): StateMachine<TContext, TEvent, TChildren, TActor, TAction, TGuard, TDelay, TStateValue, TTag, TInput, TOutput, TEmitted, TMeta, TStateSchema>;
36
+ provide(implementations: {
37
+ actions?: Partial<TActionMap>;
38
+ actors?: Partial<TActorMap>;
39
+ guards?: Partial<TGuardMap>;
40
+ delays?: Partial<TDelayMap>;
41
+ }): StateMachine<TContext, TEvent, TChildren, TStateValue, TTag, TInput, TOutput, TEmitted, TMeta, TConfig, TActionMap, TActorMap, TGuardMap, TDelayMap>;
35
42
  resolveState(config: {
36
43
  value: StateValue;
37
44
  context?: TContext;
38
- historyValue?: HistoryValue<TContext, TEvent>;
45
+ historyValue?: HistoryValue;
39
46
  status?: SnapshotStatus;
40
47
  output?: TOutput;
41
48
  error?: unknown;
42
- } & (Equals<TContext, MachineContext> extends false ? {
49
+ } & ([TContext] extends [never] ? {} : Equals<TContext, MachineContext> extends false ? {
43
50
  context: unknown;
44
- } : {})): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>;
51
+ } : {})): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
45
52
  /**
46
53
  * Determines the next snapshot given the current `snapshot` and received
47
54
  * `event`. Calculates a full macrostep from all microsteps.
@@ -49,7 +56,7 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
49
56
  * @param snapshot The current snapshot
50
57
  * @param event The received event
51
58
  */
52
- transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, event: TEvent, actorScope: ActorScope<typeof snapshot, TEvent, AnyActorSystem, TEmitted>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>;
59
+ transition(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: ActorScope<typeof snapshot, TEvent, AnyActorSystem, TEmitted>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
53
60
  /**
54
61
  * Determines the next state given the current `state` and `event`. Calculates
55
62
  * a microstep.
@@ -57,17 +64,16 @@ export declare class StateMachine<TContext extends MachineContext, TEvent extend
57
64
  * @param state The current state
58
65
  * @param event The received event
59
66
  */
60
- microstep(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, event: TEvent, actorScope: AnyActorScope): Array<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>>;
61
- getTransitionData(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, event: TEvent): Array<TransitionDefinition<TContext, TEvent>>;
67
+ microstep(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, actorScope: AnyActorScope): Array<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>>;
68
+ getTransitionData(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, event: TEvent, self: AnyActor): Array<AnyTransitionDefinition>;
69
+ isInternalEventType(eventType: string): boolean;
62
70
  /**
63
71
  * Returns the initial `State` instance, with reference to `self` as an
64
72
  * `ActorRef`.
65
73
  */
66
- getInitialSnapshot(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, TEvent, AnyActorSystem, TEmitted>, input?: TInput): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>;
67
- start(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>): void;
74
+ getInitialSnapshot(actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>, input?: TInput): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
75
+ start(snapshot?: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>): void;
68
76
  getStateNodeById(stateId: string): StateNode<TContext, TEvent>;
69
- get definition(): StateMachineDefinition<TContext, TEvent>;
70
- toJSON(): StateMachineDefinition<TContext, TEvent>;
71
- getPersistedSnapshot(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, options?: unknown): Snapshot<unknown>;
72
- restoreSnapshot(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>, TEvent, AnyActorSystem, TEmitted>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TStateSchema>;
77
+ getPersistedSnapshot(snapshot: MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, options?: unknown): Snapshot<unknown>;
78
+ restoreSnapshot(snapshot: Snapshot<unknown>, _actorScope: ActorScope<MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>, TEvent, AnyActorSystem, TEmitted>): MachineSnapshot<TContext, TEvent, TChildren, TStateValue, TTag, TOutput, TMeta, TConfig>;
73
79
  }
@@ -1,5 +1,4 @@
1
- import type { StateMachine } from "./StateMachine.js";
2
- import type { DelayedTransitionDefinition, EventObject, InitialTransitionDefinition, InvokeDefinition, MachineContext, Mapper, StateNodeConfig, StateNodeDefinition, StateNodesConfig, TransitionDefinition, TransitionDefinitionMap, TODO, UnknownAction, ParameterizedObject, AnyStateMachine, ProvidedActor, NonReducibleUnknown, EventDescriptor } from "./types.js";
1
+ import type { DelayedTransitionDefinition, EventObject, InitialTransitionDefinition, MachineContext, Mapper, StateNodesConfig, TransitionDefinitionMap, AnyStateMachine, AnyStateNodeConfig, NonReducibleUnknown, EventDescriptor, AnyAction, AnyTransitionDefinition, AnyInvokeDefinition } from "./types.js";
3
2
  interface StateNodeOptions<TContext extends MachineContext, TEvent extends EventObject> {
4
3
  _key: string;
5
4
  _parent?: StateNode<TContext, TEvent>;
@@ -7,14 +6,7 @@ interface StateNodeOptions<TContext extends MachineContext, TEvent extends Event
7
6
  }
8
7
  export declare class StateNode<TContext extends MachineContext = MachineContext, TEvent extends EventObject = EventObject> {
9
8
  /** The raw config used to create the machine. */
10
- config: StateNodeConfig<TContext, TEvent, TODO, // actors
11
- TODO, // actions
12
- TODO, // guards
13
- TODO, // delays
14
- TODO, // tags
15
- TODO, // output
16
- TODO, // emitted
17
- TODO>;
9
+ config: AnyStateNodeConfig;
18
10
  /**
19
11
  * The relative key of the state node, which represents its location in the
20
12
  * overall state value.
@@ -29,13 +21,14 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
29
21
  * - `'compound'` - nested child state nodes (XOR)
30
22
  * - `'parallel'` - orthogonal nested child state nodes (AND)
31
23
  * - `'history'` - history state node
24
+ * - `'choice'` - pure routing decision state node
32
25
  * - `'final'` - final state node
33
26
  */
34
- type: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
27
+ type: 'atomic' | 'compound' | 'parallel' | 'final' | 'history' | 'choice';
35
28
  /** The string path from the root machine node to this node. */
36
29
  path: string[];
37
30
  /** The child state nodes. */
38
- states: StateNodesConfig<TContext, TEvent>;
31
+ states: StateNodesConfig<any, any>;
39
32
  /**
40
33
  * The type of history on this state node. Can be:
41
34
  *
@@ -44,24 +37,13 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
44
37
  */
45
38
  history: false | 'shallow' | 'deep';
46
39
  /** The action(s) to be executed upon entering the state node. */
47
- entry: UnknownAction[];
40
+ entry: AnyAction | undefined;
48
41
  /** The action(s) to be executed upon exiting the state node. */
49
- exit: UnknownAction[];
42
+ exit: AnyAction | undefined;
50
43
  /** The parent state node. */
51
- parent?: StateNode<TContext, TEvent>;
44
+ parent?: StateNode<any, any>;
52
45
  /** The root machine node. */
53
- machine: StateMachine<TContext, TEvent, any, // children
54
- any, // actor
55
- any, // action
56
- any, // guard
57
- any, // delay
58
- any, // state value
59
- any, // tag
60
- any, // input
61
- any, // output
62
- any, // emitted
63
- any, // meta
64
- any>;
46
+ machine: AnyStateMachine;
65
47
  /**
66
48
  * The meta data associated with this state node, which will be returned in
67
49
  * State instances.
@@ -79,34 +61,16 @@ export declare class StateNode<TContext extends MachineContext = MachineContext,
79
61
  order: number;
80
62
  description?: string;
81
63
  tags: string[];
82
- transitions: Map<string, TransitionDefinition<TContext, TEvent>[]>;
83
- always?: Array<TransitionDefinition<TContext, TEvent>>;
64
+ transitions: Map<string, AnyTransitionDefinition[]>;
65
+ always?: Array<AnyTransitionDefinition>;
66
+ invoke: Array<AnyInvokeDefinition>;
67
+ on: TransitionDefinitionMap<any, any>;
68
+ after: Array<DelayedTransitionDefinition<any, any>>;
69
+ events: Array<EventDescriptor<any>>;
70
+ ownEvents: Array<EventDescriptor<any>>;
84
71
  constructor(
85
72
  /** The raw config used to create the machine. */
86
- config: StateNodeConfig<TContext, TEvent, TODO, // actors
87
- TODO, // actions
88
- TODO, // guards
89
- TODO, // delays
90
- TODO, // tags
91
- TODO, // output
92
- TODO, // emitted
93
- TODO>, options: StateNodeOptions<TContext, TEvent>);
94
- /** The well-structured state node definition. */
95
- get definition(): StateNodeDefinition<TContext, TEvent>;
96
- /** The logic invoked as actors by this state node. */
97
- get invoke(): Array<InvokeDefinition<TContext, TEvent, ProvidedActor, ParameterizedObject, ParameterizedObject, string, TODO, // TEmitted
98
- TODO>>;
99
- /** The mapping of events to transitions. */
100
- get on(): TransitionDefinitionMap<TContext, TEvent>;
101
- get after(): Array<DelayedTransitionDefinition<TContext, TEvent>>;
102
- get initial(): InitialTransitionDefinition<TContext, TEvent>;
103
- /** All the event types accepted by this state node and its descendants. */
104
- get events(): Array<EventDescriptor<TEvent>>;
105
- /**
106
- * All the events that have transitions directly from this state node.
107
- *
108
- * Excludes any inert events.
109
- */
110
- get ownEvents(): Array<EventDescriptor<TEvent>>;
73
+ config: AnyStateNodeConfig, options: StateNodeOptions<TContext, TEvent>);
74
+ get initial(): InitialTransitionDefinition;
111
75
  }
112
76
  export {};
@@ -1,9 +1,14 @@
1
- export { assign, type AssignAction, type AssignArgs } from "./actions/assign.js";
2
- export { cancel, type CancelAction } from "./actions/cancel.js";
3
- export { emit, type EmitAction } from "./actions/emit.js";
4
- export { enqueueActions, type EnqueueActionsAction } from "./actions/enqueueActions.js";
5
- export { log, type LogAction } from "./actions/log.js";
6
- export { raise, type RaiseAction } from "./actions/raise.js";
7
- export { forwardTo, sendParent, sendTo, type SendToAction } from "./actions/send.js";
8
- export { spawnChild, type SpawnAction, type SpawnActionOptions } from "./actions/spawnChild.js";
9
- export { stop, stopChild, type StopAction } from "./actions/stopChild.js";
1
+ import { AnyActor, AnyActorScope, EventObject } from "./types.js";
2
+ export declare const builtInActions: {
3
+ "@xstate.start": (actor: AnyActor) => void;
4
+ "@xstate.raise": (actorScope: AnyActorScope, event: EventObject, options: {
5
+ id?: string;
6
+ delay?: number;
7
+ }) => void;
8
+ "@xstate.sendTo": (actorScope: AnyActorScope, actor: AnyActor, event: EventObject, options: {
9
+ id?: string;
10
+ delay?: number;
11
+ }) => void;
12
+ "@xstate.cancel": (actorScope: AnyActorScope, sendId: string) => void;
13
+ "@xstate.stopChild": (actorScope: AnyActorScope, actor: AnyActor) => void;
14
+ };
@@ -1,18 +1,28 @@
1
1
  import { AnyActorSystem } from "../system.js";
2
2
  import { ActorLogic, ActorRefFromLogic, AnyEventObject, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
3
+ import { StandardSchemaV1 } from "../schema.types.js";
3
4
  export type CallbackSnapshot<TInput> = Snapshot<undefined> & {
4
5
  input: TInput;
6
+ effects?: Record<string, {
7
+ status: 'active';
8
+ } | {
9
+ status: 'done';
10
+ output?: unknown;
11
+ } | {
12
+ status: 'error';
13
+ error: unknown;
14
+ }>;
5
15
  };
6
16
  export type CallbackActorLogic<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<CallbackSnapshot<TInput>, TEvent, TInput, AnyActorSystem, TEmitted>;
7
17
  /**
8
- * Represents an actor created by `fromCallback`.
18
+ * Represents an actor created by `createCallbackLogic`.
9
19
  *
10
20
  * The type of `self` within the actor's logic.
11
21
  *
12
22
  * @example
13
23
  *
14
24
  * ```ts
15
- * import { fromCallback, createActor } from 'xstate';
25
+ * import { createCallbackLogic, createActor } from 'xstate';
16
26
  *
17
27
  * // The events the actor receives.
18
28
  * type Event = { type: 'someEvent' };
@@ -20,23 +30,25 @@ export type CallbackActorLogic<TEvent extends EventObject, TInput = NonReducible
20
30
  * type Input = { name: string };
21
31
  *
22
32
  * // Actor logic that logs whenever it receives an event of type `someEvent`.
23
- * const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
24
- * self;
25
- * // ^? CallbackActorRef<Event, Input>
26
- *
27
- * receive((event) => {
28
- * if (event.type === 'someEvent') {
29
- * console.log(`${input.name}: received "someEvent" event`);
30
- * // logs 'myActor: received "someEvent" event'
31
- * }
32
- * });
33
- * });
33
+ * const logic = createCallbackLogic<Event, Input>(
34
+ * ({ self, input, receive }) => {
35
+ * self;
36
+ * // ^? CallbackActorRef<Event, Input>
37
+ *
38
+ * receive((event) => {
39
+ * if (event.type === 'someEvent') {
40
+ * console.log(`${input.name}: received "someEvent" event`);
41
+ * // logs 'myActor: received "someEvent" event'
42
+ * }
43
+ * });
44
+ * }
45
+ * );
34
46
  *
35
47
  * const actor = createActor(logic, { input: { name: 'myActor' } });
36
48
  * // ^? CallbackActorRef<Event, Input>
37
49
  * ```
38
50
  *
39
- * @see {@link fromCallback}
51
+ * @see {@link createCallbackLogic}
40
52
  */
41
53
  export type CallbackActorRef<TEvent extends EventObject, TInput = NonReducibleUnknown> = ActorRefFromLogic<CallbackActorLogic<TEvent, TInput>>;
42
54
  type Receiver<TEvent extends EventObject> = (listener: {
@@ -62,6 +74,12 @@ export type CallbackLogicFunction<TEvent extends EventObject = AnyEventObject, T
62
74
  receive: Receiver<TEvent>;
63
75
  emit: (emitted: TEmitted) => void;
64
76
  }) => (() => void) | void;
77
+ export interface CallbackLogicConfig<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject, TInputSchema extends StandardSchemaV1 = StandardSchemaV1> {
78
+ schemas?: {
79
+ input?: TInputSchema;
80
+ };
81
+ run: CallbackLogicFunction<TEvent, TSentEvent, TInput, TEmitted>;
82
+ }
65
83
  /**
66
84
  * An actor logic creator which returns callback logic as defined by a callback
67
85
  * function.
@@ -85,7 +103,7 @@ export type CallbackLogicFunction<TEvent extends EventObject = AnyEventObject, T
85
103
  * @example
86
104
  *
87
105
  * ```typescript
88
- * const callbackLogic = fromCallback(({ sendBack, receive }) => {
106
+ * const callbackLogic = createCallbackLogic(({ sendBack, receive }) => {
89
107
  * let lockStatus = 'unlocked';
90
108
  *
91
109
  * const handler = (event) => {
@@ -128,5 +146,13 @@ export type CallbackLogicFunction<TEvent extends EventObject = AnyEventObject, T
128
146
  * @see {@link CallbackLogicFunction} for more information about the callback function and its object argument
129
147
  * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
130
148
  */
131
- export declare function fromCallback<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(callback: CallbackLogicFunction<TEvent, AnyEventObject, TInput, TEmitted>): CallbackActorLogic<TEvent, TInput, TEmitted>;
149
+ export declare function createCallbackLogic<TEvent extends EventObject, const TInputSchema extends StandardSchemaV1, TEmitted extends EventObject = EventObject>(config: CallbackLogicConfig<TEvent, AnyEventObject, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema> & {
150
+ schemas: {
151
+ input: TInputSchema;
152
+ };
153
+ }): CallbackActorLogic<TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted>;
154
+ export declare function createCallbackLogic<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(config: CallbackLogicConfig<TEvent, AnyEventObject, TInput, TEmitted> & {
155
+ schemas?: undefined;
156
+ }): CallbackActorLogic<TEvent, TInput, TEmitted>;
157
+ export declare function createCallbackLogic<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(callback: CallbackLogicFunction<TEvent, AnyEventObject, TInput, TEmitted>): CallbackActorLogic<TEvent, TInput, TEmitted>;
132
158
  export {};
@@ -1,6 +1,9 @@
1
- import type { ActorRef, AnyEventObject, Snapshot } from "../types.js";
2
- export { fromCallback, type CallbackActorLogic, type CallbackActorRef, type CallbackSnapshot, type CallbackLogicFunction } from "./callback.js";
3
- export { fromEventObservable, fromObservable, type ObservableActorLogic, type ObservableActorRef, type ObservableSnapshot } from "./observable.js";
4
- export { fromPromise, type PromiseActorLogic, type PromiseActorRef, type PromiseSnapshot } from "./promise.js";
5
- export { fromTransition, type TransitionActorLogic, type TransitionActorRef, type TransitionSnapshot } from "./transition.js";
6
- export declare function createEmptyActor(): ActorRef<Snapshot<undefined>, AnyEventObject, AnyEventObject>;
1
+ import type { ActorFromLogic } from "../types.js";
2
+ export { createCallbackLogic, type CallbackActorLogic, type CallbackActorRef, type CallbackLogicConfig, type CallbackSnapshot, type CallbackLogicFunction } from "./callback.js";
3
+ export { createObservableLogic, createEventObservableLogic, type EventObservableLogicConfig, type EventObservableLogicFunction, type ObservableActorLogic, type ObservableActorRef, type ObservableLogicConfig, type ObservableLogicFunction, type ObservableSnapshot } from "./observable.js";
4
+ export { createLogic, type LogicActorLogic, type LogicActorRef, type LogicArgs, type LogicConfig, type LogicEffect, type LogicEffectState, type LogicEnqueue, type LogicFunction, type LogicPatch, type LogicSnapshot } from "./logic.js";
5
+ export { createAsyncLogic, type AsyncActorLogic, type AsyncActorRef, type AsyncSnapshot, type LogicArgs as AsyncLogicArgs, type LogicConfig as AsyncLogicConfig, type LogicEnqueue as AsyncLogicEnqueue, type LogicFunction as AsyncLogicFunction, TimeoutError } from "./promise.js";
6
+ export { createListenerLogic, listenerLogic, type ListenerActorLogic, type ListenerActorRef, type ListenerSnapshot, type ListenerInput } from "./listener.js";
7
+ export { createSubscriptionLogic, subscriptionLogic, type SubscriptionActorLogic, type SubscriptionActorRef, type SubscriptionSnapshot, type SubscriptionInput, type SubscriptionMappers } from "./subscription.js";
8
+ declare const emptyLogic: import("./logic.js").LogicActorLogic<undefined, undefined, import("../types.js").EventObject, import("../types.js").NonReducibleUnknown, import("../types.js").EventObject>;
9
+ export declare function createEmptyActor(): ActorFromLogic<typeof emptyLogic>;
@@ -0,0 +1,18 @@
1
+ import { AnyActorSystem } from "../system.js";
2
+ import { ActorLogic, ActorRefFromLogic, AnyActor, EventObject, Snapshot } from "../types.js";
3
+ export type ListenerSnapshot = Snapshot<undefined> & {
4
+ input: ListenerInput<any, any>;
5
+ };
6
+ export interface ListenerInput<TEmitted extends EventObject, TMappedEvent extends EventObject> {
7
+ actor: AnyActor;
8
+ eventType: string;
9
+ mapper: (event: TEmitted) => TMappedEvent;
10
+ }
11
+ export type ListenerActorLogic<TEmitted extends EventObject = EventObject, TMappedEvent extends EventObject = EventObject> = ActorLogic<ListenerSnapshot, EventObject, ListenerInput<TEmitted, TMappedEvent>, AnyActorSystem, EventObject>;
12
+ export type ListenerActorRef<TEmitted extends EventObject = EventObject, TMappedEvent extends EventObject = EventObject> = ActorRefFromLogic<ListenerActorLogic<TEmitted, TMappedEvent>>;
13
+ /**
14
+ * Creates actor logic for listening to emitted events from another actor. Used
15
+ * internally by `enq.listen()`.
16
+ */
17
+ export declare function createListenerLogic<TEmitted extends EventObject = EventObject, TMappedEvent extends EventObject = EventObject>(): ListenerActorLogic<TEmitted, TMappedEvent>;
18
+ export declare const listenerLogic: ListenerActorLogic<EventObject, EventObject>;
@@ -0,0 +1,105 @@
1
+ import { StandardSchemaV1 } from "../schema.types.js";
2
+ import { AnyActorSystem } from "../system.js";
3
+ import { ActorLogic, ActorRefFromLogic, ActorScope, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
4
+ export type LogicSnapshot<TContext, TOutput, TInput> = Snapshot<TOutput> & {
5
+ context: TContext;
6
+ input: TInput | undefined;
7
+ effects?: Record<string, LogicEffectState>;
8
+ };
9
+ export type LogicEffectState = {
10
+ status: 'active';
11
+ } | {
12
+ status: 'done';
13
+ output?: unknown;
14
+ } | {
15
+ status: 'error';
16
+ error: unknown;
17
+ };
18
+ export interface LogicArgs<TContext, TEvent extends EventObject, TInput> {
19
+ context: TContext;
20
+ event: TEvent;
21
+ input: TInput;
22
+ system: AnyActorSystem;
23
+ self: LogicActorRef<TContext, unknown, TEvent, TInput>;
24
+ }
25
+ export type LogicEffect<TEvent extends EventObject, TEmitted extends EventObject> = {
26
+ type: 'emit';
27
+ event: TEmitted;
28
+ } | {
29
+ type: 'sendBack';
30
+ event: EventObject;
31
+ } | {
32
+ type: 'raise';
33
+ event: TEvent;
34
+ } | {
35
+ type: 'effect';
36
+ key?: string;
37
+ exec: () => void | (() => void);
38
+ } | {
39
+ type: 'cleanupEffects';
40
+ };
41
+ export interface LogicEnqueue<TEvent extends EventObject, TEmitted extends EventObject> {
42
+ emit: (emitted: TEmitted) => void;
43
+ sendBack: (event: EventObject) => void;
44
+ raise: (event: TEvent) => void;
45
+ effect: {
46
+ (exec: () => void | (() => void)): void;
47
+ (key: string, exec: () => void | (() => void)): void;
48
+ };
49
+ }
50
+ export type LogicPatch<TContext, TOutput, TInput> = Partial<{
51
+ context: TContext;
52
+ input: TInput | undefined;
53
+ status: LogicSnapshot<TContext, TOutput, TInput>['status'];
54
+ output: TOutput;
55
+ error: unknown;
56
+ effects: Record<string, LogicEffectState>;
57
+ }>;
58
+ export type LogicFunction<TContext, TOutput, TEvent extends EventObject, TInput, TEmitted extends EventObject> = (args: LogicArgs<TContext, TEvent, TInput>, enq: LogicEnqueue<TEvent, TEmitted>) => void | LogicPatch<TContext, TOutput, TInput>;
59
+ export interface LogicConfig<TContext, TOutput, TEvent extends EventObject, TInput, TEmitted extends EventObject, TInputSchema extends StandardSchemaV1 = StandardSchemaV1, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1> {
60
+ id?: string;
61
+ schemas?: {
62
+ input?: TInputSchema;
63
+ output?: TOutputSchema;
64
+ };
65
+ context: TContext | ((args: {
66
+ input: TInput;
67
+ }) => TContext);
68
+ run: LogicFunction<TContext, TOutput, TEvent, TInput, TEmitted>;
69
+ }
70
+ interface LogicTransition<TContext, TOutput, TEvent extends EventObject, TInput, TEmitted extends EventObject> {
71
+ (snapshot: LogicSnapshot<TContext, TOutput, TInput>, event: TEvent, actorScope: ActorScope<LogicSnapshot<TContext, TOutput, TInput>, TEvent, AnyActorSystem, TEmitted>): [
72
+ LogicSnapshot<TContext, TOutput, TInput>,
73
+ LogicEffect<TEvent, TEmitted>[]
74
+ ];
75
+ (snapshot: LogicSnapshot<TContext, TOutput, TInput>, event: TEvent, actorScope: ActorScope<LogicSnapshot<TContext, TOutput, TInput>, TEvent, AnyActorSystem, TEmitted>): LogicSnapshot<TContext, TOutput, TInput>;
76
+ }
77
+ export type LogicActorLogic<TContext, TOutput, TEvent extends EventObject = EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject> = Omit<ActorLogic<LogicSnapshot<TContext, TOutput, TInput>, TEvent, TInput, AnyActorSystem, TEmitted>, 'transition'> & {
78
+ id?: string;
79
+ transition: LogicTransition<TContext, TOutput, TEvent, TInput, TEmitted>;
80
+ };
81
+ export type LogicActorRef<TContext, TOutput, TEvent extends EventObject, TInput> = ActorRefFromLogic<LogicActorLogic<TContext, TOutput, TEvent, TInput>>;
82
+ export declare const XSTATE_LOGIC_EFFECT_RESOLVE = "xstate.logic.effect.resolve";
83
+ export declare const XSTATE_LOGIC_EFFECT_REJECT = "xstate.logic.effect.reject";
84
+ export declare const XSTATE_LOGIC_EFFECT_START = "xstate.logic.effect.start";
85
+ export declare function executeLogicEffects(effects: readonly unknown[] | undefined, actorScope: ActorScope<any, any, any, any>): void;
86
+ export declare function createLogic<TContext, const TInputSchema extends StandardSchemaV1, const TOutputSchema extends StandardSchemaV1, TEvent extends EventObject = EventObject, TEmitted extends EventObject = EventObject>(config: LogicConfig<TContext, StandardSchemaV1.InferOutput<TOutputSchema>, TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema, TOutputSchema> & {
87
+ schemas: {
88
+ input: TInputSchema;
89
+ output: TOutputSchema;
90
+ };
91
+ }): LogicActorLogic<TContext, StandardSchemaV1.InferOutput<TOutputSchema>, TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted>;
92
+ export declare function createLogic<TContext, TOutput, const TInputSchema extends StandardSchemaV1, TEvent extends EventObject = EventObject, TEmitted extends EventObject = EventObject>(config: LogicConfig<TContext, TOutput, TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema> & {
93
+ schemas: {
94
+ input: TInputSchema;
95
+ output?: undefined;
96
+ };
97
+ }): LogicActorLogic<TContext, TOutput, TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted>;
98
+ export declare function createLogic<TContext, const TOutputSchema extends StandardSchemaV1, TEvent extends EventObject = EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(config: LogicConfig<TContext, StandardSchemaV1.InferOutput<TOutputSchema>, TEvent, TInput, TEmitted, StandardSchemaV1, TOutputSchema> & {
99
+ schemas: {
100
+ input?: undefined;
101
+ output: TOutputSchema;
102
+ };
103
+ }): LogicActorLogic<TContext, StandardSchemaV1.InferOutput<TOutputSchema>, TEvent, TInput, TEmitted>;
104
+ export declare function createLogic<TContext, TOutput = unknown, TEvent extends EventObject = EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(config: LogicConfig<TContext, TOutput, TEvent, TInput, TEmitted>): LogicActorLogic<TContext, TOutput, TEvent, TInput, TEmitted>;
105
+ export {};