xstate 5.0.0-beta.27 → 5.0.0-beta.28

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/README.md +9 -7
  2. package/actions/dist/xstate-actions.cjs.js +11 -17
  3. package/actions/dist/xstate-actions.cjs.mjs +0 -8
  4. package/actions/dist/xstate-actions.development.cjs.js +11 -17
  5. package/actions/dist/xstate-actions.development.cjs.mjs +0 -8
  6. package/actions/dist/xstate-actions.development.esm.js +3 -1
  7. package/actions/dist/xstate-actions.esm.js +3 -1
  8. package/actions/dist/xstate-actions.umd.min.js +1 -1
  9. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  10. package/actors/dist/xstate-actors.cjs.js +413 -11
  11. package/actors/dist/xstate-actors.development.cjs.js +413 -11
  12. package/actors/dist/xstate-actors.development.esm.js +405 -4
  13. package/actors/dist/xstate-actors.esm.js +405 -4
  14. package/actors/dist/xstate-actors.umd.min.js +1 -1
  15. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  16. package/dist/declarations/src/StateNode.d.ts +3 -3
  17. package/dist/declarations/src/actions/assign.d.ts +8 -3
  18. package/dist/declarations/src/actions/choose.d.ts +4 -3
  19. package/dist/declarations/src/actions/pure.d.ts +5 -4
  20. package/dist/declarations/src/actions.d.ts +8 -44
  21. package/dist/declarations/src/actors/index.d.ts +2 -2
  22. package/dist/declarations/src/constants.d.ts +1 -0
  23. package/dist/declarations/src/index.d.ts +9 -16
  24. package/dist/declarations/src/spawn.d.ts +25 -0
  25. package/dist/declarations/src/stateUtils.d.ts +1 -1
  26. package/dist/declarations/src/typegenTypes.d.ts +4 -4
  27. package/dist/declarations/src/types.d.ts +72 -94
  28. package/dist/declarations/src/utils.d.ts +2 -2
  29. package/dist/interpreter-672794ae.cjs.js +792 -0
  30. package/dist/interpreter-a1432c7d.development.cjs.js +800 -0
  31. package/dist/interpreter-a77bb0ec.development.esm.js +765 -0
  32. package/dist/interpreter-b5203bcb.esm.js +757 -0
  33. package/dist/{actions-9754d2ca.development.esm.js → raise-436a57a2.cjs.js} +130 -1307
  34. package/dist/{actions-ca622922.development.cjs.js → raise-74b72ca5.development.cjs.js} +101 -1306
  35. package/dist/{actions-020463e9.esm.js → raise-a60c9290.development.esm.js} +109 -1203
  36. package/dist/{actions-d1dba4ac.cjs.js → raise-b9c9a234.esm.js} +65 -1267
  37. package/dist/send-35e1a689.cjs.js +349 -0
  38. package/dist/send-4192e7bc.esm.js +339 -0
  39. package/dist/send-e63b7b83.development.esm.js +364 -0
  40. package/dist/send-e8b55d00.development.cjs.js +374 -0
  41. package/dist/xstate.cjs.js +110 -106
  42. package/dist/xstate.cjs.mjs +4 -2
  43. package/dist/xstate.development.cjs.js +110 -106
  44. package/dist/xstate.development.cjs.mjs +4 -2
  45. package/dist/xstate.development.esm.js +72 -68
  46. package/dist/xstate.esm.js +72 -68
  47. package/dist/xstate.umd.min.js +1 -1
  48. package/dist/xstate.umd.min.js.map +1 -1
  49. package/guards/dist/xstate-guards.cjs.js +2 -1
  50. package/guards/dist/xstate-guards.development.cjs.js +2 -1
  51. package/guards/dist/xstate-guards.development.esm.js +2 -1
  52. package/guards/dist/xstate-guards.esm.js +2 -1
  53. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  54. package/package.json +1 -1
  55. package/dist/declarations/src/constantPrefixes.d.ts +0 -6
  56. package/dist/promise-2ad94e3b.development.esm.js +0 -406
  57. package/dist/promise-3b7e3357.development.cjs.js +0 -412
  58. package/dist/promise-5b07c38e.esm.js +0 -406
  59. package/dist/promise-7a8c1768.cjs.js +0 -412
@@ -2,10 +2,61 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('./actions-d1dba4ac.cjs.js');
6
- var promise = require('./promise-7a8c1768.cjs.js');
5
+ var actors_dist_xstateActors = require('../actors/dist/xstate-actors.cjs.js');
6
+ var interpreter = require('./interpreter-672794ae.cjs.js');
7
+ var guards_dist_xstateGuards = require('./raise-436a57a2.cjs.js');
8
+ var send = require('./send-35e1a689.cjs.js');
7
9
  require('../dev/dist/xstate-dev.cjs.js');
8
10
 
11
+ class SimulatedClock {
12
+ constructor() {
13
+ this.timeouts = new Map();
14
+ this._now = 0;
15
+ this._id = 0;
16
+ }
17
+ now() {
18
+ return this._now;
19
+ }
20
+ getId() {
21
+ return this._id++;
22
+ }
23
+ setTimeout(fn, timeout) {
24
+ const id = this.getId();
25
+ this.timeouts.set(id, {
26
+ start: this.now(),
27
+ timeout,
28
+ fn
29
+ });
30
+ return id;
31
+ }
32
+ clearTimeout(id) {
33
+ this.timeouts.delete(id);
34
+ }
35
+ set(time) {
36
+ if (this._now > time) {
37
+ throw new Error('Unable to travel back in time');
38
+ }
39
+ this._now = time;
40
+ this.flushTimeouts();
41
+ }
42
+ flushTimeouts() {
43
+ [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
44
+ const endA = timeoutA.start + timeoutA.timeout;
45
+ const endB = timeoutB.start + timeoutB.timeout;
46
+ return endB > endA ? -1 : 1;
47
+ }).forEach(([id, timeout]) => {
48
+ if (this.now() - timeout.start >= timeout.timeout) {
49
+ this.timeouts.delete(id);
50
+ timeout.fn.call(null);
51
+ }
52
+ });
53
+ }
54
+ increment(ms) {
55
+ this._now += ms;
56
+ this.flushTimeouts();
57
+ }
58
+ }
59
+
9
60
  const EMPTY_OBJECT = {};
10
61
  const toSerializableActon = action => {
11
62
  if (typeof action === 'string') {
@@ -80,7 +131,7 @@ class StateNode {
80
131
  */
81
132
 
82
133
  /**
83
- * The output data sent with the "done.state._id_" event if this is a final state node.
134
+ * The output data sent with the "xstate.done.state._id_" event if this is a final state node.
84
135
  */
85
136
 
86
137
  /**
@@ -114,12 +165,12 @@ class StateNode {
114
165
  this.key = options._key;
115
166
  this.machine = options._machine;
116
167
  this.path = this.parent ? this.parent.path.concat(this.key) : [];
117
- this.id = this.config.id || [this.machine.id, ...this.path].join(guards_dist_xstateGuards.STATE_DELIMITER);
168
+ this.id = this.config.id || [this.machine.id, ...this.path].join(interpreter.STATE_DELIMITER);
118
169
  this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
119
170
  this.description = this.config.description;
120
171
  this.order = this.machine.idMap.size;
121
172
  this.machine.idMap.set(this.id, this);
122
- this.states = this.config.states ? guards_dist_xstateGuards.mapValues(this.config.states, (stateConfig, key) => {
173
+ this.states = this.config.states ? interpreter.mapValues(this.config.states, (stateConfig, key) => {
123
174
  const stateNode = new StateNode(stateConfig, {
124
175
  _parent: this,
125
176
  _key: key,
@@ -133,16 +184,16 @@ class StateNode {
133
184
 
134
185
  // History config
135
186
  this.history = this.config.history === true ? 'shallow' : this.config.history || false;
136
- this.entry = guards_dist_xstateGuards.toArray(this.config.entry).slice();
137
- this.exit = guards_dist_xstateGuards.toArray(this.config.exit).slice();
187
+ this.entry = interpreter.toArray(this.config.entry).slice();
188
+ this.exit = interpreter.toArray(this.config.exit).slice();
138
189
  this.meta = this.config.meta;
139
190
  this.output = this.type === 'final' ? this.config.output : undefined;
140
- this.tags = guards_dist_xstateGuards.toArray(config.tags).slice();
191
+ this.tags = interpreter.toArray(config.tags).slice();
141
192
  }
142
193
  _initialize() {
143
194
  this.transitions = guards_dist_xstateGuards.formatTransitions(this);
144
195
  if (this.config.always) {
145
- this.always = guards_dist_xstateGuards.toTransitionConfigArray(this.config.always).map(t => guards_dist_xstateGuards.formatTransition(this, guards_dist_xstateGuards.NULL_EVENT, t));
196
+ this.always = interpreter.toTransitionConfigArray(this.config.always).map(t => guards_dist_xstateGuards.formatTransition(this, interpreter.NULL_EVENT, t));
146
197
  }
147
198
  Object.keys(this.states).forEach(key => {
148
199
  this.states[key]._initialize();
@@ -172,7 +223,7 @@ class StateNode {
172
223
  })
173
224
  } : undefined,
174
225
  history: this.history,
175
- states: guards_dist_xstateGuards.mapValues(this.states, state => {
226
+ states: interpreter.mapValues(this.states, state => {
176
227
  return state.definition;
177
228
  }),
178
229
  on: this.on,
@@ -198,12 +249,12 @@ class StateNode {
198
249
  * The logic invoked as actors by this state node.
199
250
  */
200
251
  get invoke() {
201
- return guards_dist_xstateGuards.memo(this, 'invoke', () => guards_dist_xstateGuards.toArray(this.config.invoke).map((invokeConfig, i) => {
252
+ return guards_dist_xstateGuards.memo(this, 'invoke', () => interpreter.toArray(this.config.invoke).map((invokeConfig, i) => {
202
253
  const {
203
254
  src,
204
255
  systemId
205
256
  } = invokeConfig;
206
- const resolvedId = invokeConfig.id || guards_dist_xstateGuards.createInvokeId(this.id, i);
257
+ const resolvedId = invokeConfig.id || interpreter.createInvokeId(this.id, i);
207
258
  // TODO: resolving should not happen here
208
259
  const resolvedSrc = typeof src === 'string' ? src : !('type' in src) ? resolvedId : src;
209
260
  if (!this.machine.implementations.actors[resolvedId] && typeof src !== 'string' && !('type' in src)) {
@@ -433,7 +484,7 @@ class StateMachine {
433
484
  */
434
485
  transition(state, event, actorCtx) {
435
486
  // TODO: handle error events in a better way
436
- if (guards_dist_xstateGuards.isErrorEvent(event) && !state.nextEvents.some(nextEvent => nextEvent === event.type)) {
487
+ if (interpreter.isErrorActorEvent(event) && !state.nextEvents.some(nextEvent => nextEvent === event.type)) {
437
488
  return guards_dist_xstateGuards.cloneState(state, {
438
489
  error: event.data
439
490
  });
@@ -482,7 +533,7 @@ class StateMachine {
482
533
  spawn,
483
534
  input: event.input
484
535
  });
485
- return guards_dist_xstateGuards.resolveActionsAndContext([guards_dist_xstateGuards.assign(assignment)], initEvent, preInitial, actorCtx);
536
+ return guards_dist_xstateGuards.resolveActionsAndContext([send.assign(assignment)], initEvent, preInitial, actorCtx);
486
537
  }
487
538
  return preInitial;
488
539
  }
@@ -491,7 +542,7 @@ class StateMachine {
491
542
  * Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
492
543
  */
493
544
  getInitialState(actorCtx, input) {
494
- const initEvent = guards_dist_xstateGuards.createInitEvent(input); // TODO: fix;
545
+ const initEvent = interpreter.createInitEvent(input); // TODO: fix;
495
546
 
496
547
  const preInitialState = this.getPreInitialState(actorCtx, initEvent);
497
548
  const nextState = guards_dist_xstateGuards.microstep([{
@@ -515,7 +566,7 @@ class StateMachine {
515
566
  });
516
567
  }
517
568
  getStateNodeById(stateId) {
518
- const fullPath = stateId.split(guards_dist_xstateGuards.STATE_DELIMITER);
569
+ const fullPath = stateId.split(interpreter.STATE_DELIMITER);
519
570
  const relativePath = fullPath.slice(1);
520
571
  const resolvedStateId = guards_dist_xstateGuards.isStateId(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
521
572
  const stateNode = this.idMap.get(resolvedStateId);
@@ -553,12 +604,12 @@ class StateMachine {
553
604
  const actorData = state.children[actorId];
554
605
  const childState = actorData.state;
555
606
  const src = actorData.src;
556
- const logic = src ? guards_dist_xstateGuards.resolveReferencedActor(this.implementations.actors[src])?.src : undefined;
607
+ const logic = src ? interpreter.resolveReferencedActor(this.implementations.actors[src])?.src : undefined;
557
608
  if (!logic) {
558
609
  return;
559
610
  }
560
611
  const actorState = logic.restoreState?.(childState, _actorCtx);
561
- const actorRef = guards_dist_xstateGuards.createActor(logic, {
612
+ const actorRef = interpreter.createActor(logic, {
562
613
  id: actorId,
563
614
  state: actorState
564
615
  });
@@ -580,9 +631,9 @@ class StateMachine {
580
631
  if (children[id]) {
581
632
  return;
582
633
  }
583
- const referenced = guards_dist_xstateGuards.resolveReferencedActor(this.implementations.actors[src]);
634
+ const referenced = interpreter.resolveReferencedActor(this.implementations.actors[src]);
584
635
  if (referenced) {
585
- const actorRef = guards_dist_xstateGuards.createActor(referenced.src, {
636
+ const actorRef = interpreter.createActor(referenced.src, {
586
637
  id,
587
638
  parent: _actorCtx?.self,
588
639
  input: 'input' in invokeConfig ? invokeConfig.input : referenced.input
@@ -598,69 +649,6 @@ class StateMachine {
598
649
  /**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
599
650
  }
600
651
 
601
- function createMachine(config, implementations) {
602
- return new StateMachine(config, implementations);
603
- }
604
-
605
- function mapState(stateMap, stateId) {
606
- let foundStateId;
607
- for (const mappedStateId of Object.keys(stateMap)) {
608
- if (guards_dist_xstateGuards.matchesState(mappedStateId, stateId) && (!foundStateId || stateId.length > foundStateId.length)) {
609
- foundStateId = mappedStateId;
610
- }
611
- }
612
- return stateMap[foundStateId];
613
- }
614
-
615
- class SimulatedClock {
616
- constructor() {
617
- this.timeouts = new Map();
618
- this._now = 0;
619
- this._id = 0;
620
- }
621
- now() {
622
- return this._now;
623
- }
624
- getId() {
625
- return this._id++;
626
- }
627
- setTimeout(fn, timeout) {
628
- const id = this.getId();
629
- this.timeouts.set(id, {
630
- start: this.now(),
631
- timeout,
632
- fn
633
- });
634
- return id;
635
- }
636
- clearTimeout(id) {
637
- this.timeouts.delete(id);
638
- }
639
- set(time) {
640
- if (this._now > time) {
641
- throw new Error('Unable to travel back in time');
642
- }
643
- this._now = time;
644
- this.flushTimeouts();
645
- }
646
- flushTimeouts() {
647
- [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
648
- const endA = timeoutA.start + timeoutA.timeout;
649
- const endB = timeoutB.start + timeoutB.timeout;
650
- return endB > endA ? -1 : 1;
651
- }).forEach(([id, timeout]) => {
652
- if (this.now() - timeout.start >= timeout.timeout) {
653
- this.timeouts.delete(id);
654
- timeout.fn.call(null);
655
- }
656
- });
657
- }
658
- increment(ms) {
659
- this._now += ms;
660
- this.flushTimeouts();
661
- }
662
- }
663
-
664
652
  const defaultWaitForOptions = {
665
653
  timeout: 10_000 // 10 seconds
666
654
  };
@@ -732,38 +720,54 @@ function waitFor(actorRef, predicate, options) {
732
720
  });
733
721
  }
734
722
 
735
- exports.Actor = guards_dist_xstateGuards.Actor;
736
- exports.ActorStatus = guards_dist_xstateGuards.ActorStatus;
737
- exports.ConstantPrefix = guards_dist_xstateGuards.ConstantPrefix;
738
- exports.InterpreterStatus = guards_dist_xstateGuards.InterpreterStatus;
739
- exports.SpecialTargets = guards_dist_xstateGuards.SpecialTargets;
723
+ function createMachine(config, implementations) {
724
+ return new StateMachine(config, implementations);
725
+ }
726
+
727
+ function mapState(stateMap, stateId) {
728
+ let foundStateId;
729
+ for (const mappedStateId of Object.keys(stateMap)) {
730
+ if (interpreter.matchesState(mappedStateId, stateId) && (!foundStateId || stateId.length > foundStateId.length)) {
731
+ foundStateId = mappedStateId;
732
+ }
733
+ }
734
+ return stateMap[foundStateId];
735
+ }
736
+
737
+ exports.createEmptyActor = actors_dist_xstateActors.createEmptyActor;
738
+ exports.fromCallback = actors_dist_xstateActors.fromCallback;
739
+ exports.fromEventObservable = actors_dist_xstateActors.fromEventObservable;
740
+ exports.fromObservable = actors_dist_xstateActors.fromObservable;
741
+ exports.fromPromise = actors_dist_xstateActors.fromPromise;
742
+ exports.fromTransition = actors_dist_xstateActors.fromTransition;
743
+ exports.isActorRef = actors_dist_xstateActors.isActorRef;
744
+ exports.toActorRef = actors_dist_xstateActors.toActorRef;
745
+ exports.Actor = interpreter.Actor;
746
+ exports.ActorStatus = interpreter.ActorStatus;
747
+ exports.InterpreterStatus = interpreter.InterpreterStatus;
748
+ exports.createActor = interpreter.createActor;
749
+ exports.interpret = interpreter.interpret;
750
+ exports.matchesState = interpreter.matchesState;
751
+ exports.pathToStateValue = interpreter.pathToStateValue;
752
+ exports.toObserver = interpreter.toObserver;
740
753
  exports.State = guards_dist_xstateGuards.State;
741
754
  exports.and = guards_dist_xstateGuards.and;
742
- exports.assign = guards_dist_xstateGuards.assign;
743
755
  exports.cancel = guards_dist_xstateGuards.cancel;
744
- exports.choose = guards_dist_xstateGuards.choose;
745
- exports.createActor = guards_dist_xstateGuards.createActor;
746
- exports.doneInvoke = guards_dist_xstateGuards.doneInvoke;
747
- exports.forwardTo = guards_dist_xstateGuards.forwardTo;
748
756
  exports.getStateNodes = guards_dist_xstateGuards.getStateNodes;
749
- exports.interpret = guards_dist_xstateGuards.interpret;
750
- exports.log = guards_dist_xstateGuards.log;
751
- exports.matchesState = guards_dist_xstateGuards.matchesState;
752
757
  exports.not = guards_dist_xstateGuards.not;
753
758
  exports.or = guards_dist_xstateGuards.or;
754
- exports.pathToStateValue = guards_dist_xstateGuards.pathToStateValue;
755
- exports.pure = guards_dist_xstateGuards.pure;
756
759
  exports.raise = guards_dist_xstateGuards.raise;
757
- exports.sendParent = guards_dist_xstateGuards.sendParent;
758
- exports.sendTo = guards_dist_xstateGuards.sendTo;
759
760
  exports.stateIn = guards_dist_xstateGuards.stateIn;
760
761
  exports.stop = guards_dist_xstateGuards.stop;
761
- exports.toObserver = guards_dist_xstateGuards.toObserver;
762
- exports.fromCallback = promise.fromCallback;
763
- exports.fromEventObservable = promise.fromEventObservable;
764
- exports.fromObservable = promise.fromObservable;
765
- exports.fromPromise = promise.fromPromise;
766
- exports.fromTransition = promise.fromTransition;
762
+ exports.SpecialTargets = send.SpecialTargets;
763
+ exports.assign = send.assign;
764
+ exports.choose = send.choose;
765
+ exports.escalate = send.escalate;
766
+ exports.forwardTo = send.forwardTo;
767
+ exports.log = send.log;
768
+ exports.pure = send.pure;
769
+ exports.sendParent = send.sendParent;
770
+ exports.sendTo = send.sendTo;
767
771
  exports.SimulatedClock = SimulatedClock;
768
772
  exports.StateMachine = StateMachine;
769
773
  exports.StateNode = StateNode;
@@ -1,7 +1,6 @@
1
1
  export {
2
2
  Actor,
3
3
  ActorStatus,
4
- ConstantPrefix,
5
4
  InterpreterStatus,
6
5
  SimulatedClock,
7
6
  SpecialTargets,
@@ -13,8 +12,9 @@ export {
13
12
  cancel,
14
13
  choose,
15
14
  createActor,
15
+ createEmptyActor,
16
16
  createMachine,
17
- doneInvoke,
17
+ escalate,
18
18
  forwardTo,
19
19
  fromCallback,
20
20
  fromEventObservable,
@@ -23,6 +23,7 @@ export {
23
23
  fromTransition,
24
24
  getStateNodes,
25
25
  interpret,
26
+ isActorRef,
26
27
  log,
27
28
  mapState,
28
29
  matchesState,
@@ -35,6 +36,7 @@ export {
35
36
  sendTo,
36
37
  stateIn,
37
38
  stop,
39
+ toActorRef,
38
40
  toObserver,
39
41
  waitFor
40
42
  } from "./xstate.cjs.js";