xstate 5.32.0 → 6.0.0-alpha.1

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-47e60715.esm.js +720 -0
  3. package/dist/StateMachine-48dcad8f.cjs.js +723 -0
  4. package/dist/StateMachine-6e063ac1.development.cjs.js +740 -0
  5. package/dist/StateMachine-6ec50e21.development.esm.js +737 -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 -123
  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 +407 -406
  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-39259ea4.development.esm.js +4948 -0
  36. package/dist/index-688701ed.esm.js +4909 -0
  37. package/dist/index-c35824b4.development.cjs.js +5007 -0
  38. package/dist/index-f0d7107d.cjs.js +4968 -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 -108
  54. package/dist/xstate.cjs.mjs +22 -22
  55. package/dist/xstate.development.cjs.js +1316 -108
  56. package/dist/xstate.development.cjs.mjs +22 -22
  57. package/dist/xstate.development.esm.js +1278 -72
  58. package/dist/xstate.esm.js +1278 -72
  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-029f180b.development.cjs.js +0 -433
  83. package/dist/log-5a37e0ee.development.esm.js +0 -425
  84. package/dist/log-605ef461.esm.js +0 -397
  85. package/dist/log-fedf0966.cjs.js +0 -405
  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,8 +1,18 @@
1
+ import { StandardSchemaV1 } from "../schema.types.js";
1
2
  import { AnyActorSystem } from "../system.js";
2
- import { ActorLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot, Subscribable, Subscription } from "../types.js";
3
+ import { ActorLogic, ActorFromLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot, Subscribable, Subscription } from "../types.js";
3
4
  export type ObservableSnapshot<TContext, TInput extends NonReducibleUnknown> = Snapshot<undefined> & {
4
5
  context: TContext | undefined;
5
6
  input: TInput | undefined;
7
+ effects?: Record<string, {
8
+ status: 'active';
9
+ } | {
10
+ status: 'done';
11
+ output?: unknown;
12
+ } | {
13
+ status: 'error';
14
+ error: unknown;
15
+ }>;
6
16
  _subscription: Subscription | undefined;
7
17
  };
8
18
  export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<ObservableSnapshot<TContext, TInput>, {
@@ -10,14 +20,15 @@ export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown, T
10
20
  [k: string]: unknown;
11
21
  }, TInput, AnyActorSystem, TEmitted>;
12
22
  /**
13
- * Represents an actor created by `fromObservable` or `fromEventObservable`.
23
+ * Represents an actor created by `createObservableLogic` or
24
+ * `createEventObservableLogic`.
14
25
  *
15
26
  * The type of `self` within the actor's logic.
16
27
  *
17
28
  * @example
18
29
  *
19
30
  * ```ts
20
- * import { fromObservable, createActor } from 'xstate';
31
+ * import { createObservableLogic, createActor } from 'xstate';
21
32
  * import { interval } from 'rxjs';
22
33
  *
23
34
  * // The type of the value observed by the actor's logic.
@@ -27,21 +38,48 @@ export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown, T
27
38
  *
28
39
  * // Actor logic that observes a number incremented every `input.period`
29
40
  * // milliseconds (default: 1_000).
30
- * const logic = fromObservable<Context, Input>(({ input, self }) => {
31
- * self;
32
- * // ^? ObservableActorRef<Event, Input>
41
+ * const logic = createObservableLogic<Context, Input>(
42
+ * ({ input, self }) => {
43
+ * self;
44
+ * // ^? ObservableActor<Event, Input>
33
45
  *
34
- * return interval(input.period ?? 1_000);
35
- * });
46
+ * return interval(input.period ?? 1_000);
47
+ * }
48
+ * );
36
49
  *
37
50
  * const actor = createActor(logic, { input: { period: 2_000 } });
38
- * // ^? ObservableActorRef<Event, Input>
51
+ * // ^? ObservableActor<Event, Input>
39
52
  * ```
40
53
  *
41
- * @see {@link fromObservable}
42
- * @see {@link fromEventObservable}
54
+ * @see {@link createObservableLogic}
55
+ * @see {@link createEventObservableLogic}
43
56
  */
44
57
  export type ObservableActorRef<TContext> = ActorRefFromLogic<ObservableActorLogic<TContext, any>>;
58
+ type ObservableActor<TContext, TInput extends NonReducibleUnknown = any, TEmitted extends EventObject = EventObject> = ActorFromLogic<ObservableActorLogic<TContext, TInput, TEmitted>>;
59
+ export type ObservableLogicFunction<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ({ input, system, self, emit }: {
60
+ input: TInput;
61
+ system: AnyActorSystem;
62
+ self: ObservableActor<TContext, TInput, TEmitted>;
63
+ emit: (emitted: TEmitted) => void;
64
+ }) => Subscribable<TContext>;
65
+ export interface ObservableLogicConfig<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject, TInputSchema extends StandardSchemaV1 = StandardSchemaV1> {
66
+ schemas?: {
67
+ input?: TInputSchema;
68
+ };
69
+ run: ObservableLogicFunction<TContext, TInput, TEmitted>;
70
+ }
71
+ export type EventObservableLogicFunction<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ({ input, system, self, emit }: {
72
+ input: TInput;
73
+ system: AnyActorSystem;
74
+ self: ObservableActor<TEvent, TInput, TEmitted>;
75
+ emit: (emitted: TEmitted) => void;
76
+ }) => Subscribable<TEvent>;
77
+ export interface EventObservableLogicConfig<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject, TInputSchema extends StandardSchemaV1 = StandardSchemaV1> {
78
+ schemas?: {
79
+ input?: TInputSchema;
80
+ };
81
+ run: EventObservableLogicFunction<TEvent, TInput, TEmitted>;
82
+ }
45
83
  /**
46
84
  * Observable actor logic is described by an observable stream of values. Actors
47
85
  * created from observable logic (“observable actors”) can:
@@ -55,10 +93,10 @@ export type ObservableActorRef<TContext> = ActorRefFromLogic<ObservableActorLogi
55
93
  * @example
56
94
  *
57
95
  * ```ts
58
- * import { fromObservable, createActor } from 'xstate';
96
+ * import { createObservableLogic, createActor } from 'xstate';
59
97
  * import { interval } from 'rxjs';
60
98
  *
61
- * const logic = fromObservable((obj) => interval(1000));
99
+ * const logic = createObservableLogic((obj) => interval(1000));
62
100
  *
63
101
  * const actor = createActor(logic);
64
102
  *
@@ -86,12 +124,15 @@ export type ObservableActorRef<TContext> = ActorRefFromLogic<ObservableActorLogi
86
124
  * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
87
125
  * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
88
126
  */
89
- export declare function fromObservable<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(observableCreator: ({ input, system, self }: {
90
- input: TInput;
91
- system: AnyActorSystem;
92
- self: ObservableActorRef<TContext>;
93
- emit: (emitted: TEmitted) => void;
94
- }) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput, TEmitted>;
127
+ export declare function createObservableLogic<TContext, const TInputSchema extends StandardSchemaV1, TEmitted extends EventObject = EventObject>(config: ObservableLogicConfig<TContext, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema> & {
128
+ schemas: {
129
+ input: TInputSchema;
130
+ };
131
+ }): ObservableActorLogic<TContext, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted>;
132
+ export declare function createObservableLogic<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(config: ObservableLogicConfig<TContext, TInput, TEmitted> & {
133
+ schemas?: undefined;
134
+ }): ObservableActorLogic<TContext, TInput, TEmitted>;
135
+ export declare function createObservableLogic<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(observableCreator: ObservableLogicFunction<TContext, TInput, TEmitted>): ObservableActorLogic<TContext, TInput, TEmitted>;
95
136
  /**
96
137
  * Creates event observable logic that listens to an observable that delivers
97
138
  * event objects.
@@ -109,7 +150,7 @@ export declare function fromObservable<TContext, TInput extends NonReducibleUnkn
109
150
  *
110
151
  * ```ts
111
152
  * import {
112
- * fromEventObservable,
153
+ * createEventObservableLogic,
113
154
  * Subscribable,
114
155
  * EventObject,
115
156
  * createMachine,
@@ -117,7 +158,7 @@ export declare function fromObservable<TContext, TInput extends NonReducibleUnkn
117
158
  * } from 'xstate';
118
159
  * import { fromEvent } from 'rxjs';
119
160
  *
120
- * const mouseClickLogic = fromEventObservable(
161
+ * const mouseClickLogic = createEventObservableLogic(
121
162
  * () => fromEvent(document.body, 'click') as Subscribable<EventObject>
122
163
  * );
123
164
  *
@@ -143,9 +184,13 @@ export declare function fromObservable<TContext, TInput extends NonReducibleUnkn
143
184
  * It should return a {@link Subscribable}, which is compatible with an RxJS
144
185
  * Observable, although RxJS is not required to create them.
145
186
  */
146
- export declare function fromEventObservable<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(lazyObservable: ({ input, system, self, emit }: {
147
- input: TInput;
148
- system: AnyActorSystem;
149
- self: ObservableActorRef<TEvent>;
150
- emit: (emitted: TEmitted) => void;
151
- }) => Subscribable<TEvent>): ObservableActorLogic<TEvent, TInput, TEmitted>;
187
+ export declare function createEventObservableLogic<TEvent extends EventObject, const TInputSchema extends StandardSchemaV1, TEmitted extends EventObject = EventObject>(config: EventObservableLogicConfig<TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema> & {
188
+ schemas: {
189
+ input: TInputSchema;
190
+ };
191
+ }): ObservableActorLogic<TEvent, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted>;
192
+ export declare function createEventObservableLogic<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(config: EventObservableLogicConfig<TEvent, TInput, TEmitted> & {
193
+ schemas?: undefined;
194
+ }): ObservableActorLogic<TEvent, TInput, TEmitted>;
195
+ export declare function createEventObservableLogic<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(lazyObservable: EventObservableLogicFunction<TEvent, TInput, TEmitted>): ObservableActorLogic<TEvent, TInput, TEmitted>;
196
+ export {};
@@ -1,22 +1,74 @@
1
+ import { StandardSchemaV1 } from "../schema.types.js";
1
2
  import { AnyActorSystem } from "../system.js";
2
- import { ActorLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
3
- export type PromiseSnapshot<TOutput, TInput> = Snapshot<TOutput> & {
3
+ import { ActorLogic, ActorFromLogic, ActorRefFromLogic, EventObject, NonReducibleUnknown, Snapshot } from "../types.js";
4
+ export type AsyncSnapshot<TOutput, TInput> = Snapshot<TOutput> & {
4
5
  input: TInput | undefined;
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
- export type PromiseActorLogic<TOutput, TInput = unknown, TEmitted extends EventObject = EventObject> = ActorLogic<PromiseSnapshot<TOutput, TInput>, {
16
+ export type AsyncActorLogic<TOutput, TInput = unknown, TEmitted extends EventObject = EventObject> = ActorLogic<AsyncSnapshot<TOutput, TInput>, {
7
17
  type: string;
8
18
  [k: string]: unknown;
9
- }, TInput, // input
10
- AnyActorSystem, TEmitted>;
19
+ }, TInput, AnyActorSystem, TEmitted>;
20
+ export type AsyncActorRef<TOutput> = ActorRefFromLogic<AsyncActorLogic<TOutput, unknown>>;
21
+ type AsyncActor<TOutput, TInput = unknown, TEmitted extends EventObject = EventObject> = ActorFromLogic<AsyncActorLogic<TOutput, TInput, TEmitted>>;
22
+ export interface LogicArgs<TOutput, TInput> {
23
+ /** Data that was provided to the async actor. */
24
+ input: TInput;
25
+ /** The actor system to which the async actor belongs. */
26
+ system: AnyActorSystem;
27
+ /** The async actor. */
28
+ self: AsyncActor<TOutput, TInput>;
29
+ /** Aborted when the async actor is stopped or times out. */
30
+ signal: AbortSignal;
31
+ }
32
+ export interface LogicEnqueue<TEmitted extends EventObject> {
33
+ /** Emits an event that can be observed with `actor.on(...)`. */
34
+ emit: (emitted: TEmitted) => void;
35
+ /**
36
+ * Executes async work as a durable effect keyed by `key`.
37
+ *
38
+ * @experimental The durability semantics (step journaling, replay, and
39
+ * resumption across persistence) are not finalized and may change.
40
+ */
41
+ step: <TStepOutput>(key: string, exec: () => TStepOutput | PromiseLike<TStepOutput>) => Promise<TStepOutput>;
42
+ }
43
+ export type LogicFunction<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject> = (args: LogicArgs<TOutput, TInput>, enq: LogicEnqueue<TEmitted>) => PromiseLike<TOutput>;
44
+ export interface LogicConfig<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject, TInputSchema extends StandardSchemaV1 = StandardSchemaV1, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1> {
45
+ /**
46
+ * Stable identifier for this async logic. This identifies the logic, not a
47
+ * particular actor instance.
48
+ */
49
+ id?: string;
50
+ /** Schemas for inferring async logic types. */
51
+ schemas?: {
52
+ input?: TInputSchema;
53
+ output?: TOutputSchema;
54
+ };
55
+ /** Maximum time this async logic may run before it is aborted and errors. */
56
+ timeout?: number | string;
57
+ /** The async work to execute when the actor starts. */
58
+ run: LogicFunction<TOutput, TInput, TEmitted>;
59
+ }
60
+ export declare class TimeoutError extends Error {
61
+ constructor(timeout: number | string);
62
+ }
11
63
  /**
12
- * Represents an actor created by `fromPromise`.
64
+ * Represents an actor created by `createAsyncLogic`.
13
65
  *
14
66
  * The type of `self` within the actor's logic.
15
67
  *
16
68
  * @example
17
69
  *
18
70
  * ```ts
19
- * import { fromPromise, createActor } from 'xstate';
71
+ * import { createAsyncLogic, createActor } from 'xstate';
20
72
  *
21
73
  * // The actor's resolved output
22
74
  * type Output = string;
@@ -24,51 +76,59 @@ AnyActorSystem, TEmitted>;
24
76
  * type Input = { message: string };
25
77
  *
26
78
  * // Actor logic that fetches the url of an image of a cat saying `input.message`.
27
- * const logic = fromPromise<Output, Input>(async ({ input, self }) => {
28
- * self;
29
- * // ^? PromiseActorRef<Output, Input>
30
- *
31
- * const data = await fetch(
32
- * `https://cataas.com/cat/says/${input.message}`
33
- * );
34
- * const url = await data.json();
35
- * return url;
79
+ * const logic = createAsyncLogic<Output, Input>({
80
+ * run: async ({ input, self }, enq) => {
81
+ * self;
82
+ * // ^? AsyncActor<Output, Input>
83
+ * enq.emit({ type: 'request.start' });
84
+ *
85
+ * const data = await fetch(
86
+ * `https://cataas.com/cat/says/${input.message}`
87
+ * );
88
+ * const url = await data.json();
89
+ * return url;
90
+ * }
36
91
  * });
37
92
  *
38
93
  * const actor = createActor(logic, { input: { message: 'hello world' } });
39
- * // ^? PromiseActorRef<Output, Input>
94
+ * // ^? AsyncActor<Output, Input>
40
95
  * ```
41
96
  *
42
- * @see {@link fromPromise}
97
+ * @see {@link createAsyncLogic}
43
98
  */
44
- export type PromiseActorRef<TOutput> = ActorRefFromLogic<PromiseActorLogic<TOutput, unknown>>;
45
99
  /**
46
- * An actor logic creator which returns promise logic as defined by an async
100
+ * An actor logic creator which returns async logic as defined by an async
47
101
  * process that resolves or rejects after some time.
48
102
  *
49
- * Actors created from promise actor logic (“promise actors”) can:
103
+ * Actors created from async actor logic can:
50
104
  *
51
- * - Emit the resolved value of the promise
52
- * - Output the resolved value of the promise
105
+ * - Output the resolved value of the async process
106
+ * - Error with the rejected value of the async process
107
+ * - Abort when stopped or timed out
53
108
  *
54
- * Sending events to promise actors will have no effect.
109
+ * Sending events to async actors will have no effect.
55
110
  *
56
111
  * @example
57
112
  *
58
113
  * ```ts
59
- * const promiseLogic = fromPromise(async () => {
60
- * const result = await fetch('https://example.com/...').then((data) =>
61
- * data.json()
62
- * );
63
- *
64
- * return result;
114
+ * const asyncLogic = createAsyncLogic({
115
+ * id: 'fetch-user',
116
+ * timeout: '30s',
117
+ * run: async ({ signal }, enq) => {
118
+ * enq.emit({ type: 'request.start' });
119
+ * const result = await fetch('https://example.com/...', {
120
+ * signal
121
+ * }).then((data) => data.json());
122
+ *
123
+ * return result;
124
+ * }
65
125
  * });
66
126
  *
67
- * const promiseActor = createActor(promiseLogic);
68
- * promiseActor.subscribe((snapshot) => {
127
+ * const asyncActor = createActor(asyncLogic);
128
+ * asyncActor.subscribe((snapshot) => {
69
129
  * console.log(snapshot);
70
130
  * });
71
- * promiseActor.start();
131
+ * asyncActor.start();
72
132
  * // => {
73
133
  * // output: undefined,
74
134
  * // status: 'active'
@@ -83,22 +143,47 @@ export type PromiseActorRef<TOutput> = ActorRefFromLogic<PromiseActorLogic<TOutp
83
143
  * // }
84
144
  * ```
85
145
  *
86
- * @param promiseCreator A function which returns a Promise, and accepts an
87
- * object with the following properties:
146
+ * @param asyncLogic A config object with a `run` function which returns a
147
+ * Promise, and accepts an object with the following properties:
148
+ *
149
+ * - `input` - Data that was provided to the async actor
150
+ * - `self` - The async actor ref
151
+ * - `system` - The actor system to which the async actor belongs
152
+ * - `signal` - An abort signal for cancellation
88
153
  *
89
- * - `input` - Data that was provided to the promise actor
90
- * - `self` - The parent actor of the promise actor
91
- * - `system` - The actor system to which the promise actor belongs
154
+ * `run` also receives an `enq` object with the following properties:
155
+ *
156
+ * - `emit` - Emits an event that can be observed with `actor.on(...)`
92
157
  *
93
158
  * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
94
159
  */
95
- export declare function fromPromise<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(promiseCreator: ({ input, system, self, signal, emit }: {
96
- /** Data that was provided to the promise actor */
97
- input: TInput;
98
- /** The actor system to which the promise actor belongs */
99
- system: AnyActorSystem;
100
- /** The parent actor of the promise actor */
101
- self: PromiseActorRef<TOutput>;
102
- signal: AbortSignal;
103
- emit: (emitted: TEmitted) => void;
104
- }) => PromiseLike<TOutput>): PromiseActorLogic<TOutput, TInput, TEmitted>;
160
+ export declare function createAsyncLogic<const TInputSchema extends StandardSchemaV1, const TOutputSchema extends StandardSchemaV1, TEmitted extends EventObject = EventObject>(asyncLogic: LogicConfig<StandardSchemaV1.InferOutput<TOutputSchema>, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema, TOutputSchema> & {
161
+ schemas: {
162
+ input: TInputSchema;
163
+ output: TOutputSchema;
164
+ };
165
+ }): AsyncActorLogic<StandardSchemaV1.InferOutput<TOutputSchema>, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted> & {
166
+ id?: string;
167
+ };
168
+ export declare function createAsyncLogic<TOutput, const TInputSchema extends StandardSchemaV1, TEmitted extends EventObject = EventObject>(asyncLogic: LogicConfig<TOutput, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted, TInputSchema> & {
169
+ schemas: {
170
+ input: TInputSchema;
171
+ output?: never;
172
+ };
173
+ }): AsyncActorLogic<TOutput, StandardSchemaV1.InferOutput<TInputSchema>, TEmitted> & {
174
+ id?: string;
175
+ };
176
+ export declare function createAsyncLogic<const TOutputSchema extends StandardSchemaV1, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(asyncLogic: LogicConfig<StandardSchemaV1.InferOutput<TOutputSchema>, TInput, TEmitted, StandardSchemaV1, TOutputSchema> & {
177
+ schemas: {
178
+ input?: never;
179
+ output: TOutputSchema;
180
+ };
181
+ }): AsyncActorLogic<StandardSchemaV1.InferOutput<TOutputSchema>, TInput, TEmitted> & {
182
+ id?: string;
183
+ };
184
+ export declare function createAsyncLogic<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(asyncLogic: Omit<LogicConfig<TOutput, TInput, TEmitted>, 'schemas'> & {
185
+ schemas?: undefined;
186
+ }): AsyncActorLogic<TOutput, TInput, TEmitted> & {
187
+ id?: string;
188
+ };
189
+ export {};
@@ -0,0 +1,22 @@
1
+ import { AnyActorSystem } from "../system.js";
2
+ import { ActorLogic, ActorRefFromLogic, AnyActorRef, EventObject, Snapshot } from "../types.js";
3
+ export type SubscriptionSnapshot = Snapshot<undefined> & {
4
+ input: SubscriptionInput<any, any, any, any>;
5
+ };
6
+ export interface SubscriptionMappers<TSnapshot extends Snapshot<unknown>, TOutput, TMappedEvent extends EventObject> {
7
+ snapshot?: (snapshot: TSnapshot) => TMappedEvent;
8
+ done?: (output: TOutput) => TMappedEvent;
9
+ error?: (error: unknown) => TMappedEvent;
10
+ }
11
+ export interface SubscriptionInput<TSnapshot extends Snapshot<unknown>, TOutput, TMappedEvent extends EventObject, TMappers extends SubscriptionMappers<TSnapshot, TOutput, TMappedEvent>> {
12
+ actor: AnyActorRef;
13
+ mappers: TMappers;
14
+ }
15
+ export type SubscriptionActorLogic<TSnapshot extends Snapshot<unknown> = Snapshot<unknown>, TOutput = unknown, TMappedEvent extends EventObject = EventObject> = ActorLogic<SubscriptionSnapshot, EventObject, SubscriptionInput<TSnapshot, TOutput, TMappedEvent, SubscriptionMappers<TSnapshot, TOutput, TMappedEvent>>, AnyActorSystem, EventObject>;
16
+ export type SubscriptionActorRef<TSnapshot extends Snapshot<unknown> = Snapshot<unknown>, TOutput = unknown, TMappedEvent extends EventObject = EventObject> = ActorRefFromLogic<SubscriptionActorLogic<TSnapshot, TOutput, TMappedEvent>>;
17
+ /**
18
+ * Creates actor logic for subscribing to lifecycle events (done/error/snapshot)
19
+ * from another actor. Used internally by `enq.subscribeTo()`.
20
+ */
21
+ export declare function createSubscriptionLogic<TSnapshot extends Snapshot<unknown> = Snapshot<unknown>, TOutput = unknown, TMappedEvent extends EventObject = EventObject>(): SubscriptionActorLogic<TSnapshot, TOutput, TMappedEvent>;
22
+ export declare const subscriptionLogic: SubscriptionActorLogic<Snapshot<unknown>, unknown, EventObject>;
@@ -0,0 +1,81 @@
1
+ type Observer<T> = {
2
+ next?: (value: T) => void;
3
+ error?: (err: unknown) => void;
4
+ complete?: () => void;
5
+ };
6
+ interface Subscription {
7
+ unsubscribe(): void;
8
+ }
9
+ interface Readable<T> extends Subscribable<T> {
10
+ get: () => T;
11
+ }
12
+ interface Subscribable<T> {
13
+ subscribe(observer: Observer<T>): Subscription;
14
+ subscribe(next: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;
15
+ }
16
+ export interface BaseAtom<T> extends Subscribable<T>, Readable<T> {
17
+ }
18
+ export interface Atom<T> extends BaseAtom<T> {
19
+ set: (valueOrFn: T | ((prev: T) => T)) => void;
20
+ }
21
+ export interface ReducerAtom<TState, TEvent> extends BaseAtom<TState> {
22
+ send: (event: TEvent) => void;
23
+ }
24
+ export interface AtomOptions<T> {
25
+ compare?: (a: T, b: T) => boolean;
26
+ }
27
+ export interface AtomConfig<TValue, TInput> {
28
+ createAtom: undefined extends TInput ? (input?: TInput) => Atom<TValue> : (input: TInput) => Atom<TValue>;
29
+ }
30
+ export type AnyAtomConfig = AtomConfig<any, any>;
31
+ export type ValueFromAtomConfig<TConfig extends AnyAtomConfig> = TConfig extends AtomConfig<infer TValue, any> ? TValue : never;
32
+ export type InputFromAtomConfig<TConfig extends AnyAtomConfig> = TConfig extends AtomConfig<any, infer TInput> ? TInput : never;
33
+ export type AnyAtom = BaseAtom<any>;
34
+ export interface ReadonlyAtom<T> extends BaseAtom<T> {
35
+ }
36
+ /**
37
+ * Returns `true` if the value is an atom (writable, computed, async, or
38
+ * reducer). Lets actor-consuming APIs (e.g. `enq.subscribeTo`) accept atoms
39
+ * directly.
40
+ */
41
+ export declare function isAtom(value: unknown): value is AnyAtom;
42
+ /** The current state of an async atom. */
43
+ export type AsyncAtomState<Data, Error = unknown> = {
44
+ status: 'pending';
45
+ } | {
46
+ status: 'done';
47
+ data: Data;
48
+ } | {
49
+ status: 'error';
50
+ error: Error;
51
+ };
52
+ /** Options passed to an async atom getter. */
53
+ export interface AsyncAtomOptions {
54
+ /** Signal aborted when the async atom recomputes before this run settles. */
55
+ signal: AbortSignal;
56
+ }
57
+ /**
58
+ * Creates a read-only atom whose value is loaded from an async getter.
59
+ *
60
+ * The getter receives an `AbortSignal`; when the async atom recomputes, the
61
+ * previous signal is aborted and stale resolutions are ignored.
62
+ */
63
+ export declare function createAsyncAtom<T>(getValue: (options: AsyncAtomOptions) => Promise<T>, options?: AtomOptions<AsyncAtomState<T>>): ReadonlyAtom<AsyncAtomState<T>>;
64
+ /**
65
+ * Creates an atom.
66
+ *
67
+ * Pass a value for a writable atom or a getter for a computed read-only atom.
68
+ */
69
+ export declare function createAtom<T>(getValue: (prev?: T) => T, options?: AtomOptions<T>): ReadonlyAtom<T>;
70
+ export declare function createAtom<T>(initialValue: T, options?: AtomOptions<T>): Atom<T>;
71
+ /**
72
+ * Creates an inert atom config that can be instantiated later.
73
+ *
74
+ * Useful for framework hooks that need to create a stable local atom from
75
+ * component input.
76
+ */
77
+ export declare function createAtomConfig<T, TInput>(getInitialValue: (input: TInput) => T, options?: AtomOptions<T>): AtomConfig<T, TInput>;
78
+ export declare function createAtomConfig<T>(initialValue: T, options?: AtomOptions<T>): AtomConfig<T, undefined>;
79
+ /** Creates an atom whose updates are handled by a reducer function. */
80
+ export declare function createReducerAtom<TState, TEvent>(initialValue: TState, reducer: (state: TState, event: TEvent) => TState, options?: AtomOptions<TState>): ReducerAtom<TState, TEvent>;
81
+ export {};
@@ -1,8 +1,7 @@
1
1
  import { symbolObservable } from "./symbolObservable.js";
2
2
  import { AnyActorSystem, Clock } from "./system.js";
3
- export declare let executingCustomAction: boolean;
4
- import type { AnyActorLogic, AnyActorRef, ConditionalRequired, EmittedFrom, EventFromLogic, InputFrom, IsNotNever, Readable, Snapshot, SnapshotFrom } from "./types.js";
5
- import { ActorOptions, ActorRef, InteropSubscribable, Observer, Subscription } from "./types.js";
3
+ import type { AnyActor, AnyActorLogic, EmittedFrom, EventFromLogic, SendableEventFromLogic, InputFrom, Snapshot, SnapshotFrom, Readable } from "./types.js";
4
+ import { ActorOptions, ActorInstance, ActorRef, InteropSubscribable, Observer, Subscription } from "./types.js";
6
5
  export declare const $$ACTOR_TYPE = 1;
7
6
  export declare enum ProcessingStatus {
8
7
  NotStarted = 0,
@@ -13,8 +12,13 @@ export declare enum ProcessingStatus {
13
12
  * An Actor is a running process that can receive events, send events and change
14
13
  * its behavior based on the events it receives, which can cause effects outside
15
14
  * of the actor. When you run a state machine, it becomes an actor.
15
+ *
16
+ * An `Actor` is the concrete runtime instance with lifecycle methods and
17
+ * system-owned internals. It also satisfies the narrower `ActorRef` contract,
18
+ * so consumer APIs should accept `ActorRef` when they only need to send events
19
+ * or read snapshots.
16
20
  */
17
- export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>> {
21
+ export declare class Actor<TLogic extends AnyActorLogic> implements ActorInstance<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>, SendableEventFromLogic<TLogic>> {
18
22
  logic: TLogic;
19
23
  /** The current internal state of the actor. */
20
24
  private _snapshot;
@@ -26,19 +30,26 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
26
30
  options: Readonly<ActorOptions<TLogic>>;
27
31
  /** The unique identifier for this actor relative to its parent. */
28
32
  id: string;
33
+ private readonly _boundProcess;
29
34
  private mailbox;
30
35
  private observers;
31
36
  private eventListeners;
32
37
  private logger;
33
- _parent?: AnyActorRef;
34
- ref: ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>>;
38
+ _parent?: AnyActor;
39
+ ref: ActorRef<SnapshotFrom<TLogic>, EventFromLogic<TLogic>, EmittedFrom<TLogic>, SendableEventFromLogic<TLogic>>;
35
40
  private _actorScope;
36
41
  systemId: string | undefined;
37
42
  /** The globally unique process ID for this invocation. */
38
43
  sessionId: string;
39
44
  /** The system to which this actor belongs. */
40
45
  system: AnyActorSystem;
41
- private _doneEvent?;
46
+ trigger: {
47
+ [K in SendableEventFromLogic<TLogic>['type']]: {} extends Omit<Extract<SendableEventFromLogic<TLogic>, {
48
+ type: K;
49
+ }>, 'type'> ? () => void : (payload: Omit<Extract<SendableEventFromLogic<TLogic>, {
50
+ type: K;
51
+ }>, 'type'>) => void;
52
+ };
42
53
  src: string | AnyActorLogic;
43
54
  /**
44
55
  * Creates a new actor instance for the given logic with the provided options,
@@ -48,8 +59,10 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
48
59
  * @param options Actor options
49
60
  */
50
61
  constructor(logic: TLogic, options?: ActorOptions<TLogic>);
51
- private _initState;
52
62
  private _deferred;
63
+ private _pendingEffects?;
64
+ private _setErrorSnapshot;
65
+ private _next;
53
66
  private update;
54
67
  /**
55
68
  * Subscribe an observer to an actor’s snapshot values.
@@ -119,11 +132,9 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
119
132
  /** Starts the Actor from the initial state */
120
133
  start(): this;
121
134
  private _process;
122
- private _stop;
123
135
  /** Stops the Actor and unsubscribe all listeners. */
124
136
  stop(): this;
125
137
  private _complete;
126
- private _reportError;
127
138
  private _error;
128
139
  private _stopProcedure;
129
140
  /**
@@ -131,8 +142,7 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
131
142
  *
132
143
  * @param event The event to send
133
144
  */
134
- send(event: EventFromLogic<TLogic>): void;
135
- private attachDevTools;
145
+ send(event: SendableEventFromLogic<TLogic>): void;
136
146
  toJSON(): {
137
147
  xstate$$type: number;
138
148
  id: string;
@@ -162,11 +172,13 @@ export declare class Actor<TLogic extends AnyActorLogic> implements ActorRef<Sna
162
172
  * may emit a snapshot when a state transition occurs.
163
173
  *
164
174
  * Note that some actors, such as callback actors generated with
165
- * `fromCallback`, will not emit snapshots.
175
+ * `createCallbackLogic`, will not emit snapshots.
166
176
  * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
167
177
  * @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot).
168
178
  */
169
179
  getSnapshot(): SnapshotFrom<TLogic>;
180
+ /** Read the actor's current snapshot as an atom value. */
181
+ get(): SnapshotFrom<TLogic>;
170
182
  }
171
183
  export type RequiredActorOptionsKeys<TLogic extends AnyActorLogic> = undefined extends InputFrom<TLogic> ? never : 'input';
172
184
  /**
@@ -203,25 +215,11 @@ export type RequiredActorOptionsKeys<TLogic extends AnyActorLogic> = undefined e
203
215
  *
204
216
  * @param logic - The actor logic to create an actor from. For a state machine
205
217
  * actor logic creator, see {@link createMachine}. Other actor logic creators
206
- * include {@link fromCallback}, {@link fromEventObservable},
207
- * {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
218
+ * include {@link createCallbackLogic}, {@link createEventObservableLogic},
219
+ * {@link createObservableLogic}, {@link createLogic}, and
220
+ * {@link createAsyncLogic}.
208
221
  * @param options - Actor options
209
222
  */
210
- export declare function createActor<TLogic extends AnyActorLogic>(logic: TLogic, ...[options]: ConditionalRequired<[
211
- options?: ActorOptions<TLogic> & {
212
- [K in RequiredActorOptionsKeys<TLogic>]: unknown;
213
- }
214
- ], IsNotNever<RequiredActorOptionsKeys<TLogic>>>): Actor<TLogic>;
215
- /**
216
- * Creates a new Interpreter instance for the given machine with the provided
217
- * options, if any.
218
- *
219
- * @deprecated Use `createActor` instead
220
- * @alias
221
- */
222
- export declare const interpret: typeof createActor;
223
- /**
224
- * @deprecated Use `Actor` instead.
225
- * @alias
226
- */
227
- export type Interpreter = typeof Actor;
223
+ export declare function createActor<TLogic extends AnyActorLogic>(logic: TLogic, options?: ActorOptions<TLogic> & {
224
+ [K in RequiredActorOptionsKeys<TLogic>]: unknown;
225
+ }): Actor<TLogic>;