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
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.development.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-0cd7e521.development.cjs.js');
7
- var log = require('./log-505687fd.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-bad6a97b.development.cjs.js');
7
+ var log = require('./log-31321d85.development.cjs.js');
8
8
  require('../dev/dist/xstate-dev.development.cjs.js');
9
9
 
10
10
  class SimulatedClock {
@@ -107,71 +107,58 @@ const toSerializableAction = action => {
107
107
  return action;
108
108
  };
109
109
  class StateNode {
110
- constructor(
111
- /**
112
- * The raw config used to create the machine.
113
- */
110
+ constructor( /** The raw config used to create the machine. */
114
111
  config, options) {
115
112
  this.config = config;
116
113
  /**
117
- * The relative key of the state node, which represents its location in the overall state value.
114
+ * The relative key of the state node, which represents its location in the
115
+ * overall state value.
118
116
  */
119
117
  this.key = void 0;
120
- /**
121
- * The unique ID of the state node.
122
- */
118
+ /** The unique ID of the state node. */
123
119
  this.id = void 0;
124
120
  /**
125
121
  * The type of this state node:
126
122
  *
127
- * - `'atomic'` - no child state nodes
128
- * - `'compound'` - nested child state nodes (XOR)
129
- * - `'parallel'` - orthogonal nested child state nodes (AND)
130
- * - `'history'` - history state node
131
- * - `'final'` - final state node
123
+ * - `'atomic'` - no child state nodes
124
+ * - `'compound'` - nested child state nodes (XOR)
125
+ * - `'parallel'` - orthogonal nested child state nodes (AND)
126
+ * - `'history'` - history state node
127
+ * - `'final'` - final state node
132
128
  */
133
129
  this.type = void 0;
134
- /**
135
- * The string path from the root machine node to this node.
136
- */
130
+ /** The string path from the root machine node to this node. */
137
131
  this.path = void 0;
138
- /**
139
- * The child state nodes.
140
- */
132
+ /** The child state nodes. */
141
133
  this.states = void 0;
142
134
  /**
143
135
  * The type of history on this state node. Can be:
144
136
  *
145
- * - `'shallow'` - recalls only top-level historical state value
146
- * - `'deep'` - recalls historical state value at all levels
137
+ * - `'shallow'` - recalls only top-level historical state value
138
+ * - `'deep'` - recalls historical state value at all levels
147
139
  */
148
140
  this.history = void 0;
149
- /**
150
- * The action(s) to be executed upon entering the state node.
151
- */
141
+ /** The action(s) to be executed upon entering the state node. */
152
142
  this.entry = void 0;
153
- /**
154
- * The action(s) to be executed upon exiting the state node.
155
- */
143
+ /** The action(s) to be executed upon exiting the state node. */
156
144
  this.exit = void 0;
157
- /**
158
- * The parent state node.
159
- */
145
+ /** The parent state node. */
160
146
  this.parent = void 0;
161
- /**
162
- * The root machine node.
163
- */
147
+ /** The root machine node. */
164
148
  this.machine = void 0;
165
149
  /**
166
- * The meta data associated with this state node, which will be returned in State instances.
150
+ * The meta data associated with this state node, which will be returned in
151
+ * State instances.
167
152
  */
168
153
  this.meta = void 0;
169
154
  /**
170
- * The output data sent with the "xstate.done.state._id_" event if this is a final state node.
155
+ * The output data sent with the "xstate.done.state._id_" event if this is a
156
+ * final state node.
171
157
  */
172
158
  this.output = void 0;
173
159
  /**
174
- * The order this state node appears. Corresponds to the implicit document order.
160
+ * The order this state node appears. Corresponds to the implicit document
161
+ * order.
175
162
  */
176
163
  this.order = -1;
177
164
  this.description = void 0;
@@ -219,9 +206,7 @@ class StateNode {
219
206
  });
220
207
  }
221
208
 
222
- /**
223
- * The well-structured state node definition.
224
- */
209
+ /** The well-structured state node definition. */
225
210
  get definition() {
226
211
  return {
227
212
  id: this.id,
@@ -266,9 +251,7 @@ class StateNode {
266
251
  return this.definition;
267
252
  }
268
253
 
269
- /**
270
- * The logic invoked as actors by this state node.
271
- */
254
+ /** The logic invoked as actors by this state node. */
272
255
  get invoke() {
273
256
  return memo(this, 'invoke', () => guards_dist_xstateGuards.toArray(this.config.invoke).map((invokeConfig, i) => {
274
257
  const {
@@ -299,9 +282,7 @@ class StateNode {
299
282
  }));
300
283
  }
301
284
 
302
- /**
303
- * The mapping of events to transitions.
304
- */
285
+ /** The mapping of events to transitions. */
305
286
  get on() {
306
287
  return memo(this, 'on', () => {
307
288
  const transitions = this.transitions;
@@ -346,9 +327,7 @@ class StateNode {
346
327
  return selectedTransition ? [selectedTransition] : undefined;
347
328
  }
348
329
 
349
- /**
350
- * All the event types accepted by this state node and its descendants.
351
- */
330
+ /** All the event types accepted by this state node and its descendants. */
352
331
  get events() {
353
332
  return memo(this, 'events', () => {
354
333
  const {
@@ -384,15 +363,10 @@ class StateNode {
384
363
 
385
364
  const STATE_IDENTIFIER = '#';
386
365
  class StateMachine {
387
- constructor(
388
- /**
389
- * The raw config used to create the machine.
390
- */
366
+ constructor( /** The raw config used to create the machine. */
391
367
  config, implementations) {
392
368
  this.config = config;
393
- /**
394
- * The machine's own version.
395
- */
369
+ /** The machine's own version. */
396
370
  this.version = void 0;
397
371
  this.schemas = void 0;
398
372
  this.implementations = void 0;
@@ -404,10 +378,6 @@ class StateMachine {
404
378
  this.id = void 0;
405
379
  this.states = void 0;
406
380
  this.events = void 0;
407
- /**
408
- * @deprecated an internal property that was acting as a "phantom" type, it's not used by anything right now but it's kept around for compatibility reasons
409
- **/
410
- this.__TResolvedTypesMeta = void 0;
411
381
  this.id = config.id || '(machine)';
412
382
  this.implementations = {
413
383
  actors: implementations?.actors ?? {},
@@ -435,12 +405,11 @@ class StateMachine {
435
405
  }
436
406
 
437
407
  /**
438
- * Clones this state machine with the provided implementations
439
- * and merges the `context` (if provided).
440
- *
441
- * @param implementations Options (`actions`, `guards`, `actors`, `delays`, `context`)
442
- * to recursively merge with the existing options.
408
+ * Clones this state machine with the provided implementations and merges the
409
+ * `context` (if provided).
443
410
  *
411
+ * @param implementations Options (`actions`, `guards`, `actors`, `delays`,
412
+ * `context`) to recursively merge with the existing options.
444
413
  * @returns A new `StateMachine` instance with the provided implementations.
445
414
  */
446
415
  provide(implementations) {
@@ -484,8 +453,8 @@ class StateMachine {
484
453
  }
485
454
 
486
455
  /**
487
- * Determines the next snapshot given the current `snapshot` and received `event`.
488
- * Calculates a full macrostep from all microsteps.
456
+ * Determines the next snapshot given the current `snapshot` and received
457
+ * `event`. Calculates a full macrostep from all microsteps.
489
458
  *
490
459
  * @param snapshot The current snapshot
491
460
  * @param event The received event
@@ -495,8 +464,8 @@ class StateMachine {
495
464
  }
496
465
 
497
466
  /**
498
- * Determines the next state given the current `state` and `event`.
499
- * Calculates a microstep.
467
+ * Determines the next state given the current `state` and `event`. Calculates
468
+ * a microstep.
500
469
  *
501
470
  * @param state The current state
502
471
  * @param event The received event
@@ -509,8 +478,8 @@ class StateMachine {
509
478
  }
510
479
 
511
480
  /**
512
- * The initial state _before_ evaluating any microsteps.
513
- * This "pre-initial" state is provided to initial actions executed in the initial state.
481
+ * The initial state _before_ evaluating any microsteps. This "pre-initial"
482
+ * state is provided to initial actions executed in the initial state.
514
483
  */
515
484
  getPreInitialState(actorScope, initEvent, internalQueue) {
516
485
  const {
@@ -538,7 +507,8 @@ class StateMachine {
538
507
  }
539
508
 
540
509
  /**
541
- * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
510
+ * Returns the initial `State` instance, with reference to `self` as an
511
+ * `ActorRef`.
542
512
  */
543
513
  getInitialSnapshot(actorScope, input) {
544
514
  const initEvent = guards_dist_xstateGuards.createInitEvent(input); // TODO: fix;
@@ -636,14 +606,14 @@ const defaultWaitForOptions = {
636
606
  };
637
607
 
638
608
  /**
639
- * Subscribes to an actor ref and waits for its emitted value to satisfy
640
- * a predicate, and then resolves with that value.
641
- * Will throw if the desired state is not reached after an optional timeout.
642
- * (defaults to Infinity).
609
+ * Subscribes to an actor ref and waits for its emitted value to satisfy a
610
+ * predicate, and then resolves with that value. Will throw if the desired state
611
+ * is not reached after an optional timeout. (defaults to Infinity).
643
612
  *
644
613
  * @example
614
+ *
645
615
  * ```js
646
- * const state = await waitFor(someService, state => {
616
+ * const state = await waitFor(someService, (state) => {
647
617
  * return state.hasTag('loaded');
648
618
  * });
649
619
  *
@@ -653,8 +623,8 @@ const defaultWaitForOptions = {
653
623
  * @param actorRef The actor ref to subscribe to
654
624
  * @param predicate Determines if a value matches the condition to wait for
655
625
  * @param options
656
- * @returns A promise that eventually resolves to the emitted value
657
- * that matches the condition
626
+ * @returns A promise that eventually resolves to the emitted value that matches
627
+ * the condition
658
628
  */
659
629
  function waitFor(actorRef, predicate, options) {
660
630
  const resolvedOptions = {
@@ -713,40 +683,42 @@ function waitFor(actorRef, predicate, options) {
713
683
  *
714
684
  * The state machine represents the pure logic of a state machine actor.
715
685
  *
716
- * @param config The state machine configuration.
717
- * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
718
- *
719
686
  * @example
720
- ```ts
721
- import { createMachine } from 'xstate';
722
-
723
- const lightMachine = createMachine({
724
- id: 'light',
725
- initial: 'green',
726
- states: {
727
- green: {
728
- on: {
729
- TIMER: { target: 'yellow' }
730
- }
731
- },
732
- yellow: {
733
- on: {
734
- TIMER: { target: 'red' }
735
- }
736
- },
737
- red: {
738
- on: {
739
- TIMER: { target: 'green' }
740
- }
741
- }
742
- }
743
- });
744
-
745
- const lightActor = createActor(lightMachine);
746
- lightActor.start();
747
-
748
- lightActor.send({ type: 'TIMER' });
749
- ```
687
+ *
688
+ * ```ts
689
+ * import { createMachine } from 'xstate';
690
+ *
691
+ * const lightMachine = createMachine({
692
+ * id: 'light',
693
+ * initial: 'green',
694
+ * states: {
695
+ * green: {
696
+ * on: {
697
+ * TIMER: { target: 'yellow' }
698
+ * }
699
+ * },
700
+ * yellow: {
701
+ * on: {
702
+ * TIMER: { target: 'red' }
703
+ * }
704
+ * },
705
+ * red: {
706
+ * on: {
707
+ * TIMER: { target: 'green' }
708
+ * }
709
+ * }
710
+ * }
711
+ * });
712
+ *
713
+ * const lightActor = createActor(lightMachine);
714
+ * lightActor.start();
715
+ *
716
+ * lightActor.send({ type: 'TIMER' });
717
+ * ```
718
+ *
719
+ * @param config The state machine configuration.
720
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })`
721
+ * to provide machine implementations instead.
750
722
  */
751
723
  function createMachine(config, implementations) {
752
724
  return new StateMachine(config, implementations);
@@ -773,33 +745,36 @@ function getInitialSnapshot(actorLogic, ...[input]) {
773
745
  }
774
746
 
775
747
  /**
776
- * Determines the next snapshot for the given `actorLogic` based on
777
- * the given `snapshot` and `event`.
748
+ * Determines the next snapshot for the given `actorLogic` based on the given
749
+ * `snapshot` and `event`.
778
750
  *
779
- * If the `snapshot` is `undefined`, the initial snapshot of the
780
- * `actorLogic` is used.
751
+ * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
752
+ * used.
781
753
  *
782
754
  * @example
783
- ```ts
784
- import { getNextSnapshot } from 'xstate';
785
- import { trafficLightMachine } from './trafficLightMachine.ts';
786
-
787
- const nextSnapshot = getNextSnapshot(
788
- trafficLightMachine, // actor logic
789
- undefined, // snapshot (or initial state if undefined)
790
- { type: 'TIMER' }); // event object
791
-
792
- console.log(nextSnapshot.value);
793
- // => 'yellow'
794
-
795
- const nextSnapshot2 = getNextSnapshot(
796
- trafficLightMachine, // actor logic
797
- nextSnapshot, // snapshot
798
- { type: 'TIMER' }); // event object
799
-
800
- console.log(nextSnapshot2.value);
801
- // =>'red'
802
- ```
755
+ *
756
+ * ```ts
757
+ * import { getNextSnapshot } from 'xstate';
758
+ * import { trafficLightMachine } from './trafficLightMachine.ts';
759
+ *
760
+ * const nextSnapshot = getNextSnapshot(
761
+ * trafficLightMachine, // actor logic
762
+ * undefined, // snapshot (or initial state if undefined)
763
+ * { type: 'TIMER' }
764
+ * ); // event object
765
+ *
766
+ * console.log(nextSnapshot.value);
767
+ * // => 'yellow'
768
+ *
769
+ * const nextSnapshot2 = getNextSnapshot(
770
+ * trafficLightMachine, // actor logic
771
+ * nextSnapshot, // snapshot
772
+ * { type: 'TIMER' }
773
+ * ); // event object
774
+ *
775
+ * console.log(nextSnapshot2.value);
776
+ * // =>'red'
777
+ * ```
803
778
  */
804
779
  function getNextSnapshot(actorLogic, snapshot, event) {
805
780
  const inertActorScope = createInertActorScope(actorLogic);
@@ -833,6 +808,7 @@ function setup({
833
808
  * Returns a promise that resolves to the `output` of the actor when it is done.
834
809
  *
835
810
  * @example
811
+ *
836
812
  * ```ts
837
813
  * const machine = createMachine({
838
814
  * // ...
@@ -863,26 +839,27 @@ function toPromise(actor) {
863
839
  }
864
840
 
865
841
  /**
866
- * Asserts that the given event object is of the specified type or types.
867
- * Throws an error if the event object is not of the specified types.
868
- @example
869
-
870
- ```ts
871
- // ...
872
- entry: ({ event }) => {
873
- assertEvent(event, 'doNothing');
874
- // event is { type: 'doNothing' }
875
- },
876
- // ...
877
- exit: ({ event }) => {
878
- assertEvent(event, 'greet');
879
- // event is { type: 'greet'; message: string }
880
-
881
- assertEvent(event, ['greet', 'notify']);
882
- // event is { type: 'greet'; message: string }
883
- // or { type: 'notify'; message: string; level: 'info' | 'error' }
884
- },
885
- ```
842
+ * Asserts that the given event object is of the specified type or types. Throws
843
+ * an error if the event object is not of the specified types.
844
+ *
845
+ * @example
846
+ *
847
+ * ```ts
848
+ * // ...
849
+ * entry: ({ event }) => {
850
+ * assertEvent(event, 'doNothing');
851
+ * // event is { type: 'doNothing' }
852
+ * },
853
+ * // ...
854
+ * exit: ({ event }) => {
855
+ * assertEvent(event, 'greet');
856
+ * // event is { type: 'greet'; message: string }
857
+ *
858
+ * assertEvent(event, ['greet', 'notify']);
859
+ * // event is { type: 'greet'; message: string }
860
+ * // or { type: 'notify'; message: string; level: 'info' | 'error' }
861
+ * },
862
+ * ```
886
863
  */
887
864
  function assertEvent(event, type) {
888
865
  const types = guards_dist_xstateGuards.toArray(type);