xstate 5.14.0 → 5.15.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 (58) 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.map +1 -1
  6. package/actors/dist/xstate-actors.cjs.js +272 -65
  7. package/actors/dist/xstate-actors.development.cjs.js +272 -65
  8. package/actors/dist/xstate-actors.development.esm.js +272 -65
  9. package/actors/dist/xstate-actors.esm.js +272 -65
  10. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  11. package/dist/declarations/src/State.d.ts +21 -25
  12. package/dist/declarations/src/StateMachine.d.ts +18 -30
  13. package/dist/declarations/src/StateNode.d.ts +29 -51
  14. package/dist/declarations/src/actions/assign.d.ts +29 -27
  15. package/dist/declarations/src/actions/cancel.d.ts +28 -22
  16. package/dist/declarations/src/actions/emit.d.ts +33 -33
  17. package/dist/declarations/src/actions/enqueueActions.d.ts +18 -16
  18. package/dist/declarations/src/actions/log.d.ts +5 -4
  19. package/dist/declarations/src/actions/send.d.ts +5 -3
  20. package/dist/declarations/src/actors/callback.d.ts +65 -26
  21. package/dist/declarations/src/actors/index.d.ts +4 -4
  22. package/dist/declarations/src/actors/observable.d.ts +65 -22
  23. package/dist/declarations/src/actors/promise.d.ts +86 -9
  24. package/dist/declarations/src/actors/transition.d.ts +89 -15
  25. package/dist/declarations/src/assert.d.ts +21 -20
  26. package/dist/declarations/src/createActor.d.ts +51 -42
  27. package/dist/declarations/src/createMachine.d.ts +40 -54
  28. package/dist/declarations/src/getNextSnapshot.d.ts +27 -24
  29. package/dist/declarations/src/guards.d.ts +67 -66
  30. package/dist/declarations/src/index.d.ts +0 -1
  31. package/dist/declarations/src/inspection.d.ts +1 -0
  32. package/dist/declarations/src/setup.d.ts +1 -2
  33. package/dist/declarations/src/spawn.d.ts +2 -5
  34. package/dist/declarations/src/stateUtils.d.ts +5 -10
  35. package/dist/declarations/src/toPromise.d.ts +1 -0
  36. package/dist/declarations/src/types.d.ts +162 -188
  37. package/dist/declarations/src/waitFor.d.ts +9 -9
  38. package/dist/{log-b87cb6bd.esm.js → log-63de2429.esm.js} +112 -117
  39. package/dist/{log-7ae0ddf8.cjs.js → log-b8c93ee3.cjs.js} +112 -117
  40. package/dist/{log-505687fd.development.cjs.js → log-d2c282d6.development.cjs.js} +112 -117
  41. package/dist/{log-c943e6aa.development.esm.js → log-e9953143.development.esm.js} +112 -117
  42. package/dist/{raise-4e39e875.esm.js → raise-2cfe6b8f.esm.js} +164 -154
  43. package/dist/{raise-0f400094.development.esm.js → raise-7d030497.development.esm.js} +164 -154
  44. package/dist/{raise-f79d2832.cjs.js → raise-a6298350.cjs.js} +164 -154
  45. package/dist/{raise-0cd7e521.development.cjs.js → raise-bad6a97b.development.cjs.js} +164 -154
  46. package/dist/xstate.cjs.js +134 -157
  47. package/dist/xstate.development.cjs.js +134 -157
  48. package/dist/xstate.development.esm.js +136 -159
  49. package/dist/xstate.esm.js +136 -159
  50. package/dist/xstate.umd.min.js +1 -1
  51. package/dist/xstate.umd.min.js.map +1 -1
  52. package/guards/dist/xstate-guards.cjs.js +1 -1
  53. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  54. package/guards/dist/xstate-guards.development.esm.js +1 -1
  55. package/guards/dist/xstate-guards.esm.js +1 -1
  56. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  57. package/package.json +1 -1
  58. package/dist/declarations/src/typegenTypes.d.ts +0 -168
@@ -1,43 +1,98 @@
1
- import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-4e39e875.esm.js';
1
+ import { X as XSTATE_STOP, A as createActor } from '../../dist/raise-2cfe6b8f.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
+ /**
5
+ * Represents an actor created by `fromTransition`.
6
+ *
7
+ * The type of `self` within the actor's logic.
8
+ *
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import {
13
+ * fromTransition,
14
+ * createActor,
15
+ * type AnyActorSystem
16
+ * } from 'xstate';
17
+ *
18
+ * //* The actor's stored context.
19
+ * type Context = {
20
+ * // The current count.
21
+ * count: number;
22
+ * // The amount to increase `count` by.
23
+ * step: number;
24
+ * };
25
+ * // The events the actor receives.
26
+ * type Event = { type: 'increment' };
27
+ * // The actor's input.
28
+ * type Input = { step?: number };
29
+ *
30
+ * // Actor logic that increments `count` by `step` when it receives an event of
31
+ * // type `increment`.
32
+ * const logic = fromTransition<Context, Event, AnyActorSystem, Input>(
33
+ * (state, event, actorScope) => {
34
+ * actorScope.self;
35
+ * // ^? TransitionActorRef<Context, Event>
36
+ *
37
+ * if (event.type === 'increment') {
38
+ * return {
39
+ * ...state,
40
+ * count: state.count + state.step
41
+ * };
42
+ * }
43
+ * return state;
44
+ * },
45
+ * ({ input, self }) => {
46
+ * self;
47
+ * // ^? TransitionActorRef<Context, Event>
48
+ *
49
+ * return {
50
+ * count: 0,
51
+ * step: input.step ?? 1
52
+ * };
53
+ * }
54
+ * );
55
+ *
56
+ * const actor = createActor(logic, { input: { step: 10 } });
57
+ * // ^? TransitionActorRef<Context, Event>
58
+ * ```
59
+ *
60
+ * @see {@link fromTransition}
61
+ */
62
+
4
63
  /**
5
64
  * Returns actor logic given a transition function and its initial state.
6
65
  *
7
- * A “transition function” is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
66
+ * A “transition function” is a function that takes the current `state` and
67
+ * received `event` object as arguments, and returns the next state, similar to
68
+ * a reducer.
8
69
  *
9
70
  * Actors created from transition logic (“transition actors”) can:
10
71
  *
11
72
  * - Receive events
12
73
  * - Emit snapshots of its state
13
74
  *
14
- * The transition function’s `state` is used as its transition actor’s `context`.
75
+ * The transition function’s `state` is used as its transition actor’s
76
+ * `context`.
15
77
  *
16
- * Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
17
- *
18
- * @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
19
- * - `state` - the current state.
20
- * - `event` - the received event.
21
- * - `actorScope` - the actor scope object, with properties like `self` and `system`.
22
- * @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
23
- * - `input` - the `input` provided to its parent transition actor.
24
- * - `self` - a reference to its parent transition actor.
25
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
26
- * @returns Actor logic
78
+ * Note that the "state" for a transition function is provided by the initial
79
+ * state argument, and is not the same as the State object of an actor or a
80
+ * state within a machine configuration.
27
81
  *
28
82
  * @example
83
+ *
29
84
  * ```ts
30
85
  * const transitionLogic = fromTransition(
31
86
  * (state, event) => {
32
87
  * if (event.type === 'increment') {
33
88
  * return {
34
89
  * ...state,
35
- * count: state.count + 1,
90
+ * count: state.count + 1
36
91
  * };
37
92
  * }
38
93
  * return state;
39
94
  * },
40
- * { count: 0 },
95
+ * { count: 0 }
41
96
  * );
42
97
  *
43
98
  * const transitionActor = createActor(transitionLogic);
@@ -58,6 +113,26 @@ import '../../dev/dist/xstate-dev.esm.js';
58
113
  * // ...
59
114
  * // }
60
115
  * ```
116
+ *
117
+ * @param transition The transition function used to describe the transition
118
+ * logic. It should return the next state given the current state and event.
119
+ * It receives the following arguments:
120
+ *
121
+ * - `state` - the current state.
122
+ * - `event` - the received event.
123
+ * - `actorScope` - the actor scope object, with properties like `self` and
124
+ * `system`.
125
+ *
126
+ * @param initialContext The initial state of the transition function, either an
127
+ * object representing the state, or a function which returns a state object.
128
+ * If a function, it will receive as its only argument an object with the
129
+ * following properties:
130
+ *
131
+ * - `input` - the `input` provided to its parent transition actor.
132
+ * - `self` - a reference to its parent transition actor.
133
+ *
134
+ * @returns Actor logic
135
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
61
136
  */
62
137
  function fromTransition(transition, initialContext) {
63
138
  return {
@@ -84,36 +159,64 @@ function fromTransition(transition, initialContext) {
84
159
  }
85
160
 
86
161
  const instanceStates = /* #__PURE__ */new WeakMap();
162
+
163
+ /**
164
+ * Represents an actor created by `fromCallback`.
165
+ *
166
+ * The type of `self` within the actor's logic.
167
+ *
168
+ * @example
169
+ *
170
+ * ```ts
171
+ * import { fromCallback, createActor } from 'xstate';
172
+ *
173
+ * // The events the actor receives.
174
+ * type Event = { type: 'someEvent' };
175
+ * // The actor's input.
176
+ * type Input = { name: string };
177
+ *
178
+ * // Actor logic that logs whenever it receives an event of type `someEvent`.
179
+ * const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
180
+ * self;
181
+ * // ^? CallbackActorRef<Event, Input>
182
+ *
183
+ * receive((event) => {
184
+ * if (event.type === 'someEvent') {
185
+ * console.log(`${input.name}: received "someEvent" event`);
186
+ * // logs 'myActor: received "someEvent" event'
187
+ * }
188
+ * });
189
+ * });
190
+ *
191
+ * const actor = createActor(logic, { input: { name: 'myActor' } });
192
+ * // ^? CallbackActorRef<Event, Input>
193
+ * ```
194
+ *
195
+ * @see {@link fromCallback}
196
+ */
197
+
87
198
  /**
88
- * An actor logic creator which returns callback logic as defined by a callback function.
199
+ * An actor logic creator which returns callback logic as defined by a callback
200
+ * function.
89
201
  *
90
202
  * @remarks
91
- * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
203
+ * Useful for subscription-based or other free-form logic that can send events
204
+ * back to the parent actor.
92
205
  *
93
206
  * Actors created from callback logic (“callback actors”) can:
207
+ *
94
208
  * - Receive events via the `receive` function
95
209
  * - Send events to the parent actor via the `sendBack` function
96
210
  *
97
211
  * Callback actors are a bit different from other actors in that they:
212
+ *
98
213
  * - Do not work with `onDone`
99
214
  * - Do not produce a snapshot using `.getSnapshot()`
100
215
  * - Do not emit values when used with `.subscribe()`
101
216
  * - Can not be stopped with `.stop()`
102
217
  *
103
- * @param invokeCallback - The callback function used to describe the callback logic
104
- * The callback function is passed an object with the following properties:
105
- * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
106
- * - `sendBack` - A function that can send events back to the parent actor
107
- * - `input` - Data that was provided to the callback actor
108
- * - `self` - The parent actor of the callback actor
109
- * - `system` - The actor system to which the callback actor belongs
110
- * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
111
- * @see {@link InvokeCallback} for more information about the callback function and its object argument
112
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
113
-
114
- * @returns Callback logic
115
- *
116
218
  * @example
219
+ *
117
220
  * ```typescript
118
221
  * const callbackLogic = fromCallback(({ sendBack, receive }) => {
119
222
  * let lockStatus = 'unlocked';
@@ -140,6 +243,24 @@ const instanceStates = /* #__PURE__ */new WeakMap();
140
243
  * };
141
244
  * });
142
245
  * ```
246
+ *
247
+ * @param invokeCallback - The callback function used to describe the callback
248
+ * logic The callback function is passed an object with the following
249
+ * properties:
250
+ *
251
+ * - `receive` - A function that can send events back to the parent actor; the
252
+ * listener is then called whenever events are received by the callback
253
+ * actor
254
+ * - `sendBack` - A function that can send events back to the parent actor
255
+ * - `input` - Data that was provided to the callback actor
256
+ * - `self` - The parent actor of the callback actor
257
+ * - `system` - The actor system to which the callback actor belongs The callback
258
+ * function can (optionally) return a cleanup function, which is called
259
+ * when the actor is stopped.
260
+ *
261
+ * @returns Callback logic
262
+ * @see {@link InvokeCallback} for more information about the callback function and its object argument
263
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
143
264
  */
144
265
  function fromCallback(invokeCallback) {
145
266
  const logic = {
@@ -205,8 +326,43 @@ function fromCallback(invokeCallback) {
205
326
  const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
206
327
  const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
207
328
  const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
329
+
330
+ /**
331
+ * Represents an actor created by `fromObservable` or `fromEventObservable`.
332
+ *
333
+ * The type of `self` within the actor's logic.
334
+ *
335
+ * @example
336
+ *
337
+ * ```ts
338
+ * import { fromObservable, createActor } from 'xstate';
339
+ * import { interval } from 'rxjs';
340
+ *
341
+ * // The type of the value observed by the actor's logic.
342
+ * type Context = number;
343
+ * // The actor's input.
344
+ * type Input = { period?: number };
345
+ *
346
+ * // Actor logic that observes a number incremented every `input.period`
347
+ * // milliseconds (default: 1_000).
348
+ * const logic = fromObservable<Context, Input>(({ input, self }) => {
349
+ * self;
350
+ * // ^? ObservableActorRef<Event, Input>
351
+ *
352
+ * return interval(input.period ?? 1_000);
353
+ * });
354
+ *
355
+ * const actor = createActor(logic, { input: { period: 2_000 } });
356
+ * // ^? ObservableActorRef<Event, Input>
357
+ * ```
358
+ *
359
+ * @see {@link fromObservable}
360
+ * @see {@link fromEventObservable}
361
+ */
362
+
208
363
  /**
209
- * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
364
+ * Observable actor logic is described by an observable stream of values. Actors
365
+ * created from observable logic (“observable actors”) can:
210
366
  *
211
367
  * - Emit snapshots of the observable’s emitted value
212
368
  *
@@ -214,16 +370,10 @@ const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
214
370
  *
215
371
  * Sending events to observable actors will have no effect.
216
372
  *
217
- * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
218
- * - `input` - Data that was provided to the observable actor
219
- * - `self` - The parent actor
220
- * - `system` - The actor system to which the observable actor belongs
221
- *
222
- * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
223
- *
224
373
  * @example
374
+ *
225
375
  * ```ts
226
- * import { fromObservable, createActor } from 'xstate'
376
+ * import { fromObservable, createActor } from 'xstate';
227
377
  * import { interval } from 'rxjs';
228
378
  *
229
379
  * const logic = fromObservable((obj) => interval(1000));
@@ -242,6 +392,15 @@ const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
242
392
  * // ...
243
393
  * ```
244
394
  *
395
+ * @param observableCreator A function that creates an observable. It receives
396
+ * one argument, an object with the following properties:
397
+ *
398
+ * - `input` - Data that was provided to the observable actor
399
+ * - `self` - The parent actor
400
+ * - `system` - The actor system to which the observable actor belongs
401
+ *
402
+ * It should return a {@link Subscribable}, which is compatible with an RxJS
403
+ * Observable, although RxJS is not required to create them.
245
404
  * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
246
405
  * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
247
406
  */
@@ -346,24 +505,20 @@ function fromObservable(observableCreator) {
346
505
  }
347
506
 
348
507
  /**
349
- * Creates event observable logic that listens to an observable that delivers event objects.
508
+ * Creates event observable logic that listens to an observable that delivers
509
+ * event objects.
350
510
  *
351
- * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
511
+ * Event observable actor logic is described by an observable stream of
512
+ * {@link https://stately.ai/docs/transitions#event-objects | event objects}.
513
+ * Actors created from event observable logic (“event observable actors”) can:
352
514
  *
353
515
  * - Implicitly send events to its parent actor
354
516
  * - Emit snapshots of its emitted event objects
355
517
  *
356
518
  * Sending events to event observable actors will have no effect.
357
519
  *
358
- * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
359
- *
360
- * - `input` - Data that was provided to the event observable actor
361
- * - `self` - The parent actor
362
- * - `system` - The actor system to which the event observable actor belongs.
363
- *
364
- * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
365
- *
366
520
  * @example
521
+ *
367
522
  * ```ts
368
523
  * import {
369
524
  * fromEventObservable,
@@ -374,20 +529,31 @@ function fromObservable(observableCreator) {
374
529
  * } from 'xstate';
375
530
  * import { fromEvent } from 'rxjs';
376
531
  *
377
- * const mouseClickLogic = fromEventObservable(() =>
378
- * fromEvent(document.body, 'click') as Subscribable<EventObject>
532
+ * const mouseClickLogic = fromEventObservable(
533
+ * () => fromEvent(document.body, 'click') as Subscribable<EventObject>
379
534
  * );
380
535
  *
381
536
  * const canvasMachine = createMachine({
382
537
  * invoke: {
383
538
  * // Will send mouse `click` events to the canvas actor
384
- * src: mouseClickLogic,
539
+ * src: mouseClickLogic
385
540
  * }
386
541
  * });
387
542
  *
388
543
  * const canvasActor = createActor(canvasMachine);
389
544
  * canvasActor.start();
390
545
  * ```
546
+ *
547
+ * @param lazyObservable A function that creates an observable that delivers
548
+ * event objects. It receives one argument, an object with the following
549
+ * properties:
550
+ *
551
+ * - `input` - Data that was provided to the event observable actor
552
+ * - `self` - The parent actor
553
+ * - `system` - The actor system to which the event observable actor belongs.
554
+ *
555
+ * It should return a {@link Subscribable}, which is compatible with an RxJS
556
+ * Observable, although RxJS is not required to create them.
391
557
  */
392
558
  function fromEventObservable(lazyObservable) {
393
559
  // TODO: event types
@@ -482,27 +648,61 @@ function fromEventObservable(lazyObservable) {
482
648
 
483
649
  const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
484
650
  const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
651
+
652
+ /**
653
+ * Represents an actor created by `fromPromise`.
654
+ *
655
+ * The type of `self` within the actor's logic.
656
+ *
657
+ * @example
658
+ *
659
+ * ```ts
660
+ * import { fromPromise, createActor } from 'xstate';
661
+ *
662
+ * // The actor's resolved output
663
+ * type Output = string;
664
+ * // The actor's input.
665
+ * type Input = { message: string };
666
+ *
667
+ * // Actor logic that fetches the url of an image of a cat saying `input.message`.
668
+ * const logic = fromPromise<Output, Input>(async ({ input, self }) => {
669
+ * self;
670
+ * // ^? PromiseActorRef<Output, Input>
671
+ *
672
+ * const data = await fetch(
673
+ * `https://cataas.com/cat/says/${input.message}`
674
+ * );
675
+ * const url = await data.json();
676
+ * return url;
677
+ * });
678
+ *
679
+ * const actor = createActor(logic, { input: { message: 'hello world' } });
680
+ * // ^? PromiseActorRef<Output, Input>
681
+ * ```
682
+ *
683
+ * @see {@link fromPromise}
684
+ */
685
+
686
+ const controllerMap = new WeakMap();
687
+
485
688
  /**
486
- * An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
689
+ * An actor logic creator which returns promise logic as defined by an async
690
+ * process that resolves or rejects after some time.
487
691
  *
488
692
  * Actors created from promise actor logic (“promise actors”) can:
693
+ *
489
694
  * - Emit the resolved value of the promise
490
695
  * - Output the resolved value of the promise
491
696
  *
492
697
  * Sending events to promise actors will have no effect.
493
698
  *
494
- * @param promiseCreator
495
- * A function which returns a Promise, and accepts an object with the following properties:
496
- * - `input` - Data that was provided to the promise actor
497
- * - `self` - The parent actor of the promise actor
498
- * - `system` - The actor system to which the promise actor belongs
499
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
500
- *
501
699
  * @example
700
+ *
502
701
  * ```ts
503
702
  * const promiseLogic = fromPromise(async () => {
504
- * const result = await fetch('https://example.com/...')
505
- * .then((data) => data.json());
703
+ * const result = await fetch('https://example.com/...').then((data) =>
704
+ * data.json()
705
+ * );
506
706
  *
507
707
  * return result;
508
708
  * });
@@ -525,9 +725,16 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
525
725
  * // ...
526
726
  * // }
527
727
  * ```
728
+ *
729
+ * @param promiseCreator A function which returns a Promise, and accepts an
730
+ * object with the following properties:
731
+ *
732
+ * - `input` - Data that was provided to the promise actor
733
+ * - `self` - The parent actor of the promise actor
734
+ * - `system` - The actor system to which the promise actor belongs
735
+ *
736
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
528
737
  */
529
-
530
- const controllerMap = new WeakMap();
531
738
  function fromPromise(promiseCreator) {
532
739
  const logic = {
533
740
  config: promiseCreator,