xstate 5.14.0 → 5.16.0

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 (59) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +272 -65
  8. package/actors/dist/xstate-actors.development.cjs.js +272 -65
  9. package/actors/dist/xstate-actors.development.esm.js +272 -65
  10. package/actors/dist/xstate-actors.esm.js +272 -65
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/declarations/src/State.d.ts +23 -27
  13. package/dist/declarations/src/StateMachine.d.ts +19 -31
  14. package/dist/declarations/src/StateNode.d.ts +29 -51
  15. package/dist/declarations/src/actions/assign.d.ts +29 -27
  16. package/dist/declarations/src/actions/cancel.d.ts +28 -22
  17. package/dist/declarations/src/actions/emit.d.ts +33 -33
  18. package/dist/declarations/src/actions/enqueueActions.d.ts +21 -18
  19. package/dist/declarations/src/actions/log.d.ts +5 -4
  20. package/dist/declarations/src/actions/send.d.ts +5 -3
  21. package/dist/declarations/src/actors/callback.d.ts +65 -26
  22. package/dist/declarations/src/actors/index.d.ts +4 -4
  23. package/dist/declarations/src/actors/observable.d.ts +65 -22
  24. package/dist/declarations/src/actors/promise.d.ts +86 -9
  25. package/dist/declarations/src/actors/transition.d.ts +89 -15
  26. package/dist/declarations/src/assert.d.ts +21 -20
  27. package/dist/declarations/src/createActor.d.ts +51 -42
  28. package/dist/declarations/src/createMachine.d.ts +40 -54
  29. package/dist/declarations/src/getNextSnapshot.d.ts +27 -24
  30. package/dist/declarations/src/guards.d.ts +67 -66
  31. package/dist/declarations/src/index.d.ts +1 -2
  32. package/dist/declarations/src/inspection.d.ts +1 -0
  33. package/dist/declarations/src/setup.d.ts +1 -2
  34. package/dist/declarations/src/spawn.d.ts +2 -5
  35. package/dist/declarations/src/stateUtils.d.ts +5 -10
  36. package/dist/declarations/src/toPromise.d.ts +1 -0
  37. package/dist/declarations/src/types.d.ts +164 -189
  38. package/dist/declarations/src/waitFor.d.ts +9 -9
  39. package/dist/{log-c943e6aa.development.esm.js → log-17f4495d.development.esm.js} +115 -117
  40. package/dist/{log-505687fd.development.cjs.js → log-31321d85.development.cjs.js} +115 -117
  41. package/dist/{log-b87cb6bd.esm.js → log-8320f5e6.esm.js} +115 -117
  42. package/dist/{log-7ae0ddf8.cjs.js → log-f9587b82.cjs.js} +115 -117
  43. package/dist/{raise-4e39e875.esm.js → raise-2cfe6b8f.esm.js} +164 -154
  44. package/dist/{raise-0f400094.development.esm.js → raise-7d030497.development.esm.js} +164 -154
  45. package/dist/{raise-f79d2832.cjs.js → raise-a6298350.cjs.js} +164 -154
  46. package/dist/{raise-0cd7e521.development.cjs.js → raise-bad6a97b.development.cjs.js} +164 -154
  47. package/dist/xstate.cjs.js +134 -157
  48. package/dist/xstate.development.cjs.js +134 -157
  49. package/dist/xstate.development.esm.js +136 -159
  50. package/dist/xstate.esm.js +136 -159
  51. package/dist/xstate.umd.min.js +1 -1
  52. package/dist/xstate.umd.min.js.map +1 -1
  53. package/guards/dist/xstate-guards.cjs.js +1 -1
  54. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  55. package/guards/dist/xstate-guards.development.esm.js +1 -1
  56. package/guards/dist/xstate-guards.esm.js +1 -1
  57. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  58. package/package.json +1 -1
  59. package/dist/declarations/src/typegenTypes.d.ts +0 -168
@@ -1,22 +1,22 @@
1
1
  import { AnyActorRef, SnapshotFrom } from "./types.js";
2
2
  interface WaitForOptions {
3
3
  /**
4
- * How long to wait before rejecting, if no emitted
5
- * state satisfies the predicate.
4
+ * How long to wait before rejecting, if no emitted state satisfies the
5
+ * predicate.
6
6
  *
7
7
  * @defaultValue Infinity
8
8
  */
9
9
  timeout: number;
10
10
  }
11
11
  /**
12
- * Subscribes to an actor ref and waits for its emitted value to satisfy
13
- * a predicate, and then resolves with that value.
14
- * Will throw if the desired state is not reached after an optional timeout.
15
- * (defaults to Infinity).
12
+ * Subscribes to an actor ref and waits for its emitted value to satisfy a
13
+ * predicate, and then resolves with that value. Will throw if the desired state
14
+ * is not reached after an optional timeout. (defaults to Infinity).
16
15
  *
17
16
  * @example
17
+ *
18
18
  * ```js
19
- * const state = await waitFor(someService, state => {
19
+ * const state = await waitFor(someService, (state) => {
20
20
  * return state.hasTag('loaded');
21
21
  * });
22
22
  *
@@ -26,8 +26,8 @@ interface WaitForOptions {
26
26
  * @param actorRef The actor ref to subscribe to
27
27
  * @param predicate Determines if a value matches the condition to wait for
28
28
  * @param options
29
- * @returns A promise that eventually resolves to the emitted value
30
- * that matches the condition
29
+ * @returns A promise that eventually resolves to the emitted value that matches
30
+ * the condition
31
31
  */
32
32
  export declare function waitFor<TActorRef extends AnyActorRef>(actorRef: TActorRef, predicate: (emitted: SnapshotFrom<TActorRef>) => boolean, options?: Partial<WaitForOptions>): Promise<SnapshotFrom<TActorRef>>;
33
33
  export {};
@@ -1,8 +1,5 @@
1
- import { T as ProcessingStatus, z as resolveReferencedActor, A as createActor, U as cloneMachineSnapshot, V as executingCustomAction, W as XSTATE_ERROR, Y as createErrorActorEvent, e as evaluateGuard, M as cancel, O as raise, P as spawnChild, R as stopChild } from './raise-0f400094.development.esm.js';
1
+ import { T as ProcessingStatus, z as resolveReferencedActor, A as createActor, U as cloneMachineSnapshot, V as executingCustomAction, W as XSTATE_ERROR, Y as createErrorActorEvent, e as evaluateGuard, M as cancel, O as raise, P as spawnChild, R as stopChild } from './raise-7d030497.development.esm.js';
2
2
 
3
- // it's likely-ish that `(TActor & { src: TSrc })['logic']` would be faster
4
- // but it's only possible to do it since https://github.com/microsoft/TypeScript/pull/53098 (TS 5.1)
5
- // and we strive to support TS 5.0 whenever possible
6
3
  function createSpawner(actorScope, {
7
4
  machine,
8
5
  context
@@ -91,34 +88,36 @@ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
91
88
  /**
92
89
  * Updates the current context of the machine.
93
90
  *
94
- * @param assignment An object that represents the partial context to update, or a
95
- * function that returns an object that represents the partial context to update.
96
- *
97
91
  * @example
98
- ```ts
99
- import { createMachine, assign } from 'xstate';
100
-
101
- const countMachine = createMachine({
102
- context: {
103
- count: 0,
104
- message: ''
105
- },
106
- on: {
107
- inc: {
108
- actions: assign({
109
- count: ({ context }) => context.count + 1
110
- })
111
- },
112
- updateMessage: {
113
- actions: assign(({ context, event }) => {
114
- return {
115
- message: event.message.trim()
116
- }
117
- })
118
- }
119
- }
120
- });
121
- ```
92
+ *
93
+ * ```ts
94
+ * import { createMachine, assign } from 'xstate';
95
+ *
96
+ * const countMachine = createMachine({
97
+ * context: {
98
+ * count: 0,
99
+ * message: ''
100
+ * },
101
+ * on: {
102
+ * inc: {
103
+ * actions: assign({
104
+ * count: ({ context }) => context.count + 1
105
+ * })
106
+ * },
107
+ * updateMessage: {
108
+ * actions: assign(({ context, event }) => {
109
+ * return {
110
+ * message: event.message.trim()
111
+ * };
112
+ * })
113
+ * }
114
+ * }
115
+ * });
116
+ * ```
117
+ *
118
+ * @param assignment An object that represents the partial context to update, or
119
+ * a function that returns an object that represents the partial context to
120
+ * update.
122
121
  */
123
122
  function assign(assignment) {
124
123
  if (executingCustomAction) {
@@ -152,43 +151,42 @@ function executeEmit(actorScope, {
152
151
  actorScope.defer(() => actorScope.emit(event));
153
152
  }
154
153
  /**
155
- * Emits an event to event handlers registered on the actor via `actor.on(event, handler)`.
154
+ * Emits an event to event handlers registered on the actor via `actor.on(event,
155
+ * handler)`.
156
156
  *
157
157
  * @example
158
- ```ts
159
- import { emit } from 'xstate';
160
-
161
- const machine = createMachine({
162
- // ...
163
- on: {
164
- something: {
165
- actions: emit({
166
- type: 'emitted',
167
- some: 'data'
168
- })
169
- }
170
- }
171
- // ...
172
- });
173
-
174
- const actor = createActor(machine).start();
175
-
176
- actor.on('emitted', (event) => {
177
- console.log(event);
178
- });
179
-
180
- actor.send({ type: 'something' });
181
- // logs:
182
- // {
183
- // type: 'emitted',
184
- // some: 'data'
185
- // }
186
- ```
187
- */
188
- function emit(
189
- /**
190
- * The event to emit, or an expression that returns an event to emit.
158
+ *
159
+ * ```ts
160
+ * import { emit } from 'xstate';
161
+ *
162
+ * const machine = createMachine({
163
+ * // ...
164
+ * on: {
165
+ * something: {
166
+ * actions: emit({
167
+ * type: 'emitted',
168
+ * some: 'data'
169
+ * })
170
+ * }
171
+ * }
172
+ * // ...
173
+ * });
174
+ *
175
+ * const actor = createActor(machine).start();
176
+ *
177
+ * actor.on('emitted', (event) => {
178
+ * console.log(event);
179
+ * });
180
+ *
181
+ * actor.send({ type: 'something' });
182
+ * // logs:
183
+ * // {
184
+ * // type: 'emitted',
185
+ * // some: 'data'
186
+ * // }
187
+ * ```
191
188
  */
189
+ function emit( /** The event to emit, or an expression that returns an event to emit. */
192
190
  eventOrExpr) {
193
191
  if (executingCustomAction) {
194
192
  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.');
@@ -206,62 +204,52 @@ eventOrExpr) {
206
204
  }
207
205
 
208
206
  /**
209
- *
210
207
  * @remarks
211
- *
212
- * `T | unknown` reduces to `unknown` and that can be problematic when it comes to contextual typing.
213
- * It especially is a problem when the union has a function member, like here:
208
+ * `T | unknown` reduces to `unknown` and that can be problematic when it comes
209
+ * to contextual typing. It especially is a problem when the union has a
210
+ * function member, like here:
214
211
  *
215
212
  * ```ts
216
- * declare function test(cbOrVal: ((arg: number) => unknown) | unknown): void;
217
- * test((arg) => {}) // oops, implicit any
213
+ * declare function test(
214
+ * cbOrVal: ((arg: number) => unknown) | unknown
215
+ * ): void;
216
+ * test((arg) => {}); // oops, implicit any
218
217
  * ```
219
218
  *
220
- * This type can be used to avoid this problem. This union represents the same value space as `unknown`.
219
+ * This type can be used to avoid this problem. This union represents the same
220
+ * value space as `unknown`.
221
221
  */
222
222
 
223
223
  // https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
224
224
 
225
225
  // @TODO: Replace with native `NoInfer` when TS issue gets fixed:
226
226
  // https://github.com/microsoft/TypeScript/pull/57673
227
+ /** @deprecated Use the built-in `NoInfer` type instead */
228
+ /** The full definition of an event, with a string `type`. */
227
229
  /**
228
- * @deprecated Use the built-in `NoInfer` type instead
229
- */
230
- /**
231
- * The full definition of an event, with a string `type`.
232
- */
233
- /**
234
- * The string or object representing the state value relative to the parent state node.
230
+ * The string or object representing the state value relative to the parent
231
+ * state node.
235
232
  *
236
233
  * @remarks
237
- *
238
234
  * - For a child atomic state node, this is a string, e.g., `"pending"`.
239
- *
240
- * - For complex state nodes, this is an object, e.g., `{ success: "someChildState" }`.
235
+ * - For complex state nodes, this is an object, e.g., `{ success:
236
+ * "someChildState" }`.
241
237
  */
242
238
  // TODO: remove once TS fixes this type-widening issue
243
- /** @deprecated use `AnyMachineSnapshot` instead */
239
+ /** @deprecated Use `AnyMachineSnapshot` instead */
244
240
  // TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
245
- /**
246
- * @hidden
247
- */
241
+ /** @ignore */
248
242
  let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
249
243
  SpecialTargets["Parent"] = "#_parent";
250
244
  SpecialTargets["Internal"] = "#_internal";
251
245
  return SpecialTargets;
252
246
  }({});
253
247
 
254
- /**
255
- * @deprecated Use `AnyActor` instead.
256
- */
248
+ /** @deprecated Use `AnyActor` instead. */
257
249
 
258
250
  // Based on RxJS types
259
251
 
260
- /**
261
- * @deprecated Use `Actor<T>` instead.
262
- */
263
-
264
- // only meant to be used internally for debugging purposes
252
+ /** @deprecated Use `Actor<T>` instead. */
265
253
 
266
254
  /**
267
255
  * Represents logic which can be used by an actor.
@@ -272,6 +260,8 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
272
260
  * @template TSystem - The type of the actor system.
273
261
  */
274
262
 
263
+ /** @deprecated */
264
+
275
265
  function resolveSendTo(actorScope, snapshot, args, actionParams, {
276
266
  to,
277
267
  event: eventOrExpr,
@@ -346,10 +336,12 @@ function executeSendTo(actorScope, params) {
346
336
  * Sends an event to an actor.
347
337
  *
348
338
  * @param actor The `ActorRef` to send the event to.
349
- * @param event The event to send, or an expression that evaluates to the event to send
339
+ * @param event The event to send, or an expression that evaluates to the event
340
+ * to send
350
341
  * @param options Send action options
351
- * - `id` - The unique send event identifier (used with `cancel()`).
352
- * - `delay` - The number of milliseconds to delay the sending of the event.
342
+ *
343
+ * - `id` - The unique send event identifier (used with `cancel()`).
344
+ * - `delay` - The number of milliseconds to delay the sending of the event.
353
345
  */
354
346
  function sendTo(to, eventOrExpr, options) {
355
347
  if (executingCustomAction) {
@@ -425,6 +417,9 @@ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
425
417
  // then it fails to typecheck that because `...args` use `string` in place of `TDelay
426
418
  actions.push(sendTo(...args));
427
419
  };
420
+ enqueue.sendParent = (...args) => {
421
+ actions.push(sendParent(...args));
422
+ };
428
423
  enqueue.spawnChild = (...args) => {
429
424
  actions.push(spawnChild(...args));
430
425
  };
@@ -445,24 +440,26 @@ function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
445
440
  return [snapshot, undefined, actions];
446
441
  }
447
442
  /**
448
- * Creates an action object that will execute actions that are queued by the `enqueue(action)` function.
443
+ * Creates an action object that will execute actions that are queued by the
444
+ * `enqueue(action)` function.
449
445
  *
450
446
  * @example
451
- ```ts
452
- import { createMachine, enqueueActions } from 'xstate';
453
-
454
- const machine = createMachine({
455
- entry: enqueueActions(({ enqueue, check }) => {
456
- enqueue.assign({ count: 0 });
457
-
458
- if (check('someGuard')) {
459
- enqueue.assign({ count: 1 });
460
- }
461
-
462
- enqueue('someAction');
463
- })
464
- })
465
- ```
447
+ *
448
+ * ```ts
449
+ * import { createMachine, enqueueActions } from 'xstate';
450
+ *
451
+ * const machine = createMachine({
452
+ * entry: enqueueActions(({ enqueue, check }) => {
453
+ * enqueue.assign({ count: 0 });
454
+ *
455
+ * if (check('someGuard')) {
456
+ * enqueue.assign({ count: 1 });
457
+ * }
458
+ *
459
+ * enqueue('someAction');
460
+ * })
461
+ * });
462
+ * ```
466
463
  */
467
464
  function enqueueActions(collect) {
468
465
  function enqueueActions(args, params) {
@@ -498,11 +495,12 @@ function executeLog({
498
495
  }
499
496
  }
500
497
  /**
498
+ * @param expr The expression function to evaluate which will be logged. Takes
499
+ * in 2 arguments:
500
+ *
501
+ * - `ctx` - the current state context
502
+ * - `event` - the event that caused this action to be executed.
501
503
  *
502
- * @param expr The expression function to evaluate which will be logged.
503
- * Takes in 2 arguments:
504
- * - `ctx` - the current state context
505
- * - `event` - the event that caused this action to be executed.
506
504
  * @param label The label to give to the logged expression.
507
505
  */
508
506
  function log(value = ({