xstate 5.32.1 → 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 -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-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 -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,401 +0,0 @@
1
- import { W as XSTATE_ERROR, Y as createErrorActorEvent, D as evaluateGuard, a as cancel, r as raise, b as spawnChild, s as stopChild } from './raise-e974d1c9.esm.js';
2
- import { a as assign } from './assign-2aa58daa.esm.js';
3
-
4
- function resolveEmit(_, snapshot, args, actionParams, {
5
- event: eventOrExpr
6
- }) {
7
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
8
- return [snapshot, {
9
- event: resolvedEvent
10
- }, undefined];
11
- }
12
- function executeEmit(actorScope, {
13
- event
14
- }) {
15
- actorScope.defer(() => actorScope.emit(event));
16
- }
17
- /**
18
- * Emits an event to event handlers registered on the actor via `actor.on(event,
19
- * handler)`.
20
- *
21
- * @example
22
- *
23
- * ```ts
24
- * import { emit } from 'xstate';
25
- *
26
- * const machine = createMachine({
27
- * // ...
28
- * on: {
29
- * something: {
30
- * actions: emit({
31
- * type: 'emitted',
32
- * some: 'data'
33
- * })
34
- * }
35
- * }
36
- * // ...
37
- * });
38
- *
39
- * const actor = createActor(machine).start();
40
- *
41
- * actor.on('emitted', (event) => {
42
- * console.log(event);
43
- * });
44
- *
45
- * actor.send({ type: 'something' });
46
- * // logs:
47
- * // {
48
- * // type: 'emitted',
49
- * // some: 'data'
50
- * // }
51
- * ```
52
- */
53
- function emit(/** The event to emit, or an expression that returns an event to emit. */
54
- eventOrExpr) {
55
- function emit(_args, _params) {
56
- }
57
- emit.type = 'xstate.emit';
58
- emit.event = eventOrExpr;
59
- emit.resolve = resolveEmit;
60
- emit.execute = executeEmit;
61
- return emit;
62
- }
63
-
64
- // this is needed to make JSDoc `@link` work properly
65
-
66
- /**
67
- * @remarks
68
- * `T | unknown` reduces to `unknown` and that can be problematic when it comes
69
- * to contextual typing. It especially is a problem when the union has a
70
- * function member, like here:
71
- *
72
- * ```ts
73
- * declare function test(
74
- * cbOrVal: ((arg: number) => unknown) | unknown
75
- * ): void;
76
- * test((arg) => {}); // oops, implicit any
77
- * ```
78
- *
79
- * This type can be used to avoid this problem. This union represents the same
80
- * value space as `unknown`.
81
- */
82
-
83
- // https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
84
-
85
- // @TODO: we can't use native `NoInfer` as we need those:
86
- // https://github.com/microsoft/TypeScript/pull/61092
87
- // https://github.com/microsoft/TypeScript/pull/61077
88
- // but even with those fixes native NoInfer still doesn't work - further issues have to be reproduced and fixed
89
-
90
- /** @deprecated Use the built-in `NoInfer` type instead */
91
-
92
- /** The full definition of an event, with a string `type`. */
93
-
94
- /**
95
- * The string or object representing the state value relative to the parent
96
- * state node.
97
- *
98
- * @remarks
99
- * - For a child atomic state node, this is a string, e.g., `"pending"`.
100
- * - For complex state nodes, this is an object, e.g., `{ success:
101
- * "someChildState" }`.
102
- */
103
-
104
- /** @deprecated Use `AnyMachineSnapshot` instead */
105
-
106
- // TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
107
- /** @ignore */
108
-
109
- /**
110
- * Runtime options for state machine execution.
111
- *
112
- * @example
113
- *
114
- * ```ts
115
- * const machine = createMachine({
116
- * // ... machine config
117
- * options: {
118
- * maxIterations: 5000
119
- * // other runtime options can be added here
120
- * }
121
- * });
122
- * ```
123
- */
124
-
125
- let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
126
- SpecialTargets["Parent"] = "#_parent";
127
- SpecialTargets["Internal"] = "#_internal";
128
- return SpecialTargets;
129
- }({});
130
-
131
- /** @deprecated Use `AnyActor` instead. */
132
-
133
- // Based on RxJS types
134
-
135
- // TODO: in v6, this should only accept AnyActorLogic, like ActorRefFromLogic
136
-
137
- /** @deprecated Use `Actor<T>` instead. */
138
-
139
- /**
140
- * Represents logic which can be used by an actor.
141
- *
142
- * @template TSnapshot - The type of the snapshot.
143
- * @template TEvent - The type of the event object.
144
- * @template TInput - The type of the input.
145
- * @template TSystem - The type of the actor system.
146
- */
147
-
148
- // Actors that don't have literal string IDs — these are the only ones
149
- // that should appear in the index signature fallback, since actors with
150
- // literal IDs are already covered by ToConcreteChildren.
151
-
152
- /** @deprecated */
153
-
154
- // TODO: cover all that can be actually returned
155
-
156
- function resolveSendTo(actorScope, snapshot, args, actionParams, {
157
- to,
158
- event: eventOrExpr,
159
- id,
160
- delay
161
- }, extra) {
162
- const delaysMap = snapshot.machine.implementations.delays;
163
- if (typeof eventOrExpr === 'string') {
164
- throw new Error(
165
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
166
- `Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
167
- }
168
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
169
- let resolvedDelay;
170
- if (typeof delay === 'string') {
171
- const configDelay = delaysMap && delaysMap[delay];
172
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
173
- } else {
174
- resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
175
- }
176
- const resolvedTarget = typeof to === 'function' ? to(args, actionParams) : to;
177
- let targetActorRef;
178
- if (typeof resolvedTarget === 'string') {
179
- // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
180
- if (resolvedTarget === SpecialTargets.Parent) {
181
- targetActorRef = actorScope.self._parent;
182
- }
183
- // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
184
- else if (resolvedTarget === SpecialTargets.Internal) {
185
- targetActorRef = actorScope.self;
186
- } else if (resolvedTarget.startsWith('#_')) {
187
- // SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
188
- // #_invokeid. If the target is the special term '#_invokeid', where invokeid is the invokeid of an SCXML session that the sending session has created by <invoke>, the Processor must add the event to the external queue of that session.
189
- targetActorRef = snapshot.children[resolvedTarget.slice(2)];
190
- } else {
191
- targetActorRef = extra.deferredActorIds?.includes(resolvedTarget) ? resolvedTarget : snapshot.children[resolvedTarget];
192
- }
193
- if (!targetActorRef) {
194
- throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${snapshot.machine.id}'.`);
195
- }
196
- } else {
197
- targetActorRef = resolvedTarget || actorScope.self;
198
- }
199
- return [snapshot, {
200
- to: targetActorRef,
201
- targetId: typeof resolvedTarget === 'string' ? resolvedTarget : undefined,
202
- event: resolvedEvent,
203
- id,
204
- delay: resolvedDelay
205
- }, undefined];
206
- }
207
- function retryResolveSendTo(_, snapshot, params) {
208
- if (typeof params.to === 'string') {
209
- params.to = snapshot.children[params.to];
210
- }
211
- }
212
- function executeSendTo(actorScope, params) {
213
- // this forms an outgoing events queue
214
- // thanks to that the recipient actors are able to read the *updated* snapshot value of the sender
215
- actorScope.defer(() => {
216
- const {
217
- to,
218
- event,
219
- delay,
220
- id
221
- } = params;
222
- if (typeof delay === 'number') {
223
- actorScope.system.scheduler.schedule(actorScope.self, to, event, delay, id);
224
- return;
225
- }
226
- actorScope.system._relay(actorScope.self,
227
- // at this point, in a deferred task, it should already be mutated by retryResolveSendTo
228
- // if it initially started as a string
229
- to, event.type === XSTATE_ERROR ? createErrorActorEvent(actorScope.self.id, event.data) : event);
230
- });
231
- }
232
- /**
233
- * Sends an event to an actor.
234
- *
235
- * @param actor The `ActorRef` to send the event to.
236
- * @param event The event to send, or an expression that evaluates to the event
237
- * to send
238
- * @param options Send action options
239
- *
240
- * - `id` - The unique send event identifier (used with `cancel()`).
241
- * - `delay` - The number of milliseconds to delay the sending of the event.
242
- */
243
- function sendTo(to, eventOrExpr, options) {
244
- function sendTo(_args, _params) {
245
- }
246
- sendTo.type = 'xstate.sendTo';
247
- sendTo.to = to;
248
- sendTo.event = eventOrExpr;
249
- sendTo.id = options?.id;
250
- sendTo.delay = options?.delay;
251
- sendTo.resolve = resolveSendTo;
252
- sendTo.retryResolve = retryResolveSendTo;
253
- sendTo.execute = executeSendTo;
254
- return sendTo;
255
- }
256
-
257
- /**
258
- * Sends an event to this machine's parent.
259
- *
260
- * @param event The event to send to the parent machine.
261
- * @param options Options to pass into the send event.
262
- */
263
- function sendParent(event, options) {
264
- return sendTo(SpecialTargets.Parent, event, options);
265
- }
266
- /**
267
- * Forwards (sends) an event to the `target` actor.
268
- *
269
- * @param target The target actor to forward the event to.
270
- * @param options Options to pass into the send action creator.
271
- */
272
- function forwardTo(target, options) {
273
- return sendTo(target, ({
274
- event
275
- }) => event, options);
276
- }
277
-
278
- function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
279
- collect
280
- }) {
281
- const actions = [];
282
- const enqueue = function enqueue(action) {
283
- actions.push(action);
284
- };
285
- enqueue.assign = (...args) => {
286
- actions.push(assign(...args));
287
- };
288
- enqueue.cancel = (...args) => {
289
- actions.push(cancel(...args));
290
- };
291
- enqueue.raise = (...args) => {
292
- // for some reason it fails to infer `TDelay` from `...args` here and picks its default (`never`)
293
- // then it fails to typecheck that because `...args` use `string` in place of `TDelay`
294
- actions.push(raise(...args));
295
- };
296
- enqueue.sendTo = (...args) => {
297
- // for some reason it fails to infer `TDelay` from `...args` here and picks its default (`never`)
298
- // then it fails to typecheck that because `...args` use `string` in place of `TDelay
299
- actions.push(sendTo(...args));
300
- };
301
- enqueue.sendParent = (...args) => {
302
- actions.push(sendParent(...args));
303
- };
304
- enqueue.spawnChild = (...args) => {
305
- actions.push(spawnChild(...args));
306
- };
307
- enqueue.stopChild = (...args) => {
308
- actions.push(stopChild(...args));
309
- };
310
- enqueue.emit = (...args) => {
311
- actions.push(emit(...args));
312
- };
313
- collect({
314
- context: args.context,
315
- event: args.event,
316
- enqueue,
317
- check: guard => evaluateGuard(guard, snapshot.context, args.event, snapshot),
318
- self: actorScope.self,
319
- system: actorScope.system
320
- }, actionParams);
321
- return [snapshot, undefined, actions];
322
- }
323
- /**
324
- * Creates an action object that will execute actions that are queued by the
325
- * `enqueue(action)` function.
326
- *
327
- * @example
328
- *
329
- * ```ts
330
- * import { createMachine, enqueueActions } from 'xstate';
331
- *
332
- * const machine = createMachine({
333
- * entry: enqueueActions(({ enqueue, check }) => {
334
- * enqueue.assign({ count: 0 });
335
- *
336
- * if (check('someGuard')) {
337
- * enqueue.assign({ count: 1 });
338
- * }
339
- *
340
- * enqueue('someAction');
341
- * })
342
- * });
343
- * ```
344
- */
345
- function enqueueActions(collect) {
346
- function enqueueActions(_args, _params) {
347
- }
348
- enqueueActions.type = 'xstate.enqueueActions';
349
- enqueueActions.collect = collect;
350
- enqueueActions.resolve = resolveEnqueueActions;
351
- return enqueueActions;
352
- }
353
-
354
- function resolveLog(_, snapshot, actionArgs, actionParams, {
355
- value,
356
- label
357
- }) {
358
- return [snapshot, {
359
- value: typeof value === 'function' ? value(actionArgs, actionParams) : value,
360
- label
361
- }, undefined];
362
- }
363
- function executeLog({
364
- logger
365
- }, {
366
- value,
367
- label
368
- }) {
369
- if (label) {
370
- logger(label, value);
371
- } else {
372
- logger(value);
373
- }
374
- }
375
- /**
376
- * @param expr The expression function to evaluate which will be logged. Takes
377
- * in 2 arguments:
378
- *
379
- * - `ctx` - the current state context
380
- * - `event` - the event that caused this action to be executed.
381
- *
382
- * @param label The label to give to the logged expression.
383
- */
384
- function log(value = ({
385
- context,
386
- event
387
- }) => ({
388
- context,
389
- event
390
- }), label) {
391
- function log(_args, _params) {
392
- }
393
- log.type = 'xstate.log';
394
- log.value = value;
395
- log.label = label;
396
- log.resolve = resolveLog;
397
- log.execute = executeLog;
398
- return log;
399
- }
400
-
401
- export { SpecialTargets as S, emit as a, sendParent as b, enqueueActions as e, forwardTo as f, log as l, sendTo as s };