xstate 5.32.2 → 6.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +25 -24
  2. package/dist/StateMachine-0aa98f54.development.esm.js +746 -0
  3. package/dist/StateMachine-aef700df.cjs.js +732 -0
  4. package/dist/StateMachine-d4931336.esm.js +729 -0
  5. package/dist/StateMachine-da872dde.development.cjs.js +749 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +40 -26
  8. package/dist/declarations/src/StateNode.d.ts +18 -54
  9. package/dist/declarations/src/actions.d.ts +14 -9
  10. package/dist/declarations/src/actors/callback.d.ts +42 -16
  11. package/dist/declarations/src/actors/index.d.ts +9 -6
  12. package/dist/declarations/src/actors/listener.d.ts +18 -0
  13. package/dist/declarations/src/actors/logic.d.ts +105 -0
  14. package/dist/declarations/src/actors/observable.d.ts +72 -27
  15. package/dist/declarations/src/actors/promise.d.ts +133 -48
  16. package/dist/declarations/src/actors/subscription.d.ts +22 -0
  17. package/dist/declarations/src/createActor.d.ts +33 -34
  18. package/dist/declarations/src/createMachine.d.ts +38 -7
  19. package/dist/declarations/src/createMachineFromConfig.d.ts +177 -0
  20. package/dist/declarations/src/graph/graph.d.ts +6 -4
  21. package/dist/declarations/src/graph/shortestPaths.d.ts +2 -2
  22. package/dist/declarations/src/graph/simplePaths.d.ts +2 -2
  23. package/dist/declarations/src/index.d.ts +13 -9
  24. package/dist/declarations/src/inspection.d.ts +84 -30
  25. package/dist/declarations/src/mapState.d.ts +4 -2
  26. package/dist/declarations/src/schema.types.d.ts +90 -0
  27. package/dist/declarations/src/serialize.d.ts +50 -0
  28. package/dist/declarations/src/setup.d.ts +396 -136
  29. package/dist/declarations/src/spawn.d.ts +7 -32
  30. package/dist/declarations/src/stateUtils.d.ts +22 -19
  31. package/dist/declarations/src/system.d.ts +8 -7
  32. package/dist/declarations/src/transition.d.ts +8 -5
  33. package/dist/declarations/src/transitionActions.d.ts +4 -0
  34. package/dist/declarations/src/types.d.ts +494 -429
  35. package/dist/declarations/src/types.v6.d.ts +423 -0
  36. package/dist/declarations/src/utils.d.ts +5 -3
  37. package/dist/index-219cb621.development.cjs.js +4476 -0
  38. package/dist/index-2f2fbd9b.development.esm.js +4421 -0
  39. package/dist/index-559ceab3.cjs.js +4437 -0
  40. package/dist/index-8828376f.esm.js +4382 -0
  41. package/dist/xstate-actors.cjs.js +12 -838
  42. package/dist/xstate-actors.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.cjs.js +12 -838
  44. package/dist/xstate-actors.development.cjs.mjs +10 -5
  45. package/dist/xstate-actors.development.esm.js +1 -836
  46. package/dist/xstate-actors.esm.js +1 -836
  47. package/dist/xstate-actors.umd.min.js +1 -1
  48. package/dist/xstate-actors.umd.min.js.map +1 -1
  49. package/dist/xstate-graph.cjs.js +18 -29
  50. package/dist/xstate-graph.development.cjs.js +18 -29
  51. package/dist/xstate-graph.development.esm.js +15 -26
  52. package/dist/xstate-graph.esm.js +15 -26
  53. package/dist/xstate-graph.umd.min.js +1 -1
  54. package/dist/xstate-graph.umd.min.js.map +1 -1
  55. package/dist/xstate.cjs.js +1477 -122
  56. package/dist/xstate.cjs.mjs +19 -22
  57. package/dist/xstate.development.cjs.js +1477 -122
  58. package/dist/xstate.development.cjs.mjs +19 -22
  59. package/dist/xstate.development.esm.js +1442 -86
  60. package/dist/xstate.esm.js +1442 -86
  61. package/dist/xstate.umd.min.js +1 -1
  62. package/dist/xstate.umd.min.js.map +1 -1
  63. package/package.json +3 -47
  64. package/dist/StateMachine-23c204bb.development.esm.js +0 -566
  65. package/dist/StateMachine-67a0f463.development.cjs.js +0 -569
  66. package/dist/StateMachine-94d215e3.cjs.js +0 -563
  67. package/dist/StateMachine-d1d80308.esm.js +0 -560
  68. package/dist/assign-43b1b558.development.esm.js +0 -133
  69. package/dist/assign-54b312bc.esm.js +0 -127
  70. package/dist/assign-b5bc78f7.cjs.js +0 -129
  71. package/dist/assign-f8f66278.development.cjs.js +0 -135
  72. package/dist/declarations/src/actions/assign.d.ts +0 -45
  73. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  74. package/dist/declarations/src/actions/emit.d.ts +0 -44
  75. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  76. package/dist/declarations/src/actions/log.d.ts +0 -16
  77. package/dist/declarations/src/actions/raise.d.ts +0 -21
  78. package/dist/declarations/src/actions/send.d.ts +0 -42
  79. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  80. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  81. package/dist/declarations/src/actors/transition.d.ts +0 -143
  82. package/dist/declarations/src/dev/index.d.ts +0 -14
  83. package/dist/declarations/src/guards.d.ts +0 -128
  84. package/dist/log-9b02c355.esm.js +0 -401
  85. package/dist/log-c94995c7.cjs.js +0 -409
  86. package/dist/log-d9f3f459.development.cjs.js +0 -437
  87. package/dist/log-db444cad.development.esm.js +0 -429
  88. package/dist/raise-235108c6.esm.js +0 -2762
  89. package/dist/raise-45f11c00.development.esm.js +0 -2824
  90. package/dist/raise-c445379d.cjs.js +0 -2817
  91. package/dist/raise-eca73993.development.cjs.js +0 -2879
  92. package/dist/xstate-actions.cjs.d.mts +0 -2
  93. package/dist/xstate-actions.cjs.d.ts +0 -2
  94. package/dist/xstate-actions.cjs.js +0 -23
  95. package/dist/xstate-actions.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.cjs.js +0 -23
  97. package/dist/xstate-actions.development.cjs.mjs +0 -14
  98. package/dist/xstate-actions.development.esm.js +0 -4
  99. package/dist/xstate-actions.esm.js +0 -4
  100. package/dist/xstate-actions.umd.min.js +0 -2
  101. package/dist/xstate-actions.umd.min.js.map +0 -1
  102. package/dist/xstate-dev.cjs.d.mts +0 -2
  103. package/dist/xstate-dev.cjs.d.ts +0 -2
  104. package/dist/xstate-dev.cjs.js +0 -48
  105. package/dist/xstate-dev.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.cjs.js +0 -51
  107. package/dist/xstate-dev.development.cjs.mjs +0 -5
  108. package/dist/xstate-dev.development.esm.js +0 -45
  109. package/dist/xstate-dev.esm.js +0 -42
  110. package/dist/xstate-dev.umd.min.js +0 -2
  111. package/dist/xstate-dev.umd.min.js.map +0 -1
  112. package/dist/xstate-guards.cjs.d.mts +0 -2
  113. package/dist/xstate-guards.cjs.d.ts +0 -2
  114. package/dist/xstate-guards.cjs.js +0 -14
  115. package/dist/xstate-guards.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.cjs.js +0 -14
  117. package/dist/xstate-guards.development.cjs.mjs +0 -7
  118. package/dist/xstate-guards.development.esm.js +0 -2
  119. package/dist/xstate-guards.esm.js +0 -2
  120. package/dist/xstate-guards.umd.min.js +0 -2
  121. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -1,437 +0,0 @@
1
- 'use strict';
2
-
3
- var dist_xstateGuards = require('./raise-eca73993.development.cjs.js');
4
- var assign = require('./assign-f8f66278.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
- // Actors that don't have literal string IDs — these are the only ones
157
- // that should appear in the index signature fallback, since actors with
158
- // literal IDs are already covered by ToConcreteChildren.
159
-
160
- /** @deprecated */
161
-
162
- // TODO: cover all that can be actually returned
163
-
164
- function resolveSendTo(actorScope, snapshot, args, actionParams, {
165
- to,
166
- event: eventOrExpr,
167
- id,
168
- delay
169
- }, extra) {
170
- const delaysMap = snapshot.machine.implementations.delays;
171
- if (typeof eventOrExpr === 'string') {
172
- throw new Error(
173
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
174
- `Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
175
- }
176
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
177
- let resolvedDelay;
178
- if (typeof delay === 'string') {
179
- const configDelay = delaysMap && delaysMap[delay];
180
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
181
- } else {
182
- resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
183
- }
184
- const resolvedTarget = typeof to === 'function' ? to(args, actionParams) : to;
185
- let targetActorRef;
186
- if (typeof resolvedTarget === 'string') {
187
- // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
188
- if (resolvedTarget === SpecialTargets.Parent) {
189
- targetActorRef = actorScope.self._parent;
190
- }
191
- // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
192
- else if (resolvedTarget === SpecialTargets.Internal) {
193
- targetActorRef = actorScope.self;
194
- } else if (resolvedTarget.startsWith('#_')) {
195
- // SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
196
- // #_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.
197
- targetActorRef = snapshot.children[resolvedTarget.slice(2)];
198
- } else {
199
- targetActorRef = extra.deferredActorIds?.includes(resolvedTarget) ? resolvedTarget : snapshot.children[resolvedTarget];
200
- }
201
- if (!targetActorRef) {
202
- throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${snapshot.machine.id}'.`);
203
- }
204
- } else {
205
- targetActorRef = resolvedTarget || actorScope.self;
206
- }
207
- return [snapshot, {
208
- to: targetActorRef,
209
- targetId: typeof resolvedTarget === 'string' ? resolvedTarget : undefined,
210
- event: resolvedEvent,
211
- id,
212
- delay: resolvedDelay
213
- }, undefined];
214
- }
215
- function retryResolveSendTo(_, snapshot, params) {
216
- if (typeof params.to === 'string') {
217
- params.to = snapshot.children[params.to];
218
- }
219
- }
220
- function executeSendTo(actorScope, params) {
221
- // this forms an outgoing events queue
222
- // thanks to that the recipient actors are able to read the *updated* snapshot value of the sender
223
- actorScope.defer(() => {
224
- const {
225
- to,
226
- event,
227
- delay,
228
- id
229
- } = params;
230
- if (typeof delay === 'number') {
231
- actorScope.system.scheduler.schedule(actorScope.self, to, event, delay, id);
232
- return;
233
- }
234
- actorScope.system._relay(actorScope.self,
235
- // at this point, in a deferred task, it should already be mutated by retryResolveSendTo
236
- // if it initially started as a string
237
- to, event.type === dist_xstateGuards.XSTATE_ERROR ? dist_xstateGuards.createErrorActorEvent(actorScope.self.id, event.data) : event);
238
- });
239
- }
240
- /**
241
- * Sends an event to an actor.
242
- *
243
- * @param actor The `ActorRef` to send the event to.
244
- * @param event The event to send, or an expression that evaluates to the event
245
- * to send
246
- * @param options Send action options
247
- *
248
- * - `id` - The unique send event identifier (used with `cancel()`).
249
- * - `delay` - The number of milliseconds to delay the sending of the event.
250
- */
251
- function sendTo(to, eventOrExpr, options) {
252
- if (dist_xstateGuards.executingCustomAction) {
253
- 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.');
254
- }
255
- function sendTo(_args, _params) {
256
- {
257
- throw new Error(`This isn't supposed to be called`);
258
- }
259
- }
260
- sendTo.type = 'xstate.sendTo';
261
- sendTo.to = to;
262
- sendTo.event = eventOrExpr;
263
- sendTo.id = options?.id;
264
- sendTo.delay = options?.delay;
265
- sendTo.resolve = resolveSendTo;
266
- sendTo.retryResolve = retryResolveSendTo;
267
- sendTo.execute = executeSendTo;
268
- return sendTo;
269
- }
270
-
271
- /**
272
- * Sends an event to this machine's parent.
273
- *
274
- * @param event The event to send to the parent machine.
275
- * @param options Options to pass into the send event.
276
- */
277
- function sendParent(event, options) {
278
- return sendTo(SpecialTargets.Parent, event, options);
279
- }
280
- /**
281
- * Forwards (sends) an event to the `target` actor.
282
- *
283
- * @param target The target actor to forward the event to.
284
- * @param options Options to pass into the send action creator.
285
- */
286
- function forwardTo(target, options) {
287
- if ((!target || typeof target === 'function')) {
288
- const originalTarget = target;
289
- target = (...args) => {
290
- const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
291
- if (!resolvedTarget) {
292
- throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
293
- }
294
- return resolvedTarget;
295
- };
296
- }
297
- return sendTo(target, ({
298
- event
299
- }) => event, options);
300
- }
301
-
302
- function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
303
- collect
304
- }) {
305
- const actions = [];
306
- const enqueue = function enqueue(action) {
307
- actions.push(action);
308
- };
309
- enqueue.assign = (...args) => {
310
- actions.push(assign.assign(...args));
311
- };
312
- enqueue.cancel = (...args) => {
313
- actions.push(dist_xstateGuards.cancel(...args));
314
- };
315
- enqueue.raise = (...args) => {
316
- // for some reason it fails to infer `TDelay` from `...args` here and picks its default (`never`)
317
- // then it fails to typecheck that because `...args` use `string` in place of `TDelay`
318
- actions.push(dist_xstateGuards.raise(...args));
319
- };
320
- enqueue.sendTo = (...args) => {
321
- // for some reason it fails to infer `TDelay` from `...args` here and picks its default (`never`)
322
- // then it fails to typecheck that because `...args` use `string` in place of `TDelay
323
- actions.push(sendTo(...args));
324
- };
325
- enqueue.sendParent = (...args) => {
326
- actions.push(sendParent(...args));
327
- };
328
- enqueue.spawnChild = (...args) => {
329
- actions.push(dist_xstateGuards.spawnChild(...args));
330
- };
331
- enqueue.stopChild = (...args) => {
332
- actions.push(dist_xstateGuards.stopChild(...args));
333
- };
334
- enqueue.emit = (...args) => {
335
- actions.push(emit(...args));
336
- };
337
- collect({
338
- context: args.context,
339
- event: args.event,
340
- enqueue,
341
- check: guard => dist_xstateGuards.evaluateGuard(guard, snapshot.context, args.event, snapshot),
342
- self: actorScope.self,
343
- system: actorScope.system
344
- }, actionParams);
345
- return [snapshot, undefined, actions];
346
- }
347
- /**
348
- * Creates an action object that will execute actions that are queued by the
349
- * `enqueue(action)` function.
350
- *
351
- * @example
352
- *
353
- * ```ts
354
- * import { createMachine, enqueueActions } from 'xstate';
355
- *
356
- * const machine = createMachine({
357
- * entry: enqueueActions(({ enqueue, check }) => {
358
- * enqueue.assign({ count: 0 });
359
- *
360
- * if (check('someGuard')) {
361
- * enqueue.assign({ count: 1 });
362
- * }
363
- *
364
- * enqueue('someAction');
365
- * })
366
- * });
367
- * ```
368
- */
369
- function enqueueActions(collect) {
370
- function enqueueActions(_args, _params) {
371
- {
372
- throw new Error(`This isn't supposed to be called`);
373
- }
374
- }
375
- enqueueActions.type = 'xstate.enqueueActions';
376
- enqueueActions.collect = collect;
377
- enqueueActions.resolve = resolveEnqueueActions;
378
- return enqueueActions;
379
- }
380
-
381
- function resolveLog(_, snapshot, actionArgs, actionParams, {
382
- value,
383
- label
384
- }) {
385
- return [snapshot, {
386
- value: typeof value === 'function' ? value(actionArgs, actionParams) : value,
387
- label
388
- }, undefined];
389
- }
390
- function executeLog({
391
- logger
392
- }, {
393
- value,
394
- label
395
- }) {
396
- if (label) {
397
- logger(label, value);
398
- } else {
399
- logger(value);
400
- }
401
- }
402
- /**
403
- * @param expr The expression function to evaluate which will be logged. Takes
404
- * in 2 arguments:
405
- *
406
- * - `ctx` - the current state context
407
- * - `event` - the event that caused this action to be executed.
408
- *
409
- * @param label The label to give to the logged expression.
410
- */
411
- function log(value = ({
412
- context,
413
- event
414
- }) => ({
415
- context,
416
- event
417
- }), label) {
418
- function log(_args, _params) {
419
- {
420
- throw new Error(`This isn't supposed to be called`);
421
- }
422
- }
423
- log.type = 'xstate.log';
424
- log.value = value;
425
- log.label = label;
426
- log.resolve = resolveLog;
427
- log.execute = executeLog;
428
- return log;
429
- }
430
-
431
- exports.SpecialTargets = SpecialTargets;
432
- exports.emit = emit;
433
- exports.enqueueActions = enqueueActions;
434
- exports.forwardTo = forwardTo;
435
- exports.log = log;
436
- exports.sendParent = sendParent;
437
- exports.sendTo = sendTo;