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