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