xstate 6.0.0-alpha.33 → 6.0.0-alpha.35
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.
- package/dist/{StateMachine-d3243bd1.development.cjs.js → StateMachine-41dc03be.development.cjs.js} +37 -139
- package/dist/{StateMachine-33eec203.development.esm.js → StateMachine-8dd1e4a0.development.esm.js} +29 -126
- package/dist/{StateMachine-d073209c.esm.js → StateMachine-9d68f525.esm.js} +29 -126
- package/dist/{StateMachine-ff580dd5.cjs.js → StateMachine-c6fc9d29.cjs.js} +37 -139
- package/dist/declarations/src/StateMachine.d.ts +2 -2
- package/dist/declarations/src/createActor.d.ts +5 -0
- package/dist/declarations/src/stateUtils.d.ts +4 -4
- package/dist/declarations/src/types.d.ts +1 -1
- package/dist/{index-691f27b9.development.cjs.js → index-0acba787.development.cjs.js} +687 -298
- package/dist/{index-ab93831d.esm.js → index-9146dea1.esm.js} +679 -297
- package/dist/{index-9939f02a.cjs.js → index-9683e24d.cjs.js} +687 -298
- package/dist/{index-92227865.development.esm.js → index-9d364b4a.development.esm.js} +679 -297
- package/dist/xstate-actors.cjs.js +1 -1
- package/dist/xstate-actors.development.cjs.js +1 -1
- package/dist/xstate-actors.development.esm.js +1 -1
- package/dist/xstate-actors.esm.js +1 -1
- package/dist/xstate-actors.umd.min.js +1 -1
- package/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/xstate-graph.cjs.js +2 -2
- package/dist/xstate-graph.development.cjs.js +2 -2
- package/dist/xstate-graph.development.esm.js +2 -2
- package/dist/xstate-graph.esm.js +2 -2
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate.cjs.js +34 -33
- package/dist/xstate.development.cjs.js +34 -33
- package/dist/xstate.development.esm.js +26 -25
- package/dist/xstate.esm.js +26 -25
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/package.json +1 -1
package/dist/{StateMachine-d3243bd1.development.cjs.js → StateMachine-41dc03be.development.cjs.js}
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var dist_xstateActors = require('./index-
|
|
3
|
+
var dist_xstateActors = require('./index-0acba787.development.cjs.js');
|
|
4
4
|
|
|
5
5
|
function createSpawner(actorScope, actors, spawnedChildren) {
|
|
6
6
|
return (src, options) => {
|
|
@@ -18,111 +18,6 @@ function createSpawner(actorScope, actors, spawnedChildren) {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
/** @internal */
|
|
22
|
-
function setInertActorScopeSnapshot(actorScope, snapshot, attachActorRef = true) {
|
|
23
|
-
actorScope.self._snapshot = snapshot;
|
|
24
|
-
if (attachActorRef && snapshot && typeof snapshot === 'object') {
|
|
25
|
-
dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self);
|
|
26
|
-
}
|
|
27
|
-
return snapshot;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** @internal */
|
|
31
|
-
function isInertActorScope(actorScope) {
|
|
32
|
-
return !!actorScope.self.options?._inert;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** @internal */
|
|
36
|
-
function attachSnapshotActorRef(actorLogic, actorScope, snapshot) {
|
|
37
|
-
const snapshotScope = createInertActorScope(actorLogic, snapshot, actorScope.self);
|
|
38
|
-
return setInertActorScopeSnapshot(snapshotScope, snapshot);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** @internal */
|
|
42
|
-
function createInertActorScope(actorLogic, snapshot, sourceSelf) {
|
|
43
|
-
const snapshotRef = snapshot ? dist_xstateActors.getSnapshotActorRef(snapshot) : undefined;
|
|
44
|
-
const previousSelf = sourceSelf ?? snapshotRef?.actor;
|
|
45
|
-
const baseSystem = previousSelf?.system;
|
|
46
|
-
const system = previousSelf && baseSystem ? dist_xstateActors.createSnapshotSystem(baseSystem, dist_xstateActors.isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
|
|
47
|
-
const self = dist_xstateActors.createActor(actorLogic, {
|
|
48
|
-
_inert: true,
|
|
49
|
-
...(previousSelf ? {
|
|
50
|
-
id: previousSelf.id,
|
|
51
|
-
_sessionId: previousSelf.sessionId
|
|
52
|
-
} : {}),
|
|
53
|
-
...(system ? {
|
|
54
|
-
_systemRef: {
|
|
55
|
-
current: system
|
|
56
|
-
}
|
|
57
|
-
} : {})
|
|
58
|
-
});
|
|
59
|
-
if (previousSelf?._parent) {
|
|
60
|
-
self._parent = previousSelf._parent;
|
|
61
|
-
}
|
|
62
|
-
if (snapshot) {
|
|
63
|
-
self._snapshot = snapshot;
|
|
64
|
-
}
|
|
65
|
-
const actorScope = {
|
|
66
|
-
self: self,
|
|
67
|
-
defer: () => {},
|
|
68
|
-
id: self.id,
|
|
69
|
-
logger: self.system._logger,
|
|
70
|
-
sessionId: self.sessionId,
|
|
71
|
-
stopChild: child => child._stop(),
|
|
72
|
-
system: self.system,
|
|
73
|
-
emit: event => self.system.emitEvent(self, event),
|
|
74
|
-
actionExecutor: () => {}
|
|
75
|
-
};
|
|
76
|
-
return actorScope;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** @deprecated Use `initialTransition(…)` instead. */
|
|
80
|
-
function getInitialSnapshot(actorLogic, ...[input]) {
|
|
81
|
-
const actorScope = createInertActorScope(actorLogic);
|
|
82
|
-
return actorLogic.initialTransition(input, actorScope)[0];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Determines the next snapshot for the given `actorLogic` based on the given
|
|
87
|
-
* `snapshot` and `event`.
|
|
88
|
-
*
|
|
89
|
-
* If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
|
|
90
|
-
* used.
|
|
91
|
-
*
|
|
92
|
-
* @deprecated Use `transition(…)` instead.
|
|
93
|
-
* @example
|
|
94
|
-
*
|
|
95
|
-
* ```ts
|
|
96
|
-
* import { getNextSnapshot } from 'xstate';
|
|
97
|
-
* import { trafficLightMachine } from './trafficLightMachine.ts';
|
|
98
|
-
*
|
|
99
|
-
* const nextSnapshot = getNextSnapshot(
|
|
100
|
-
* trafficLightMachine, // actor logic
|
|
101
|
-
* undefined, // snapshot (or initial state if undefined)
|
|
102
|
-
* { type: 'TIMER' }
|
|
103
|
-
* ); // event object
|
|
104
|
-
*
|
|
105
|
-
* console.log(nextSnapshot.value);
|
|
106
|
-
* // => 'yellow'
|
|
107
|
-
*
|
|
108
|
-
* const nextSnapshot2 = getNextSnapshot(
|
|
109
|
-
* trafficLightMachine, // actor logic
|
|
110
|
-
* nextSnapshot, // snapshot
|
|
111
|
-
* { type: 'TIMER' }
|
|
112
|
-
* ); // event object
|
|
113
|
-
*
|
|
114
|
-
* console.log(nextSnapshot2.value);
|
|
115
|
-
* // =>'red'
|
|
116
|
-
* ```
|
|
117
|
-
*/
|
|
118
|
-
function getNextSnapshot(actorLogic, snapshot, event) {
|
|
119
|
-
const actorScope = createInertActorScope(actorLogic, snapshot);
|
|
120
|
-
const transitionResult = actorLogic.transition(snapshot, event, actorScope);
|
|
121
|
-
const nextSnapshot = Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
|
|
122
|
-
setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
|
|
123
|
-
return nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(actorLogic, actorScope, nextSnapshot);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
21
|
const cache = new WeakMap();
|
|
127
22
|
function memo(object, key, fn) {
|
|
128
23
|
let memoizedData = cache.get(object);
|
|
@@ -306,7 +201,7 @@ class StateNode {
|
|
|
306
201
|
}
|
|
307
202
|
|
|
308
203
|
/** @internal */
|
|
309
|
-
next(snapshot, event,
|
|
204
|
+
next(snapshot, event, actorScope, selectionResults) {
|
|
310
205
|
const descriptorKey = dist_xstateActors.getEventDescriptorKey(event);
|
|
311
206
|
let candidates = this._candidateCache?.get(descriptorKey);
|
|
312
207
|
if (!candidates) {
|
|
@@ -314,7 +209,7 @@ class StateNode {
|
|
|
314
209
|
(this._candidateCache ??= new Map()).set(descriptorKey, candidates);
|
|
315
210
|
}
|
|
316
211
|
for (const candidate of candidates) {
|
|
317
|
-
const guardPassed = dist_xstateActors.evaluateCandidate(candidate, event, snapshot, this,
|
|
212
|
+
const guardPassed = dist_xstateActors.evaluateCandidate(candidate, event, snapshot, this, actorScope, selectionResults);
|
|
318
213
|
if (guardPassed) {
|
|
319
214
|
return [candidate];
|
|
320
215
|
}
|
|
@@ -686,7 +581,7 @@ class StateMachine {
|
|
|
686
581
|
*/
|
|
687
582
|
transition(snapshot, event, actorScope) {
|
|
688
583
|
const usesInertScope = !actorScope;
|
|
689
|
-
const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
|
|
584
|
+
const resolvedActorScope = actorScope ?? dist_xstateActors.createInertActorScope(this, snapshot);
|
|
690
585
|
if (this.validator) {
|
|
691
586
|
dist_xstateActors.assertValid(this.validator, {
|
|
692
587
|
kind: 'event',
|
|
@@ -696,14 +591,14 @@ class StateMachine {
|
|
|
696
591
|
});
|
|
697
592
|
}
|
|
698
593
|
if (usesInertScope) {
|
|
699
|
-
setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
|
|
594
|
+
dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
|
|
700
595
|
}
|
|
701
596
|
const fastSnapshot = this._transitionFast(snapshot, event, resolvedActorScope);
|
|
702
597
|
if (fastSnapshot) {
|
|
703
598
|
if (usesInertScope) {
|
|
704
|
-
setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
|
|
599
|
+
dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
|
|
705
600
|
}
|
|
706
|
-
const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(
|
|
601
|
+
const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
|
|
707
602
|
if (this.validator) {
|
|
708
603
|
dist_xstateActors.assertValid(this.validator, {
|
|
709
604
|
kind: 'result',
|
|
@@ -719,9 +614,9 @@ class StateMachine {
|
|
|
719
614
|
microsteps
|
|
720
615
|
} = dist_xstateActors.macrostep(snapshot, event, resolvedActorScope, []);
|
|
721
616
|
if (usesInertScope) {
|
|
722
|
-
setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
|
|
617
|
+
dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
|
|
723
618
|
}
|
|
724
|
-
const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(
|
|
619
|
+
const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
|
|
725
620
|
const effects = this._collectEffects(microsteps);
|
|
726
621
|
if (this.validator) {
|
|
727
622
|
dist_xstateActors.assertValid(this.validator, {
|
|
@@ -736,8 +631,11 @@ class StateMachine {
|
|
|
736
631
|
_collectEffects(microsteps) {
|
|
737
632
|
return microsteps.flatMap(([, actions]) => actions);
|
|
738
633
|
}
|
|
739
|
-
_attachPureActorRef(snapshot, actorScope) {
|
|
740
|
-
if (isInertActorScope(actorScope)) {
|
|
634
|
+
_attachPureActorRef(snapshot, actorScope, skipInitializingActor = false) {
|
|
635
|
+
if (dist_xstateActors.isInertActorScope(actorScope)) {
|
|
636
|
+
return snapshot;
|
|
637
|
+
}
|
|
638
|
+
if (skipInitializingActor && actorScope.self._actorScope === actorScope && actorScope.self._snapshot === undefined) {
|
|
741
639
|
return snapshot;
|
|
742
640
|
}
|
|
743
641
|
dist_xstateActors.setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
|
|
@@ -768,9 +666,11 @@ class StateMachine {
|
|
|
768
666
|
...snapshot.context,
|
|
769
667
|
...selected.context
|
|
770
668
|
} : snapshot.context;
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
669
|
+
if (!dist_xstateActors.isInertActorScope(actorScope) && (actorScope.system._hasInspectionObservers?.() ?? true)) {
|
|
670
|
+
const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
|
|
671
|
+
collectedMicrosteps.push(selected);
|
|
672
|
+
actorScope.self._collectedMicrosteps = collectedMicrosteps;
|
|
673
|
+
}
|
|
774
674
|
return dist_xstateActors.cloneMachineSnapshot(snapshot, {
|
|
775
675
|
...(context !== snapshot.context ? {
|
|
776
676
|
context
|
|
@@ -798,8 +698,8 @@ class StateMachine {
|
|
|
798
698
|
}
|
|
799
699
|
return snapshots;
|
|
800
700
|
}
|
|
801
|
-
getTransitionData(snapshot, event,
|
|
802
|
-
return dist_xstateActors.transitionNode(this.root, snapshot.value, snapshot, event,
|
|
701
|
+
getTransitionData(snapshot, event, actorScope, selectionResults) {
|
|
702
|
+
return dist_xstateActors.transitionNode(this.root, snapshot.value, snapshot, event, actorScope, selectionResults) || [];
|
|
803
703
|
}
|
|
804
704
|
isInternalEventType(eventType) {
|
|
805
705
|
if (eventType === dist_xstateActors.XSTATE_TIMER) {
|
|
@@ -821,9 +721,8 @@ class StateMachine {
|
|
|
821
721
|
* @internal
|
|
822
722
|
*/
|
|
823
723
|
_canTransition(snapshot, event) {
|
|
824
|
-
const emptyActor = getEmptyCanActor();
|
|
825
724
|
const emptyActorScope = getEmptyCanActorScope();
|
|
826
|
-
const transitionData = this.getTransitionData(snapshot, event,
|
|
725
|
+
const transitionData = this.getTransitionData(snapshot, event, emptyActorScope);
|
|
827
726
|
if (!transitionData?.length) {
|
|
828
727
|
return false;
|
|
829
728
|
}
|
|
@@ -836,7 +735,7 @@ class StateMachine {
|
|
|
836
735
|
const res = dist_xstateActors.getTransitionResult(transition, snapshot, event, emptyActorScope, {
|
|
837
736
|
resolveActions: false
|
|
838
737
|
});
|
|
839
|
-
if (res.targets?.length || res.context || dist_xstateActors.hasEffect(transition, snapshot.context, event, snapshot,
|
|
738
|
+
if (res.targets?.length || res.context || dist_xstateActors.hasEffect(transition, snapshot.context, event, snapshot, emptyActorScope)) {
|
|
840
739
|
return true;
|
|
841
740
|
}
|
|
842
741
|
}
|
|
@@ -875,12 +774,11 @@ class StateMachine {
|
|
|
875
774
|
if (typeof context === 'function') {
|
|
876
775
|
const children = {};
|
|
877
776
|
const spawn = createSpawner(actorScope, this.sources.actors, children);
|
|
878
|
-
const resolvedContext = context({
|
|
777
|
+
const resolvedContext = context(dist_xstateActors.withActorSelf({
|
|
879
778
|
spawn,
|
|
880
779
|
input: initEvent.input,
|
|
881
|
-
self: actorScope.self,
|
|
882
780
|
actors: this.sources.actors
|
|
883
|
-
});
|
|
781
|
+
}, actorScope));
|
|
884
782
|
const [nextState] = dist_xstateActors.resolveActionsWithContext(preInitial, initEvent, actorScope, []);
|
|
885
783
|
if (resolvedContext) {
|
|
886
784
|
nextState.context = resolvedContext;
|
|
@@ -912,16 +810,16 @@ class StateMachine {
|
|
|
912
810
|
});
|
|
913
811
|
}
|
|
914
812
|
const usesInertScope = !actorScope;
|
|
915
|
-
const resolvedActorScope = actorScope ?? createInertActorScope(this);
|
|
813
|
+
const resolvedActorScope = actorScope ?? dist_xstateActors.createInertActorScope(this);
|
|
916
814
|
const initEvent = dist_xstateActors.createInitEvent(input); // TODO: fix;
|
|
917
815
|
const internalQueue = [];
|
|
918
816
|
const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
|
|
919
817
|
const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
|
|
920
818
|
const finalizeInitialResult = (macroState, microsteps) => {
|
|
921
819
|
if (usesInertScope) {
|
|
922
|
-
setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
|
|
820
|
+
dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
|
|
923
821
|
}
|
|
924
|
-
const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(
|
|
822
|
+
const returnedSnapshot = usesInertScope ? dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
|
|
925
823
|
const effects = this._collectEffects(microsteps);
|
|
926
824
|
if (this.validator) {
|
|
927
825
|
dist_xstateActors.assertValid(this.validator, {
|
|
@@ -956,7 +854,12 @@ class StateMachine {
|
|
|
956
854
|
if (!snapshot?.children) {
|
|
957
855
|
return;
|
|
958
856
|
}
|
|
959
|
-
|
|
857
|
+
const children = snapshot.children;
|
|
858
|
+
for (const childId in children) {
|
|
859
|
+
if (!Object.hasOwn(children, childId)) {
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
const child = children[childId];
|
|
960
863
|
if (child._rehydrated && child.getSnapshot?.().status === 'active') {
|
|
961
864
|
child.start();
|
|
962
865
|
}
|
|
@@ -977,7 +880,7 @@ class StateMachine {
|
|
|
977
880
|
}
|
|
978
881
|
restoreSnapshot(snapshot, actorScope) {
|
|
979
882
|
const usesInertScope = !actorScope;
|
|
980
|
-
const resolvedActorScope = actorScope ?? createInertActorScope(this);
|
|
883
|
+
const resolvedActorScope = actorScope ?? dist_xstateActors.createInertActorScope(this);
|
|
981
884
|
const persistedMachine = snapshot.machine;
|
|
982
885
|
const legacyPersistedVersion = snapshot.version;
|
|
983
886
|
const persistedVersion = typeof persistedMachine?.version === 'string' ? persistedMachine.version : legacyPersistedVersion;
|
|
@@ -1104,8 +1007,8 @@ class StateMachine {
|
|
|
1104
1007
|
}
|
|
1105
1008
|
reviveContext(restoredSnapshot.context);
|
|
1106
1009
|
if (usesInertScope) {
|
|
1107
|
-
setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
|
|
1108
|
-
return attachSnapshotActorRef(
|
|
1010
|
+
dist_xstateActors.setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
|
|
1011
|
+
return dist_xstateActors.attachSnapshotActorRef(resolvedActorScope, restoredSnapshot);
|
|
1109
1012
|
}
|
|
1110
1013
|
return this._attachPureActorRef(restoredSnapshot, resolvedActorScope);
|
|
1111
1014
|
}
|
|
@@ -1113,8 +1016,3 @@ class StateMachine {
|
|
|
1113
1016
|
|
|
1114
1017
|
exports.StateMachine = StateMachine;
|
|
1115
1018
|
exports.StateNode = StateNode;
|
|
1116
|
-
exports.attachSnapshotActorRef = attachSnapshotActorRef;
|
|
1117
|
-
exports.createInertActorScope = createInertActorScope;
|
|
1118
|
-
exports.getInitialSnapshot = getInitialSnapshot;
|
|
1119
|
-
exports.getNextSnapshot = getNextSnapshot;
|
|
1120
|
-
exports.setInertActorScopeSnapshot = setInertActorScopeSnapshot;
|
package/dist/{StateMachine-33eec203.development.esm.js → StateMachine-8dd1e4a0.development.esm.js}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { U as STATE_DELIMITER, V as mapValues, t as toArray, W as createInvokeId, Y as getDelayedTransitions, Z as getEventDescriptorKey, _ as getCandidates, $ as evaluateCandidate, a0 as formatTransition, a1 as NULL_EVENT, a2 as toTransitionConfigArray, a3 as createInvokeTimeoutEventId, a4 as matchesActorSession, a5 as formatRouteTransitions, a6 as resolveStateValue, a7 as getAllStateNodes, B as getStateNodes, a8 as createMachineSnapshot, a9 as isInFinalState, k as createInertActorScope, aa as assertValid, s as setInertActorScopeSnapshot, l as attachSnapshotActorRef, n as macrostep, ab as isInertActorScope, ac as setSnapshotActorRef, ad as cloneMachineSnapshot, ae as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, af as getTransitionResult, ag as hasEffect, ah as createErrorPlatformEvent, ai as withActorSelf, r as resolveActionsWithContext, o as createInitEvent, q as createSpawnEffect, u as initialMicrostep, aj as toStatePath, ak as isStateId, al as getStateNodeByPath, am as getPersistedSnapshot, an as resolveReferencedActor, ao as createInvokeTimeoutEvent, ap as $$ACTOR_TYPE, j as createActor } from './index-9d364b4a.development.esm.js';
|
|
2
2
|
|
|
3
3
|
function createSpawner(actorScope, actors, spawnedChildren) {
|
|
4
4
|
return (src, options) => {
|
|
@@ -16,111 +16,6 @@ function createSpawner(actorScope, actors, spawnedChildren) {
|
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
/** @internal */
|
|
20
|
-
function setInertActorScopeSnapshot(actorScope, snapshot, attachActorRef = true) {
|
|
21
|
-
actorScope.self._snapshot = snapshot;
|
|
22
|
-
if (attachActorRef && snapshot && typeof snapshot === 'object') {
|
|
23
|
-
setSnapshotActorRef(snapshot, actorScope.self);
|
|
24
|
-
}
|
|
25
|
-
return snapshot;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** @internal */
|
|
29
|
-
function isInertActorScope(actorScope) {
|
|
30
|
-
return !!actorScope.self.options?._inert;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** @internal */
|
|
34
|
-
function attachSnapshotActorRef(actorLogic, actorScope, snapshot) {
|
|
35
|
-
const snapshotScope = createInertActorScope(actorLogic, snapshot, actorScope.self);
|
|
36
|
-
return setInertActorScopeSnapshot(snapshotScope, snapshot);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** @internal */
|
|
40
|
-
function createInertActorScope(actorLogic, snapshot, sourceSelf) {
|
|
41
|
-
const snapshotRef = snapshot ? getSnapshotActorRef(snapshot) : undefined;
|
|
42
|
-
const previousSelf = sourceSelf ?? snapshotRef?.actor;
|
|
43
|
-
const baseSystem = previousSelf?.system;
|
|
44
|
-
const system = previousSelf && baseSystem ? createSnapshotSystem(baseSystem, isMachineSnapshot(snapshot) ? snapshot.children : {}, sourceSelf ? undefined : snapshotRef?.systemState) : undefined;
|
|
45
|
-
const self = createActor(actorLogic, {
|
|
46
|
-
_inert: true,
|
|
47
|
-
...(previousSelf ? {
|
|
48
|
-
id: previousSelf.id,
|
|
49
|
-
_sessionId: previousSelf.sessionId
|
|
50
|
-
} : {}),
|
|
51
|
-
...(system ? {
|
|
52
|
-
_systemRef: {
|
|
53
|
-
current: system
|
|
54
|
-
}
|
|
55
|
-
} : {})
|
|
56
|
-
});
|
|
57
|
-
if (previousSelf?._parent) {
|
|
58
|
-
self._parent = previousSelf._parent;
|
|
59
|
-
}
|
|
60
|
-
if (snapshot) {
|
|
61
|
-
self._snapshot = snapshot;
|
|
62
|
-
}
|
|
63
|
-
const actorScope = {
|
|
64
|
-
self: self,
|
|
65
|
-
defer: () => {},
|
|
66
|
-
id: self.id,
|
|
67
|
-
logger: self.system._logger,
|
|
68
|
-
sessionId: self.sessionId,
|
|
69
|
-
stopChild: child => child._stop(),
|
|
70
|
-
system: self.system,
|
|
71
|
-
emit: event => self.system.emitEvent(self, event),
|
|
72
|
-
actionExecutor: () => {}
|
|
73
|
-
};
|
|
74
|
-
return actorScope;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** @deprecated Use `initialTransition(…)` instead. */
|
|
78
|
-
function getInitialSnapshot(actorLogic, ...[input]) {
|
|
79
|
-
const actorScope = createInertActorScope(actorLogic);
|
|
80
|
-
return actorLogic.initialTransition(input, actorScope)[0];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Determines the next snapshot for the given `actorLogic` based on the given
|
|
85
|
-
* `snapshot` and `event`.
|
|
86
|
-
*
|
|
87
|
-
* If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
|
|
88
|
-
* used.
|
|
89
|
-
*
|
|
90
|
-
* @deprecated Use `transition(…)` instead.
|
|
91
|
-
* @example
|
|
92
|
-
*
|
|
93
|
-
* ```ts
|
|
94
|
-
* import { getNextSnapshot } from 'xstate';
|
|
95
|
-
* import { trafficLightMachine } from './trafficLightMachine.ts';
|
|
96
|
-
*
|
|
97
|
-
* const nextSnapshot = getNextSnapshot(
|
|
98
|
-
* trafficLightMachine, // actor logic
|
|
99
|
-
* undefined, // snapshot (or initial state if undefined)
|
|
100
|
-
* { type: 'TIMER' }
|
|
101
|
-
* ); // event object
|
|
102
|
-
*
|
|
103
|
-
* console.log(nextSnapshot.value);
|
|
104
|
-
* // => 'yellow'
|
|
105
|
-
*
|
|
106
|
-
* const nextSnapshot2 = getNextSnapshot(
|
|
107
|
-
* trafficLightMachine, // actor logic
|
|
108
|
-
* nextSnapshot, // snapshot
|
|
109
|
-
* { type: 'TIMER' }
|
|
110
|
-
* ); // event object
|
|
111
|
-
*
|
|
112
|
-
* console.log(nextSnapshot2.value);
|
|
113
|
-
* // =>'red'
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
function getNextSnapshot(actorLogic, snapshot, event) {
|
|
117
|
-
const actorScope = createInertActorScope(actorLogic, snapshot);
|
|
118
|
-
const transitionResult = actorLogic.transition(snapshot, event, actorScope);
|
|
119
|
-
const nextSnapshot = Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
|
|
120
|
-
setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
|
|
121
|
-
return nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(actorLogic, actorScope, nextSnapshot);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
19
|
const cache = new WeakMap();
|
|
125
20
|
function memo(object, key, fn) {
|
|
126
21
|
let memoizedData = cache.get(object);
|
|
@@ -304,7 +199,7 @@ class StateNode {
|
|
|
304
199
|
}
|
|
305
200
|
|
|
306
201
|
/** @internal */
|
|
307
|
-
next(snapshot, event,
|
|
202
|
+
next(snapshot, event, actorScope, selectionResults) {
|
|
308
203
|
const descriptorKey = getEventDescriptorKey(event);
|
|
309
204
|
let candidates = this._candidateCache?.get(descriptorKey);
|
|
310
205
|
if (!candidates) {
|
|
@@ -312,7 +207,7 @@ class StateNode {
|
|
|
312
207
|
(this._candidateCache ??= new Map()).set(descriptorKey, candidates);
|
|
313
208
|
}
|
|
314
209
|
for (const candidate of candidates) {
|
|
315
|
-
const guardPassed = evaluateCandidate(candidate, event, snapshot, this,
|
|
210
|
+
const guardPassed = evaluateCandidate(candidate, event, snapshot, this, actorScope, selectionResults);
|
|
316
211
|
if (guardPassed) {
|
|
317
212
|
return [candidate];
|
|
318
213
|
}
|
|
@@ -701,7 +596,7 @@ class StateMachine {
|
|
|
701
596
|
if (usesInertScope) {
|
|
702
597
|
setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
|
|
703
598
|
}
|
|
704
|
-
const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(
|
|
599
|
+
const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
|
|
705
600
|
if (this.validator) {
|
|
706
601
|
assertValid(this.validator, {
|
|
707
602
|
kind: 'result',
|
|
@@ -719,7 +614,7 @@ class StateMachine {
|
|
|
719
614
|
if (usesInertScope) {
|
|
720
615
|
setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
|
|
721
616
|
}
|
|
722
|
-
const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(
|
|
617
|
+
const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
|
|
723
618
|
const effects = this._collectEffects(microsteps);
|
|
724
619
|
if (this.validator) {
|
|
725
620
|
assertValid(this.validator, {
|
|
@@ -734,10 +629,13 @@ class StateMachine {
|
|
|
734
629
|
_collectEffects(microsteps) {
|
|
735
630
|
return microsteps.flatMap(([, actions]) => actions);
|
|
736
631
|
}
|
|
737
|
-
_attachPureActorRef(snapshot, actorScope) {
|
|
632
|
+
_attachPureActorRef(snapshot, actorScope, skipInitializingActor = false) {
|
|
738
633
|
if (isInertActorScope(actorScope)) {
|
|
739
634
|
return snapshot;
|
|
740
635
|
}
|
|
636
|
+
if (skipInitializingActor && actorScope.self._actorScope === actorScope && actorScope.self._snapshot === undefined) {
|
|
637
|
+
return snapshot;
|
|
638
|
+
}
|
|
741
639
|
setSnapshotActorRef(snapshot, actorScope.self, actorScope.system);
|
|
742
640
|
return snapshot;
|
|
743
641
|
}
|
|
@@ -766,9 +664,11 @@ class StateMachine {
|
|
|
766
664
|
...snapshot.context,
|
|
767
665
|
...selected.context
|
|
768
666
|
} : snapshot.context;
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
667
|
+
if (!isInertActorScope(actorScope) && (actorScope.system._hasInspectionObservers?.() ?? true)) {
|
|
668
|
+
const collectedMicrosteps = actorScope.self._collectedMicrosteps || [];
|
|
669
|
+
collectedMicrosteps.push(selected);
|
|
670
|
+
actorScope.self._collectedMicrosteps = collectedMicrosteps;
|
|
671
|
+
}
|
|
772
672
|
return cloneMachineSnapshot(snapshot, {
|
|
773
673
|
...(context !== snapshot.context ? {
|
|
774
674
|
context
|
|
@@ -796,8 +696,8 @@ class StateMachine {
|
|
|
796
696
|
}
|
|
797
697
|
return snapshots;
|
|
798
698
|
}
|
|
799
|
-
getTransitionData(snapshot, event,
|
|
800
|
-
return transitionNode(this.root, snapshot.value, snapshot, event,
|
|
699
|
+
getTransitionData(snapshot, event, actorScope, selectionResults) {
|
|
700
|
+
return transitionNode(this.root, snapshot.value, snapshot, event, actorScope, selectionResults) || [];
|
|
801
701
|
}
|
|
802
702
|
isInternalEventType(eventType) {
|
|
803
703
|
if (eventType === XSTATE_TIMER) {
|
|
@@ -819,9 +719,8 @@ class StateMachine {
|
|
|
819
719
|
* @internal
|
|
820
720
|
*/
|
|
821
721
|
_canTransition(snapshot, event) {
|
|
822
|
-
const emptyActor = getEmptyCanActor();
|
|
823
722
|
const emptyActorScope = getEmptyCanActorScope();
|
|
824
|
-
const transitionData = this.getTransitionData(snapshot, event,
|
|
723
|
+
const transitionData = this.getTransitionData(snapshot, event, emptyActorScope);
|
|
825
724
|
if (!transitionData?.length) {
|
|
826
725
|
return false;
|
|
827
726
|
}
|
|
@@ -834,7 +733,7 @@ class StateMachine {
|
|
|
834
733
|
const res = getTransitionResult(transition, snapshot, event, emptyActorScope, {
|
|
835
734
|
resolveActions: false
|
|
836
735
|
});
|
|
837
|
-
if (res.targets?.length || res.context || hasEffect(transition, snapshot.context, event, snapshot,
|
|
736
|
+
if (res.targets?.length || res.context || hasEffect(transition, snapshot.context, event, snapshot, emptyActorScope)) {
|
|
838
737
|
return true;
|
|
839
738
|
}
|
|
840
739
|
}
|
|
@@ -873,12 +772,11 @@ class StateMachine {
|
|
|
873
772
|
if (typeof context === 'function') {
|
|
874
773
|
const children = {};
|
|
875
774
|
const spawn = createSpawner(actorScope, this.sources.actors, children);
|
|
876
|
-
const resolvedContext = context({
|
|
775
|
+
const resolvedContext = context(withActorSelf({
|
|
877
776
|
spawn,
|
|
878
777
|
input: initEvent.input,
|
|
879
|
-
self: actorScope.self,
|
|
880
778
|
actors: this.sources.actors
|
|
881
|
-
});
|
|
779
|
+
}, actorScope));
|
|
882
780
|
const [nextState] = resolveActionsWithContext(preInitial, initEvent, actorScope, []);
|
|
883
781
|
if (resolvedContext) {
|
|
884
782
|
nextState.context = resolvedContext;
|
|
@@ -919,7 +817,7 @@ class StateMachine {
|
|
|
919
817
|
if (usesInertScope) {
|
|
920
818
|
setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
|
|
921
819
|
}
|
|
922
|
-
const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(
|
|
820
|
+
const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope, true);
|
|
923
821
|
const effects = this._collectEffects(microsteps);
|
|
924
822
|
if (this.validator) {
|
|
925
823
|
assertValid(this.validator, {
|
|
@@ -954,7 +852,12 @@ class StateMachine {
|
|
|
954
852
|
if (!snapshot?.children) {
|
|
955
853
|
return;
|
|
956
854
|
}
|
|
957
|
-
|
|
855
|
+
const children = snapshot.children;
|
|
856
|
+
for (const childId in children) {
|
|
857
|
+
if (!Object.hasOwn(children, childId)) {
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
const child = children[childId];
|
|
958
861
|
if (child._rehydrated && child.getSnapshot?.().status === 'active') {
|
|
959
862
|
child.start();
|
|
960
863
|
}
|
|
@@ -1103,10 +1006,10 @@ class StateMachine {
|
|
|
1103
1006
|
reviveContext(restoredSnapshot.context);
|
|
1104
1007
|
if (usesInertScope) {
|
|
1105
1008
|
setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
|
|
1106
|
-
return attachSnapshotActorRef(
|
|
1009
|
+
return attachSnapshotActorRef(resolvedActorScope, restoredSnapshot);
|
|
1107
1010
|
}
|
|
1108
1011
|
return this._attachPureActorRef(restoredSnapshot, resolvedActorScope);
|
|
1109
1012
|
}
|
|
1110
1013
|
}
|
|
1111
1014
|
|
|
1112
|
-
export { StateMachine as S,
|
|
1015
|
+
export { StateMachine as S, StateNode as a };
|