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,8 +1,8 @@
1
1
  export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.esm.js';
2
- import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-4e39e875.esm.js';
3
- export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, E as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, D as isMachineSnapshot, J as matchesState, F as not, G as or, K as pathToStateValue, O as raise, P as spawnChild, H as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-4e39e875.esm.js';
4
- import { a as assign } from './log-b87cb6bd.esm.js';
5
- export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-b87cb6bd.esm.js';
2
+ import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, e as evaluateGuard, c as createInvokeId, g as getDelayedTransitions, d as formatInitialTransition, h as getCandidates, r as resolveStateValue, i as getAllStateNodes, j as getStateNodes, k as createMachineSnapshot, l as isInFinalState, n as macrostep, o as transitionNode, p as resolveActionsAndContext, q as createInitEvent, s as microstep, u as getInitialStateNodes, v as toStatePath, w as isStateId, x as getStateNodeByPath, y as getPersistedSnapshot, z as resolveReferencedActor, A as createActor, $ as $$ACTOR_TYPE } from './raise-2cfe6b8f.esm.js';
3
+ export { B as Actor, I as __unsafe_getAllOwnEventDescriptors, E as and, M as cancel, A as createActor, j as getStateNodes, C as interpret, D as isMachineSnapshot, J as matchesState, F as not, G as or, K as pathToStateValue, O as raise, P as spawnChild, H as stateIn, Q as stop, R as stopChild, L as toObserver } from './raise-2cfe6b8f.esm.js';
4
+ import { a as assign } from './log-63de2429.esm.js';
5
+ export { S as SpecialTargets, a as assign, e as emit, b as enqueueActions, f as forwardTo, l as log, s as sendParent, c as sendTo } from './log-63de2429.esm.js';
6
6
  import '../dev/dist/xstate-dev.esm.js';
7
7
 
8
8
  class SimulatedClock {
@@ -105,71 +105,58 @@ const toSerializableAction = action => {
105
105
  return action;
106
106
  };
107
107
  class StateNode {
108
- constructor(
109
- /**
110
- * The raw config used to create the machine.
111
- */
108
+ constructor( /** The raw config used to create the machine. */
112
109
  config, options) {
113
110
  this.config = config;
114
111
  /**
115
- * The relative key of the state node, which represents its location in the overall state value.
112
+ * The relative key of the state node, which represents its location in the
113
+ * overall state value.
116
114
  */
117
115
  this.key = void 0;
118
- /**
119
- * The unique ID of the state node.
120
- */
116
+ /** The unique ID of the state node. */
121
117
  this.id = void 0;
122
118
  /**
123
119
  * The type of this state node:
124
120
  *
125
- * - `'atomic'` - no child state nodes
126
- * - `'compound'` - nested child state nodes (XOR)
127
- * - `'parallel'` - orthogonal nested child state nodes (AND)
128
- * - `'history'` - history state node
129
- * - `'final'` - final state node
121
+ * - `'atomic'` - no child state nodes
122
+ * - `'compound'` - nested child state nodes (XOR)
123
+ * - `'parallel'` - orthogonal nested child state nodes (AND)
124
+ * - `'history'` - history state node
125
+ * - `'final'` - final state node
130
126
  */
131
127
  this.type = void 0;
132
- /**
133
- * The string path from the root machine node to this node.
134
- */
128
+ /** The string path from the root machine node to this node. */
135
129
  this.path = void 0;
136
- /**
137
- * The child state nodes.
138
- */
130
+ /** The child state nodes. */
139
131
  this.states = void 0;
140
132
  /**
141
133
  * The type of history on this state node. Can be:
142
134
  *
143
- * - `'shallow'` - recalls only top-level historical state value
144
- * - `'deep'` - recalls historical state value at all levels
135
+ * - `'shallow'` - recalls only top-level historical state value
136
+ * - `'deep'` - recalls historical state value at all levels
145
137
  */
146
138
  this.history = void 0;
147
- /**
148
- * The action(s) to be executed upon entering the state node.
149
- */
139
+ /** The action(s) to be executed upon entering the state node. */
150
140
  this.entry = void 0;
151
- /**
152
- * The action(s) to be executed upon exiting the state node.
153
- */
141
+ /** The action(s) to be executed upon exiting the state node. */
154
142
  this.exit = void 0;
155
- /**
156
- * The parent state node.
157
- */
143
+ /** The parent state node. */
158
144
  this.parent = void 0;
159
- /**
160
- * The root machine node.
161
- */
145
+ /** The root machine node. */
162
146
  this.machine = void 0;
163
147
  /**
164
- * The meta data associated with this state node, which will be returned in State instances.
148
+ * The meta data associated with this state node, which will be returned in
149
+ * State instances.
165
150
  */
166
151
  this.meta = void 0;
167
152
  /**
168
- * The output data sent with the "xstate.done.state._id_" event if this is a final state node.
153
+ * The output data sent with the "xstate.done.state._id_" event if this is a
154
+ * final state node.
169
155
  */
170
156
  this.output = void 0;
171
157
  /**
172
- * The order this state node appears. Corresponds to the implicit document order.
158
+ * The order this state node appears. Corresponds to the implicit document
159
+ * order.
173
160
  */
174
161
  this.order = -1;
175
162
  this.description = void 0;
@@ -217,9 +204,7 @@ class StateNode {
217
204
  });
218
205
  }
219
206
 
220
- /**
221
- * The well-structured state node definition.
222
- */
207
+ /** The well-structured state node definition. */
223
208
  get definition() {
224
209
  return {
225
210
  id: this.id,
@@ -264,9 +249,7 @@ class StateNode {
264
249
  return this.definition;
265
250
  }
266
251
 
267
- /**
268
- * The logic invoked as actors by this state node.
269
- */
252
+ /** The logic invoked as actors by this state node. */
270
253
  get invoke() {
271
254
  return memo(this, 'invoke', () => toArray(this.config.invoke).map((invokeConfig, i) => {
272
255
  const {
@@ -297,9 +280,7 @@ class StateNode {
297
280
  }));
298
281
  }
299
282
 
300
- /**
301
- * The mapping of events to transitions.
302
- */
283
+ /** The mapping of events to transitions. */
303
284
  get on() {
304
285
  return memo(this, 'on', () => {
305
286
  const transitions = this.transitions;
@@ -344,9 +325,7 @@ class StateNode {
344
325
  return selectedTransition ? [selectedTransition] : undefined;
345
326
  }
346
327
 
347
- /**
348
- * All the event types accepted by this state node and its descendants.
349
- */
328
+ /** All the event types accepted by this state node and its descendants. */
350
329
  get events() {
351
330
  return memo(this, 'events', () => {
352
331
  const {
@@ -382,15 +361,10 @@ class StateNode {
382
361
 
383
362
  const STATE_IDENTIFIER = '#';
384
363
  class StateMachine {
385
- constructor(
386
- /**
387
- * The raw config used to create the machine.
388
- */
364
+ constructor( /** The raw config used to create the machine. */
389
365
  config, implementations) {
390
366
  this.config = config;
391
- /**
392
- * The machine's own version.
393
- */
367
+ /** The machine's own version. */
394
368
  this.version = void 0;
395
369
  this.schemas = void 0;
396
370
  this.implementations = void 0;
@@ -402,10 +376,6 @@ class StateMachine {
402
376
  this.id = void 0;
403
377
  this.states = void 0;
404
378
  this.events = void 0;
405
- /**
406
- * @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
407
- **/
408
- this.__TResolvedTypesMeta = void 0;
409
379
  this.id = config.id || '(machine)';
410
380
  this.implementations = {
411
381
  actors: implementations?.actors ?? {},
@@ -430,12 +400,11 @@ class StateMachine {
430
400
  }
431
401
 
432
402
  /**
433
- * Clones this state machine with the provided implementations
434
- * and merges the `context` (if provided).
435
- *
436
- * @param implementations Options (`actions`, `guards`, `actors`, `delays`, `context`)
437
- * to recursively merge with the existing options.
403
+ * Clones this state machine with the provided implementations and merges the
404
+ * `context` (if provided).
438
405
  *
406
+ * @param implementations Options (`actions`, `guards`, `actors`, `delays`,
407
+ * `context`) to recursively merge with the existing options.
439
408
  * @returns A new `StateMachine` instance with the provided implementations.
440
409
  */
441
410
  provide(implementations) {
@@ -479,8 +448,8 @@ class StateMachine {
479
448
  }
480
449
 
481
450
  /**
482
- * Determines the next snapshot given the current `snapshot` and received `event`.
483
- * Calculates a full macrostep from all microsteps.
451
+ * Determines the next snapshot given the current `snapshot` and received
452
+ * `event`. Calculates a full macrostep from all microsteps.
484
453
  *
485
454
  * @param snapshot The current snapshot
486
455
  * @param event The received event
@@ -490,8 +459,8 @@ class StateMachine {
490
459
  }
491
460
 
492
461
  /**
493
- * Determines the next state given the current `state` and `event`.
494
- * Calculates a microstep.
462
+ * Determines the next state given the current `state` and `event`. Calculates
463
+ * a microstep.
495
464
  *
496
465
  * @param state The current state
497
466
  * @param event The received event
@@ -504,8 +473,8 @@ class StateMachine {
504
473
  }
505
474
 
506
475
  /**
507
- * The initial state _before_ evaluating any microsteps.
508
- * This "pre-initial" state is provided to initial actions executed in the initial state.
476
+ * The initial state _before_ evaluating any microsteps. This "pre-initial"
477
+ * state is provided to initial actions executed in the initial state.
509
478
  */
510
479
  getPreInitialState(actorScope, initEvent, internalQueue) {
511
480
  const {
@@ -533,7 +502,8 @@ class StateMachine {
533
502
  }
534
503
 
535
504
  /**
536
- * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
505
+ * Returns the initial `State` instance, with reference to `self` as an
506
+ * `ActorRef`.
537
507
  */
538
508
  getInitialSnapshot(actorScope, input) {
539
509
  const initEvent = createInitEvent(input); // TODO: fix;
@@ -631,14 +601,14 @@ const defaultWaitForOptions = {
631
601
  };
632
602
 
633
603
  /**
634
- * Subscribes to an actor ref and waits for its emitted value to satisfy
635
- * a predicate, and then resolves with that value.
636
- * Will throw if the desired state is not reached after an optional timeout.
637
- * (defaults to Infinity).
604
+ * Subscribes to an actor ref and waits for its emitted value to satisfy a
605
+ * predicate, and then resolves with that value. Will throw if the desired state
606
+ * is not reached after an optional timeout. (defaults to Infinity).
638
607
  *
639
608
  * @example
609
+ *
640
610
  * ```js
641
- * const state = await waitFor(someService, state => {
611
+ * const state = await waitFor(someService, (state) => {
642
612
  * return state.hasTag('loaded');
643
613
  * });
644
614
  *
@@ -648,8 +618,8 @@ const defaultWaitForOptions = {
648
618
  * @param actorRef The actor ref to subscribe to
649
619
  * @param predicate Determines if a value matches the condition to wait for
650
620
  * @param options
651
- * @returns A promise that eventually resolves to the emitted value
652
- * that matches the condition
621
+ * @returns A promise that eventually resolves to the emitted value that matches
622
+ * the condition
653
623
  */
654
624
  function waitFor(actorRef, predicate, options) {
655
625
  const resolvedOptions = {
@@ -705,40 +675,42 @@ function waitFor(actorRef, predicate, options) {
705
675
  *
706
676
  * The state machine represents the pure logic of a state machine actor.
707
677
  *
708
- * @param config The state machine configuration.
709
- * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
710
- *
711
678
  * @example
712
- ```ts
713
- import { createMachine } from 'xstate';
714
-
715
- const lightMachine = createMachine({
716
- id: 'light',
717
- initial: 'green',
718
- states: {
719
- green: {
720
- on: {
721
- TIMER: { target: 'yellow' }
722
- }
723
- },
724
- yellow: {
725
- on: {
726
- TIMER: { target: 'red' }
727
- }
728
- },
729
- red: {
730
- on: {
731
- TIMER: { target: 'green' }
732
- }
733
- }
734
- }
735
- });
736
-
737
- const lightActor = createActor(lightMachine);
738
- lightActor.start();
739
-
740
- lightActor.send({ type: 'TIMER' });
741
- ```
679
+ *
680
+ * ```ts
681
+ * import { createMachine } from 'xstate';
682
+ *
683
+ * const lightMachine = createMachine({
684
+ * id: 'light',
685
+ * initial: 'green',
686
+ * states: {
687
+ * green: {
688
+ * on: {
689
+ * TIMER: { target: 'yellow' }
690
+ * }
691
+ * },
692
+ * yellow: {
693
+ * on: {
694
+ * TIMER: { target: 'red' }
695
+ * }
696
+ * },
697
+ * red: {
698
+ * on: {
699
+ * TIMER: { target: 'green' }
700
+ * }
701
+ * }
702
+ * }
703
+ * });
704
+ *
705
+ * const lightActor = createActor(lightMachine);
706
+ * lightActor.start();
707
+ *
708
+ * lightActor.send({ type: 'TIMER' });
709
+ * ```
710
+ *
711
+ * @param config The state machine configuration.
712
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })`
713
+ * to provide machine implementations instead.
742
714
  */
743
715
  function createMachine(config, implementations) {
744
716
  return new StateMachine(config, implementations);
@@ -765,33 +737,36 @@ function getInitialSnapshot(actorLogic, ...[input]) {
765
737
  }
766
738
 
767
739
  /**
768
- * Determines the next snapshot for the given `actorLogic` based on
769
- * the given `snapshot` and `event`.
740
+ * Determines the next snapshot for the given `actorLogic` based on the given
741
+ * `snapshot` and `event`.
770
742
  *
771
- * If the `snapshot` is `undefined`, the initial snapshot of the
772
- * `actorLogic` is used.
743
+ * If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
744
+ * used.
773
745
  *
774
746
  * @example
775
- ```ts
776
- import { getNextSnapshot } from 'xstate';
777
- import { trafficLightMachine } from './trafficLightMachine.ts';
778
-
779
- const nextSnapshot = getNextSnapshot(
780
- trafficLightMachine, // actor logic
781
- undefined, // snapshot (or initial state if undefined)
782
- { type: 'TIMER' }); // event object
783
-
784
- console.log(nextSnapshot.value);
785
- // => 'yellow'
786
-
787
- const nextSnapshot2 = getNextSnapshot(
788
- trafficLightMachine, // actor logic
789
- nextSnapshot, // snapshot
790
- { type: 'TIMER' }); // event object
791
-
792
- console.log(nextSnapshot2.value);
793
- // =>'red'
794
- ```
747
+ *
748
+ * ```ts
749
+ * import { getNextSnapshot } from 'xstate';
750
+ * import { trafficLightMachine } from './trafficLightMachine.ts';
751
+ *
752
+ * const nextSnapshot = getNextSnapshot(
753
+ * trafficLightMachine, // actor logic
754
+ * undefined, // snapshot (or initial state if undefined)
755
+ * { type: 'TIMER' }
756
+ * ); // event object
757
+ *
758
+ * console.log(nextSnapshot.value);
759
+ * // => 'yellow'
760
+ *
761
+ * const nextSnapshot2 = getNextSnapshot(
762
+ * trafficLightMachine, // actor logic
763
+ * nextSnapshot, // snapshot
764
+ * { type: 'TIMER' }
765
+ * ); // event object
766
+ *
767
+ * console.log(nextSnapshot2.value);
768
+ * // =>'red'
769
+ * ```
795
770
  */
796
771
  function getNextSnapshot(actorLogic, snapshot, event) {
797
772
  const inertActorScope = createInertActorScope(actorLogic);
@@ -825,6 +800,7 @@ function setup({
825
800
  * Returns a promise that resolves to the `output` of the actor when it is done.
826
801
  *
827
802
  * @example
803
+ *
828
804
  * ```ts
829
805
  * const machine = createMachine({
830
806
  * // ...
@@ -855,26 +831,27 @@ function toPromise(actor) {
855
831
  }
856
832
 
857
833
  /**
858
- * Asserts that the given event object is of the specified type or types.
859
- * Throws an error if the event object is not of the specified types.
860
- @example
861
-
862
- ```ts
863
- // ...
864
- entry: ({ event }) => {
865
- assertEvent(event, 'doNothing');
866
- // event is { type: 'doNothing' }
867
- },
868
- // ...
869
- exit: ({ event }) => {
870
- assertEvent(event, 'greet');
871
- // event is { type: 'greet'; message: string }
872
-
873
- assertEvent(event, ['greet', 'notify']);
874
- // event is { type: 'greet'; message: string }
875
- // or { type: 'notify'; message: string; level: 'info' | 'error' }
876
- },
877
- ```
834
+ * Asserts that the given event object is of the specified type or types. Throws
835
+ * an error if the event object is not of the specified types.
836
+ *
837
+ * @example
838
+ *
839
+ * ```ts
840
+ * // ...
841
+ * entry: ({ event }) => {
842
+ * assertEvent(event, 'doNothing');
843
+ * // event is { type: 'doNothing' }
844
+ * },
845
+ * // ...
846
+ * exit: ({ event }) => {
847
+ * assertEvent(event, 'greet');
848
+ * // event is { type: 'greet'; message: string }
849
+ *
850
+ * assertEvent(event, ['greet', 'notify']);
851
+ * // event is { type: 'greet'; message: string }
852
+ * // or { type: 'notify'; message: string; level: 'info' | 'error' }
853
+ * },
854
+ * ```
878
855
  */
879
856
  function assertEvent(event, type) {
880
857
  const types = toArray(type);