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.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-f79d2832.cjs.js');
7
- var log = require('./log-7ae0ddf8.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-a6298350.cjs.js');
7
+ var log = require('./log-f9587b82.cjs.js');
8
8
  require('../dev/dist/xstate-dev.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 ?? {},
@@ -432,12 +402,11 @@ class StateMachine {
432
402
  }
433
403
 
434
404
  /**
435
- * Clones this state machine with the provided implementations
436
- * and merges the `context` (if provided).
437
- *
438
- * @param implementations Options (`actions`, `guards`, `actors`, `delays`, `context`)
439
- * to recursively merge with the existing options.
405
+ * Clones this state machine with the provided implementations and merges the
406
+ * `context` (if provided).
440
407
  *
408
+ * @param implementations Options (`actions`, `guards`, `actors`, `delays`,
409
+ * `context`) to recursively merge with the existing options.
441
410
  * @returns A new `StateMachine` instance with the provided implementations.
442
411
  */
443
412
  provide(implementations) {
@@ -481,8 +450,8 @@ class StateMachine {
481
450
  }
482
451
 
483
452
  /**
484
- * Determines the next snapshot given the current `snapshot` and received `event`.
485
- * Calculates a full macrostep from all microsteps.
453
+ * Determines the next snapshot given the current `snapshot` and received
454
+ * `event`. Calculates a full macrostep from all microsteps.
486
455
  *
487
456
  * @param snapshot The current snapshot
488
457
  * @param event The received event
@@ -492,8 +461,8 @@ class StateMachine {
492
461
  }
493
462
 
494
463
  /**
495
- * Determines the next state given the current `state` and `event`.
496
- * Calculates a microstep.
464
+ * Determines the next state given the current `state` and `event`. Calculates
465
+ * a microstep.
497
466
  *
498
467
  * @param state The current state
499
468
  * @param event The received event
@@ -506,8 +475,8 @@ class StateMachine {
506
475
  }
507
476
 
508
477
  /**
509
- * The initial state _before_ evaluating any microsteps.
510
- * This "pre-initial" state is provided to initial actions executed in the initial state.
478
+ * The initial state _before_ evaluating any microsteps. This "pre-initial"
479
+ * state is provided to initial actions executed in the initial state.
511
480
  */
512
481
  getPreInitialState(actorScope, initEvent, internalQueue) {
513
482
  const {
@@ -535,7 +504,8 @@ class StateMachine {
535
504
  }
536
505
 
537
506
  /**
538
- * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
507
+ * Returns the initial `State` instance, with reference to `self` as an
508
+ * `ActorRef`.
539
509
  */
540
510
  getInitialSnapshot(actorScope, input) {
541
511
  const initEvent = guards_dist_xstateGuards.createInitEvent(input); // TODO: fix;
@@ -633,14 +603,14 @@ const defaultWaitForOptions = {
633
603
  };
634
604
 
635
605
  /**
636
- * Subscribes to an actor ref and waits for its emitted value to satisfy
637
- * a predicate, and then resolves with that value.
638
- * Will throw if the desired state is not reached after an optional timeout.
639
- * (defaults to Infinity).
606
+ * Subscribes to an actor ref and waits for its emitted value to satisfy a
607
+ * predicate, and then resolves with that value. Will throw if the desired state
608
+ * is not reached after an optional timeout. (defaults to Infinity).
640
609
  *
641
610
  * @example
611
+ *
642
612
  * ```js
643
- * const state = await waitFor(someService, state => {
613
+ * const state = await waitFor(someService, (state) => {
644
614
  * return state.hasTag('loaded');
645
615
  * });
646
616
  *
@@ -650,8 +620,8 @@ const defaultWaitForOptions = {
650
620
  * @param actorRef The actor ref to subscribe to
651
621
  * @param predicate Determines if a value matches the condition to wait for
652
622
  * @param options
653
- * @returns A promise that eventually resolves to the emitted value
654
- * that matches the condition
623
+ * @returns A promise that eventually resolves to the emitted value that matches
624
+ * the condition
655
625
  */
656
626
  function waitFor(actorRef, predicate, options) {
657
627
  const resolvedOptions = {
@@ -707,40 +677,42 @@ function waitFor(actorRef, predicate, options) {
707
677
  *
708
678
  * The state machine represents the pure logic of a state machine actor.
709
679
  *
710
- * @param config The state machine configuration.
711
- * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
712
- *
713
680
  * @example
714
- ```ts
715
- import { createMachine } from 'xstate';
716
-
717
- const lightMachine = createMachine({
718
- id: 'light',
719
- initial: 'green',
720
- states: {
721
- green: {
722
- on: {
723
- TIMER: { target: 'yellow' }
724
- }
725
- },
726
- yellow: {
727
- on: {
728
- TIMER: { target: 'red' }
729
- }
730
- },
731
- red: {
732
- on: {
733
- TIMER: { target: 'green' }
734
- }
735
- }
736
- }
737
- });
738
-
739
- const lightActor = createActor(lightMachine);
740
- lightActor.start();
741
-
742
- lightActor.send({ type: 'TIMER' });
743
- ```
681
+ *
682
+ * ```ts
683
+ * import { createMachine } from 'xstate';
684
+ *
685
+ * const lightMachine = createMachine({
686
+ * id: 'light',
687
+ * initial: 'green',
688
+ * states: {
689
+ * green: {
690
+ * on: {
691
+ * TIMER: { target: 'yellow' }
692
+ * }
693
+ * },
694
+ * yellow: {
695
+ * on: {
696
+ * TIMER: { target: 'red' }
697
+ * }
698
+ * },
699
+ * red: {
700
+ * on: {
701
+ * TIMER: { target: 'green' }
702
+ * }
703
+ * }
704
+ * }
705
+ * });
706
+ *
707
+ * const lightActor = createActor(lightMachine);
708
+ * lightActor.start();
709
+ *
710
+ * lightActor.send({ type: 'TIMER' });
711
+ * ```
712
+ *
713
+ * @param config The state machine configuration.
714
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })`
715
+ * to provide machine implementations instead.
744
716
  */
745
717
  function createMachine(config, implementations) {
746
718
  return new StateMachine(config, implementations);
@@ -767,33 +739,36 @@ function getInitialSnapshot(actorLogic, ...[input]) {
767
739
  }
768
740
 
769
741
  /**
770
- * Determines the next snapshot for the given `actorLogic` based on
771
- * the given `snapshot` and `event`.
742
+ * Determines the next snapshot for the given `actorLogic` based on the given
743
+ * `snapshot` and `event`.
772
744
  *
773
- * If the `snapshot` is `undefined`, the initial snapshot of the
774
- * `actorLogic` is used.
745
+ * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
746
+ * used.
775
747
  *
776
748
  * @example
777
- ```ts
778
- import { getNextSnapshot } from 'xstate';
779
- import { trafficLightMachine } from './trafficLightMachine.ts';
780
-
781
- const nextSnapshot = getNextSnapshot(
782
- trafficLightMachine, // actor logic
783
- undefined, // snapshot (or initial state if undefined)
784
- { type: 'TIMER' }); // event object
785
-
786
- console.log(nextSnapshot.value);
787
- // => 'yellow'
788
-
789
- const nextSnapshot2 = getNextSnapshot(
790
- trafficLightMachine, // actor logic
791
- nextSnapshot, // snapshot
792
- { type: 'TIMER' }); // event object
793
-
794
- console.log(nextSnapshot2.value);
795
- // =>'red'
796
- ```
749
+ *
750
+ * ```ts
751
+ * import { getNextSnapshot } from 'xstate';
752
+ * import { trafficLightMachine } from './trafficLightMachine.ts';
753
+ *
754
+ * const nextSnapshot = getNextSnapshot(
755
+ * trafficLightMachine, // actor logic
756
+ * undefined, // snapshot (or initial state if undefined)
757
+ * { type: 'TIMER' }
758
+ * ); // event object
759
+ *
760
+ * console.log(nextSnapshot.value);
761
+ * // => 'yellow'
762
+ *
763
+ * const nextSnapshot2 = getNextSnapshot(
764
+ * trafficLightMachine, // actor logic
765
+ * nextSnapshot, // snapshot
766
+ * { type: 'TIMER' }
767
+ * ); // event object
768
+ *
769
+ * console.log(nextSnapshot2.value);
770
+ * // =>'red'
771
+ * ```
797
772
  */
798
773
  function getNextSnapshot(actorLogic, snapshot, event) {
799
774
  const inertActorScope = createInertActorScope(actorLogic);
@@ -827,6 +802,7 @@ function setup({
827
802
  * Returns a promise that resolves to the `output` of the actor when it is done.
828
803
  *
829
804
  * @example
805
+ *
830
806
  * ```ts
831
807
  * const machine = createMachine({
832
808
  * // ...
@@ -857,26 +833,27 @@ function toPromise(actor) {
857
833
  }
858
834
 
859
835
  /**
860
- * Asserts that the given event object is of the specified type or types.
861
- * Throws an error if the event object is not of the specified types.
862
- @example
863
-
864
- ```ts
865
- // ...
866
- entry: ({ event }) => {
867
- assertEvent(event, 'doNothing');
868
- // event is { type: 'doNothing' }
869
- },
870
- // ...
871
- exit: ({ event }) => {
872
- assertEvent(event, 'greet');
873
- // event is { type: 'greet'; message: string }
874
-
875
- assertEvent(event, ['greet', 'notify']);
876
- // event is { type: 'greet'; message: string }
877
- // or { type: 'notify'; message: string; level: 'info' | 'error' }
878
- },
879
- ```
836
+ * Asserts that the given event object is of the specified type or types. Throws
837
+ * an error if the event object is not of the specified types.
838
+ *
839
+ * @example
840
+ *
841
+ * ```ts
842
+ * // ...
843
+ * entry: ({ event }) => {
844
+ * assertEvent(event, 'doNothing');
845
+ * // event is { type: 'doNothing' }
846
+ * },
847
+ * // ...
848
+ * exit: ({ event }) => {
849
+ * assertEvent(event, 'greet');
850
+ * // event is { type: 'greet'; message: string }
851
+ *
852
+ * assertEvent(event, ['greet', 'notify']);
853
+ * // event is { type: 'greet'; message: string }
854
+ * // or { type: 'notify'; message: string; level: 'info' | 'error' }
855
+ * },
856
+ * ```
880
857
  */
881
858
  function assertEvent(event, type) {
882
859
  const types = guards_dist_xstateGuards.toArray(type);