xstate 5.4.0 → 5.5.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 (48) 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 +1 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/declarations/src/State.d.ts +43 -17
  13. package/dist/declarations/src/StateMachine.d.ts +16 -15
  14. package/dist/declarations/src/StateNode.d.ts +0 -4
  15. package/dist/declarations/src/actions/assign.d.ts +28 -1
  16. package/dist/declarations/src/actions/cancel.d.ts +23 -4
  17. package/dist/declarations/src/actions/enqueueActions.d.ts +20 -0
  18. package/dist/declarations/src/actions/send.d.ts +2 -2
  19. package/dist/declarations/src/{interpreter.d.ts → createActor.d.ts} +2 -4
  20. package/dist/declarations/src/createMachine.d.ts +40 -0
  21. package/dist/declarations/src/getNextSnapshot.d.ts +32 -0
  22. package/dist/declarations/src/guards.d.ts +86 -0
  23. package/dist/declarations/src/index.d.ts +2 -1
  24. package/dist/declarations/src/spawn.d.ts +2 -2
  25. package/dist/declarations/src/typegenTypes.d.ts +26 -14
  26. package/dist/declarations/src/types.d.ts +17 -36
  27. package/dist/{log-e870aec8.esm.js → log-22b3587f.esm.js} +67 -10
  28. package/dist/{log-580765a2.development.esm.js → log-285f62db.development.esm.js} +67 -10
  29. package/dist/{log-a32b44b3.cjs.js → log-742895c6.cjs.js} +67 -10
  30. package/dist/{log-cd3d7c14.development.cjs.js → log-da322832.development.cjs.js} +67 -10
  31. package/dist/{raise-7df513e7.esm.js → raise-0e64ee6e.esm.js} +125 -16
  32. package/dist/{raise-e3ff3de1.development.cjs.js → raise-7af39710.development.cjs.js} +125 -16
  33. package/dist/{raise-0fc3a80c.development.esm.js → raise-8da27ebb.development.esm.js} +125 -16
  34. package/dist/{raise-c3bbdd3a.cjs.js → raise-ad8bb7c2.cjs.js} +125 -16
  35. package/dist/xstate.cjs.js +105 -4
  36. package/dist/xstate.cjs.mjs +1 -0
  37. package/dist/xstate.development.cjs.js +105 -4
  38. package/dist/xstate.development.cjs.mjs +1 -0
  39. package/dist/xstate.development.esm.js +107 -7
  40. package/dist/xstate.esm.js +107 -7
  41. package/dist/xstate.umd.min.js +1 -1
  42. package/dist/xstate.umd.min.js.map +1 -1
  43. package/guards/dist/xstate-guards.cjs.js +1 -1
  44. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  45. package/guards/dist/xstate-guards.development.esm.js +1 -1
  46. package/guards/dist/xstate-guards.esm.js +1 -1
  47. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  48. package/package.json +1 -1
@@ -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-e3ff3de1.development.cjs.js');
7
- var log = require('./log-cd3d7c14.development.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-7af39710.development.cjs.js');
7
+ var log = require('./log-da322832.development.cjs.js');
8
8
  require('../dev/dist/xstate-dev.development.cjs.js');
9
9
 
10
10
  class SimulatedClock {
@@ -207,6 +207,8 @@ class StateNode {
207
207
  this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
208
208
  this.tags = guards_dist_xstateGuards.toArray(config.tags).slice();
209
209
  }
210
+
211
+ /** @internal */
210
212
  _initialize() {
211
213
  this.transitions = guards_dist_xstateGuards.formatTransitions(this);
212
214
  if (this.config.always) {
@@ -258,6 +260,8 @@ class StateNode {
258
260
  tags: this.tags
259
261
  };
260
262
  }
263
+
264
+ /** @internal */
261
265
  toJSON() {
262
266
  return this.definition;
263
267
  }
@@ -314,6 +318,8 @@ class StateNode {
314
318
  get initial() {
315
319
  return memo(this, 'initial', () => guards_dist_xstateGuards.formatInitialTransition(this, this.config.initial));
316
320
  }
321
+
322
+ /** @internal */
317
323
  next(snapshot, event) {
318
324
  const eventType = event.type;
319
325
  const actions = [];
@@ -389,13 +395,17 @@ class StateMachine {
389
395
  */
390
396
  this.version = void 0;
391
397
  this.implementations = void 0;
398
+ /** @internal */
392
399
  this.__xstatenode = true;
400
+ /** @internal */
393
401
  this.idMap = new Map();
394
402
  this.root = void 0;
395
403
  this.id = void 0;
396
404
  this.states = void 0;
397
405
  this.events = void 0;
398
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
406
+ /**
407
+ * @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
408
+ **/
399
409
  this.__TResolvedTypesMeta = void 0;
400
410
  this.id = config.id || '(machine)';
401
411
  this.implementations = {
@@ -584,7 +594,7 @@ class StateMachine {
584
594
  }
585
595
  const actorRef = guards_dist_xstateGuards.createActor(logic, {
586
596
  id: actorId,
587
- parent: _actorScope?.self,
597
+ parent: _actorScope.self,
588
598
  syncSnapshot: actorData.syncSnapshot,
589
599
  snapshot: childState,
590
600
  src,
@@ -696,10 +706,100 @@ function waitFor(actorRef, predicate, options) {
696
706
  // this is not 100% accurate since we can't make parallel regions required in the result
697
707
  // `TTestValue` doesn't encode this information anyhow for us to be able to do that
698
708
  // this is fine for most practical use cases anyway though
709
+ /**
710
+ * Creates a state machine (statechart) with the given configuration.
711
+ *
712
+ * The state machine represents the pure logic of a state machine actor.
713
+ *
714
+ * @param config The state machine configuration.
715
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
716
+ *
717
+ * @example
718
+ ```ts
719
+ import { createMachine } from 'xstate';
720
+
721
+ const lightMachine = createMachine({
722
+ id: 'light',
723
+ initial: 'green',
724
+ states: {
725
+ green: {
726
+ on: {
727
+ TIMER: { target: 'yellow' }
728
+ }
729
+ },
730
+ yellow: {
731
+ on: {
732
+ TIMER: { target: 'red' }
733
+ }
734
+ },
735
+ red: {
736
+ on: {
737
+ TIMER: { target: 'green' }
738
+ }
739
+ }
740
+ }
741
+ });
742
+
743
+ const lightActor = createActor(lightMachine);
744
+ lightActor.start();
745
+
746
+ lightActor.send({ type: 'TIMER' });
747
+ ```
748
+ */
699
749
  function createMachine(config, implementations) {
700
750
  return new StateMachine(config, implementations);
701
751
  }
702
752
 
753
+ /** @internal */
754
+ function createInertActorScope(actorLogic) {
755
+ const self = guards_dist_xstateGuards.createActor(actorLogic);
756
+ const inertActorScope = {
757
+ self,
758
+ defer: () => {},
759
+ id: '',
760
+ logger: () => {},
761
+ sessionId: '',
762
+ stopChild: () => {},
763
+ system: self.system
764
+ };
765
+ return inertActorScope;
766
+ }
767
+
768
+ /**
769
+ * Determines the next snapshot for the given `actorLogic` based on
770
+ * the given `snapshot` and `event`.
771
+ *
772
+ * If the `snapshot` is `undefined`, the initial snapshot of the
773
+ * `actorLogic` is used.
774
+ *
775
+ * @example
776
+ ```ts
777
+ import { getNextSnapshot } from 'xstate';
778
+ import { trafficLightMachine } from './trafficLightMachine.ts';
779
+
780
+ const nextSnapshot = getNextSnapshot(
781
+ trafficLightMachine, // actor logic
782
+ undefined, // snapshot (or initial state if undefined)
783
+ { type: 'TIMER' }); // event object
784
+
785
+ console.log(nextSnapshot.value);
786
+ // => 'yellow'
787
+
788
+ const nextSnapshot2 = getNextSnapshot(
789
+ trafficLightMachine, // actor logic
790
+ nextSnapshot, // snapshot
791
+ { type: 'TIMER' }); // event object
792
+
793
+ console.log(nextSnapshot2.value);
794
+ // =>'red'
795
+ ```
796
+ */
797
+ function getNextSnapshot(actorLogic, snapshot, event) {
798
+ const inertActorScope = createInertActorScope(actorLogic);
799
+ inertActorScope.self._snapshot = snapshot;
800
+ return actorLogic.transition(snapshot, event, inertActorScope);
801
+ }
802
+
703
803
  // at the moment we allow extra actors - ones that are not specified by `children`
704
804
  // this could be reconsidered in the future
705
805
  function setup({
@@ -817,6 +917,7 @@ exports.StateMachine = StateMachine;
817
917
  exports.StateNode = StateNode;
818
918
  exports.assertEvent = assertEvent;
819
919
  exports.createMachine = createMachine;
920
+ exports.getNextSnapshot = getNextSnapshot;
820
921
  exports.setup = setup;
821
922
  exports.toPromise = toPromise;
822
923
  exports.waitFor = waitFor;
@@ -19,6 +19,7 @@ export {
19
19
  fromObservable,
20
20
  fromPromise,
21
21
  fromTransition,
22
+ getNextSnapshot,
22
23
  getStateNodes,
23
24
  interpret,
24
25
  isMachineSnapshot,
@@ -1,8 +1,8 @@
1
1
  export { createEmptyActor, fromCallback, fromEventObservable, fromObservable, fromPromise, fromTransition } from '../actors/dist/xstate-actors.development.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 isStateId, w as getStateNodeByPath, x as getPersistedSnapshot, y as resolveReferencedActor, z as createActor, $ as $$ACTOR_TYPE } from './raise-0fc3a80c.development.esm.js';
3
- export { A as Actor, H as __unsafe_getAllOwnEventDescriptors, D as and, L as cancel, z as createActor, j as getStateNodes, B as interpret, C as isMachineSnapshot, I as matchesState, E as not, F as or, J as pathToStateValue, M as raise, O as spawnChild, G as stateIn, P as stop, Q as stopChild, K as toObserver } from './raise-0fc3a80c.development.esm.js';
4
- import { a as assign } from './log-580765a2.development.esm.js';
5
- export { S as SpecialTargets, a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from './log-580765a2.development.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 isStateId, w as getStateNodeByPath, x as getPersistedSnapshot, y as resolveReferencedActor, z as createActor, $ as $$ACTOR_TYPE } from './raise-8da27ebb.development.esm.js';
3
+ export { A as Actor, H as __unsafe_getAllOwnEventDescriptors, D as and, L as cancel, z as createActor, j as getStateNodes, B as interpret, C as isMachineSnapshot, I as matchesState, E as not, F as or, J as pathToStateValue, M as raise, O as spawnChild, G as stateIn, P as stop, Q as stopChild, K as toObserver } from './raise-8da27ebb.development.esm.js';
4
+ import { a as assign } from './log-285f62db.development.esm.js';
5
+ export { S as SpecialTargets, a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from './log-285f62db.development.esm.js';
6
6
  import '../dev/dist/xstate-dev.development.esm.js';
7
7
 
8
8
  class SimulatedClock {
@@ -205,6 +205,8 @@ class StateNode {
205
205
  this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
206
206
  this.tags = toArray(config.tags).slice();
207
207
  }
208
+
209
+ /** @internal */
208
210
  _initialize() {
209
211
  this.transitions = formatTransitions(this);
210
212
  if (this.config.always) {
@@ -256,6 +258,8 @@ class StateNode {
256
258
  tags: this.tags
257
259
  };
258
260
  }
261
+
262
+ /** @internal */
259
263
  toJSON() {
260
264
  return this.definition;
261
265
  }
@@ -312,6 +316,8 @@ class StateNode {
312
316
  get initial() {
313
317
  return memo(this, 'initial', () => formatInitialTransition(this, this.config.initial));
314
318
  }
319
+
320
+ /** @internal */
315
321
  next(snapshot, event) {
316
322
  const eventType = event.type;
317
323
  const actions = [];
@@ -387,13 +393,17 @@ class StateMachine {
387
393
  */
388
394
  this.version = void 0;
389
395
  this.implementations = void 0;
396
+ /** @internal */
390
397
  this.__xstatenode = true;
398
+ /** @internal */
391
399
  this.idMap = new Map();
392
400
  this.root = void 0;
393
401
  this.id = void 0;
394
402
  this.states = void 0;
395
403
  this.events = void 0;
396
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
404
+ /**
405
+ * @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
406
+ **/
397
407
  this.__TResolvedTypesMeta = void 0;
398
408
  this.id = config.id || '(machine)';
399
409
  this.implementations = {
@@ -582,7 +592,7 @@ class StateMachine {
582
592
  }
583
593
  const actorRef = createActor(logic, {
584
594
  id: actorId,
585
- parent: _actorScope?.self,
595
+ parent: _actorScope.self,
586
596
  syncSnapshot: actorData.syncSnapshot,
587
597
  snapshot: childState,
588
598
  src,
@@ -694,10 +704,100 @@ function waitFor(actorRef, predicate, options) {
694
704
  // this is not 100% accurate since we can't make parallel regions required in the result
695
705
  // `TTestValue` doesn't encode this information anyhow for us to be able to do that
696
706
  // this is fine for most practical use cases anyway though
707
+ /**
708
+ * Creates a state machine (statechart) with the given configuration.
709
+ *
710
+ * The state machine represents the pure logic of a state machine actor.
711
+ *
712
+ * @param config The state machine configuration.
713
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
714
+ *
715
+ * @example
716
+ ```ts
717
+ import { createMachine } from 'xstate';
718
+
719
+ const lightMachine = createMachine({
720
+ id: 'light',
721
+ initial: 'green',
722
+ states: {
723
+ green: {
724
+ on: {
725
+ TIMER: { target: 'yellow' }
726
+ }
727
+ },
728
+ yellow: {
729
+ on: {
730
+ TIMER: { target: 'red' }
731
+ }
732
+ },
733
+ red: {
734
+ on: {
735
+ TIMER: { target: 'green' }
736
+ }
737
+ }
738
+ }
739
+ });
740
+
741
+ const lightActor = createActor(lightMachine);
742
+ lightActor.start();
743
+
744
+ lightActor.send({ type: 'TIMER' });
745
+ ```
746
+ */
697
747
  function createMachine(config, implementations) {
698
748
  return new StateMachine(config, implementations);
699
749
  }
700
750
 
751
+ /** @internal */
752
+ function createInertActorScope(actorLogic) {
753
+ const self = createActor(actorLogic);
754
+ const inertActorScope = {
755
+ self,
756
+ defer: () => {},
757
+ id: '',
758
+ logger: () => {},
759
+ sessionId: '',
760
+ stopChild: () => {},
761
+ system: self.system
762
+ };
763
+ return inertActorScope;
764
+ }
765
+
766
+ /**
767
+ * Determines the next snapshot for the given `actorLogic` based on
768
+ * the given `snapshot` and `event`.
769
+ *
770
+ * If the `snapshot` is `undefined`, the initial snapshot of the
771
+ * `actorLogic` is used.
772
+ *
773
+ * @example
774
+ ```ts
775
+ import { getNextSnapshot } from 'xstate';
776
+ import { trafficLightMachine } from './trafficLightMachine.ts';
777
+
778
+ const nextSnapshot = getNextSnapshot(
779
+ trafficLightMachine, // actor logic
780
+ undefined, // snapshot (or initial state if undefined)
781
+ { type: 'TIMER' }); // event object
782
+
783
+ console.log(nextSnapshot.value);
784
+ // => 'yellow'
785
+
786
+ const nextSnapshot2 = getNextSnapshot(
787
+ trafficLightMachine, // actor logic
788
+ nextSnapshot, // snapshot
789
+ { type: 'TIMER' }); // event object
790
+
791
+ console.log(nextSnapshot2.value);
792
+ // =>'red'
793
+ ```
794
+ */
795
+ function getNextSnapshot(actorLogic, snapshot, event) {
796
+ const inertActorScope = createInertActorScope(actorLogic);
797
+ inertActorScope.self._snapshot = snapshot;
798
+ return actorLogic.transition(snapshot, event, inertActorScope);
799
+ }
800
+
701
801
  // at the moment we allow extra actors - ones that are not specified by `children`
702
802
  // this could be reconsidered in the future
703
803
  function setup({
@@ -779,4 +879,4 @@ function assertEvent(event, type) {
779
879
  }
780
880
  }
781
881
 
782
- export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, setup, toPromise, waitFor };
882
+ export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getNextSnapshot, setup, toPromise, waitFor };
@@ -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 isStateId, w as getStateNodeByPath, x as getPersistedSnapshot, y as resolveReferencedActor, z as createActor, $ as $$ACTOR_TYPE } from './raise-7df513e7.esm.js';
3
- export { A as Actor, H as __unsafe_getAllOwnEventDescriptors, D as and, L as cancel, z as createActor, j as getStateNodes, B as interpret, C as isMachineSnapshot, I as matchesState, E as not, F as or, J as pathToStateValue, M as raise, O as spawnChild, G as stateIn, P as stop, Q as stopChild, K as toObserver } from './raise-7df513e7.esm.js';
4
- import { a as assign } from './log-e870aec8.esm.js';
5
- export { S as SpecialTargets, a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from './log-e870aec8.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 isStateId, w as getStateNodeByPath, x as getPersistedSnapshot, y as resolveReferencedActor, z as createActor, $ as $$ACTOR_TYPE } from './raise-0e64ee6e.esm.js';
3
+ export { A as Actor, H as __unsafe_getAllOwnEventDescriptors, D as and, L as cancel, z as createActor, j as getStateNodes, B as interpret, C as isMachineSnapshot, I as matchesState, E as not, F as or, J as pathToStateValue, M as raise, O as spawnChild, G as stateIn, P as stop, Q as stopChild, K as toObserver } from './raise-0e64ee6e.esm.js';
4
+ import { a as assign } from './log-22b3587f.esm.js';
5
+ export { S as SpecialTargets, a as assign, e as enqueueActions, f as forwardTo, l as log, s as sendParent, b as sendTo } from './log-22b3587f.esm.js';
6
6
  import '../dev/dist/xstate-dev.esm.js';
7
7
 
8
8
  class SimulatedClock {
@@ -205,6 +205,8 @@ class StateNode {
205
205
  this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
206
206
  this.tags = toArray(config.tags).slice();
207
207
  }
208
+
209
+ /** @internal */
208
210
  _initialize() {
209
211
  this.transitions = formatTransitions(this);
210
212
  if (this.config.always) {
@@ -256,6 +258,8 @@ class StateNode {
256
258
  tags: this.tags
257
259
  };
258
260
  }
261
+
262
+ /** @internal */
259
263
  toJSON() {
260
264
  return this.definition;
261
265
  }
@@ -312,6 +316,8 @@ class StateNode {
312
316
  get initial() {
313
317
  return memo(this, 'initial', () => formatInitialTransition(this, this.config.initial));
314
318
  }
319
+
320
+ /** @internal */
315
321
  next(snapshot, event) {
316
322
  const eventType = event.type;
317
323
  const actions = [];
@@ -387,13 +393,17 @@ class StateMachine {
387
393
  */
388
394
  this.version = void 0;
389
395
  this.implementations = void 0;
396
+ /** @internal */
390
397
  this.__xstatenode = true;
398
+ /** @internal */
391
399
  this.idMap = new Map();
392
400
  this.root = void 0;
393
401
  this.id = void 0;
394
402
  this.states = void 0;
395
403
  this.events = void 0;
396
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
404
+ /**
405
+ * @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
406
+ **/
397
407
  this.__TResolvedTypesMeta = void 0;
398
408
  this.id = config.id || '(machine)';
399
409
  this.implementations = {
@@ -579,7 +589,7 @@ class StateMachine {
579
589
  }
580
590
  const actorRef = createActor(logic, {
581
591
  id: actorId,
582
- parent: _actorScope?.self,
592
+ parent: _actorScope.self,
583
593
  syncSnapshot: actorData.syncSnapshot,
584
594
  snapshot: childState,
585
595
  src,
@@ -688,10 +698,100 @@ function waitFor(actorRef, predicate, options) {
688
698
  // this is not 100% accurate since we can't make parallel regions required in the result
689
699
  // `TTestValue` doesn't encode this information anyhow for us to be able to do that
690
700
  // this is fine for most practical use cases anyway though
701
+ /**
702
+ * Creates a state machine (statechart) with the given configuration.
703
+ *
704
+ * The state machine represents the pure logic of a state machine actor.
705
+ *
706
+ * @param config The state machine configuration.
707
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
708
+ *
709
+ * @example
710
+ ```ts
711
+ import { createMachine } from 'xstate';
712
+
713
+ const lightMachine = createMachine({
714
+ id: 'light',
715
+ initial: 'green',
716
+ states: {
717
+ green: {
718
+ on: {
719
+ TIMER: { target: 'yellow' }
720
+ }
721
+ },
722
+ yellow: {
723
+ on: {
724
+ TIMER: { target: 'red' }
725
+ }
726
+ },
727
+ red: {
728
+ on: {
729
+ TIMER: { target: 'green' }
730
+ }
731
+ }
732
+ }
733
+ });
734
+
735
+ const lightActor = createActor(lightMachine);
736
+ lightActor.start();
737
+
738
+ lightActor.send({ type: 'TIMER' });
739
+ ```
740
+ */
691
741
  function createMachine(config, implementations) {
692
742
  return new StateMachine(config, implementations);
693
743
  }
694
744
 
745
+ /** @internal */
746
+ function createInertActorScope(actorLogic) {
747
+ const self = createActor(actorLogic);
748
+ const inertActorScope = {
749
+ self,
750
+ defer: () => {},
751
+ id: '',
752
+ logger: () => {},
753
+ sessionId: '',
754
+ stopChild: () => {},
755
+ system: self.system
756
+ };
757
+ return inertActorScope;
758
+ }
759
+
760
+ /**
761
+ * Determines the next snapshot for the given `actorLogic` based on
762
+ * the given `snapshot` and `event`.
763
+ *
764
+ * If the `snapshot` is `undefined`, the initial snapshot of the
765
+ * `actorLogic` is used.
766
+ *
767
+ * @example
768
+ ```ts
769
+ import { getNextSnapshot } from 'xstate';
770
+ import { trafficLightMachine } from './trafficLightMachine.ts';
771
+
772
+ const nextSnapshot = getNextSnapshot(
773
+ trafficLightMachine, // actor logic
774
+ undefined, // snapshot (or initial state if undefined)
775
+ { type: 'TIMER' }); // event object
776
+
777
+ console.log(nextSnapshot.value);
778
+ // => 'yellow'
779
+
780
+ const nextSnapshot2 = getNextSnapshot(
781
+ trafficLightMachine, // actor logic
782
+ nextSnapshot, // snapshot
783
+ { type: 'TIMER' }); // event object
784
+
785
+ console.log(nextSnapshot2.value);
786
+ // =>'red'
787
+ ```
788
+ */
789
+ function getNextSnapshot(actorLogic, snapshot, event) {
790
+ const inertActorScope = createInertActorScope(actorLogic);
791
+ inertActorScope.self._snapshot = snapshot;
792
+ return actorLogic.transition(snapshot, event, inertActorScope);
793
+ }
794
+
695
795
  // at the moment we allow extra actors - ones that are not specified by `children`
696
796
  // this could be reconsidered in the future
697
797
  function setup({
@@ -773,4 +873,4 @@ function assertEvent(event, type) {
773
873
  }
774
874
  }
775
875
 
776
- export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, setup, toPromise, waitFor };
876
+ export { SimulatedClock, StateMachine, StateNode, assertEvent, createMachine, getNextSnapshot, setup, toPromise, waitFor };