xstate 6.0.0-alpha.21 → 6.0.0-alpha.23
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-3c17fe3e.esm.js → StateMachine-022864fd.esm.js} +183 -64
- package/dist/{StateMachine-550ff7f2.development.cjs.js → StateMachine-53114dce.development.cjs.js} +187 -63
- package/dist/{StateMachine-1b155670.cjs.js → StateMachine-aad57180.cjs.js} +187 -63
- package/dist/{StateMachine-e25dd61f.development.esm.js → StateMachine-d2247245.development.esm.js} +183 -64
- package/dist/declarations/src/State.d.ts +4 -2
- package/dist/declarations/src/StateMachine.d.ts +5 -4
- package/dist/declarations/src/actions.d.ts +10 -8
- package/dist/declarations/src/actors/logic.d.ts +5 -21
- package/dist/declarations/src/createActor.d.ts +3 -1
- package/dist/declarations/src/fsm.d.ts +3 -1
- package/dist/declarations/src/index.d.ts +2 -2
- package/dist/declarations/src/spawn.d.ts +3 -3
- package/dist/declarations/src/stateUtils.d.ts +1 -1
- package/dist/declarations/src/system.d.ts +34 -9
- package/dist/declarations/src/transitionActions.d.ts +5 -1
- package/dist/declarations/src/types.d.ts +70 -51
- package/dist/{index-3f87408c.esm.js → index-3609e096.esm.js} +782 -389
- package/dist/{index-ec7c7a2d.development.cjs.js → index-41c0375c.development.cjs.js} +790 -390
- package/dist/{index-e694228c.cjs.js → index-434fac23.cjs.js} +789 -389
- package/dist/{index-d39fb138.development.esm.js → index-f5407323.development.esm.js} +783 -390
- 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 +82 -103
- package/dist/xstate.cjs.mjs +1 -0
- package/dist/xstate.development.cjs.js +82 -103
- package/dist/xstate.development.cjs.mjs +1 -0
- package/dist/xstate.development.esm.js +80 -102
- package/dist/xstate.esm.js +80 -102
- 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-550ff7f2.development.cjs.js → StateMachine-53114dce.development.cjs.js}
RENAMED
|
@@ -1,56 +1,127 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var dist_xstateActors = require('./index-
|
|
3
|
+
var dist_xstateActors = require('./index-41c0375c.development.cjs.js');
|
|
4
4
|
|
|
5
|
-
function createSpawner(actorScope, {
|
|
6
|
-
machine,
|
|
7
|
-
context
|
|
8
|
-
}, event, spawnedChildren) {
|
|
9
|
-
const spawn = (src, options) => {
|
|
10
|
-
if (typeof src === 'string') {
|
|
11
|
-
const logic = dist_xstateActors.resolveReferencedActor(machine, src);
|
|
12
|
-
if (!logic) {
|
|
13
|
-
throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
|
|
14
|
-
}
|
|
15
|
-
const actor = dist_xstateActors.createActor(logic, {
|
|
16
|
-
id: options?.id,
|
|
17
|
-
parent: actorScope.self,
|
|
18
|
-
syncSnapshot: options?.syncSnapshot,
|
|
19
|
-
input: typeof options?.input === 'function' ? options.input({
|
|
20
|
-
context,
|
|
21
|
-
event,
|
|
22
|
-
self: actorScope.self
|
|
23
|
-
}) : options?.input,
|
|
24
|
-
src,
|
|
25
|
-
registryKey: options?.registryKey
|
|
26
|
-
});
|
|
27
|
-
spawnedChildren[actor.id] = actor;
|
|
28
|
-
return actor;
|
|
29
|
-
} else {
|
|
30
|
-
const actor = dist_xstateActors.createActor(src, {
|
|
31
|
-
id: options?.id,
|
|
32
|
-
parent: actorScope.self,
|
|
33
|
-
syncSnapshot: options?.syncSnapshot,
|
|
34
|
-
input: options?.input,
|
|
35
|
-
src,
|
|
36
|
-
registryKey: options?.registryKey
|
|
37
|
-
});
|
|
38
|
-
return actor;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
5
|
+
function createSpawner(actorScope, actorSources, spawnedChildren) {
|
|
41
6
|
return (src, options) => {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
7
|
+
const referencedSrc = Object.entries(actorSources).find(([, logic]) => logic === src)?.[0];
|
|
8
|
+
const actor = actorScope.system.createActorRef(src, {
|
|
9
|
+
id: options?.id,
|
|
10
|
+
parent: actorScope.self,
|
|
11
|
+
syncSnapshot: options?.syncSnapshot,
|
|
12
|
+
input: options?.input,
|
|
13
|
+
src: referencedSrc ?? src,
|
|
14
|
+
registryKey: options?.registryKey
|
|
49
15
|
});
|
|
16
|
+
spawnedChildren[actor.id] = actor;
|
|
50
17
|
return actor;
|
|
51
18
|
};
|
|
52
19
|
}
|
|
53
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, previousSelf, 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
|
+
} : {}),
|
|
52
|
+
...(system ? {
|
|
53
|
+
_systemRef: {
|
|
54
|
+
current: system
|
|
55
|
+
}
|
|
56
|
+
} : {})
|
|
57
|
+
});
|
|
58
|
+
if (previousSelf?._parent) {
|
|
59
|
+
self._parent = previousSelf._parent;
|
|
60
|
+
}
|
|
61
|
+
if (snapshot) {
|
|
62
|
+
self._snapshot = snapshot;
|
|
63
|
+
}
|
|
64
|
+
const actorScope = {
|
|
65
|
+
self: self,
|
|
66
|
+
defer: () => {},
|
|
67
|
+
id: self.id,
|
|
68
|
+
logger: self.system._logger,
|
|
69
|
+
sessionId: self.sessionId,
|
|
70
|
+
stopChild: child => child._stop(),
|
|
71
|
+
system: self.system,
|
|
72
|
+
emit: event => self.system.emitEvent(self, event),
|
|
73
|
+
actionExecutor: () => {}
|
|
74
|
+
};
|
|
75
|
+
return actorScope;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @deprecated Use `initialTransition(…)` instead. */
|
|
79
|
+
function getInitialSnapshot(actorLogic, ...[input]) {
|
|
80
|
+
const actorScope = createInertActorScope(actorLogic);
|
|
81
|
+
return actorLogic.initialTransition(input, actorScope)[0];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Determines the next snapshot for the given `actorLogic` based on the given
|
|
86
|
+
* `snapshot` and `event`.
|
|
87
|
+
*
|
|
88
|
+
* If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
|
|
89
|
+
* used.
|
|
90
|
+
*
|
|
91
|
+
* @deprecated Use `transition(…)` instead.
|
|
92
|
+
* @example
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { getNextSnapshot } from 'xstate';
|
|
96
|
+
* import { trafficLightMachine } from './trafficLightMachine.ts';
|
|
97
|
+
*
|
|
98
|
+
* const nextSnapshot = getNextSnapshot(
|
|
99
|
+
* trafficLightMachine, // actor logic
|
|
100
|
+
* undefined, // snapshot (or initial state if undefined)
|
|
101
|
+
* { type: 'TIMER' }
|
|
102
|
+
* ); // event object
|
|
103
|
+
*
|
|
104
|
+
* console.log(nextSnapshot.value);
|
|
105
|
+
* // => 'yellow'
|
|
106
|
+
*
|
|
107
|
+
* const nextSnapshot2 = getNextSnapshot(
|
|
108
|
+
* trafficLightMachine, // actor logic
|
|
109
|
+
* nextSnapshot, // snapshot
|
|
110
|
+
* { type: 'TIMER' }
|
|
111
|
+
* ); // event object
|
|
112
|
+
*
|
|
113
|
+
* console.log(nextSnapshot2.value);
|
|
114
|
+
* // =>'red'
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
function getNextSnapshot(actorLogic, snapshot, event) {
|
|
118
|
+
const actorScope = createInertActorScope(actorLogic, snapshot);
|
|
119
|
+
const transitionResult = actorLogic.transition(snapshot, event, actorScope);
|
|
120
|
+
const nextSnapshot = Array.isArray(transitionResult) ? transitionResult[0] : transitionResult;
|
|
121
|
+
setInertActorScopeSnapshot(actorScope, nextSnapshot, false);
|
|
122
|
+
return nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(actorLogic, actorScope, nextSnapshot);
|
|
123
|
+
}
|
|
124
|
+
|
|
54
125
|
const cache = new WeakMap();
|
|
55
126
|
function memo(object, key, fn) {
|
|
56
127
|
let memoizedData = cache.get(object);
|
|
@@ -572,19 +643,37 @@ class StateMachine {
|
|
|
572
643
|
* @param event The received event
|
|
573
644
|
*/
|
|
574
645
|
transition(snapshot, event, actorScope) {
|
|
575
|
-
const
|
|
646
|
+
const usesInertScope = !actorScope;
|
|
647
|
+
const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
|
|
648
|
+
if (usesInertScope) {
|
|
649
|
+
setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
|
|
650
|
+
}
|
|
651
|
+
const fastSnapshot = this._transitionFast(snapshot, event, resolvedActorScope);
|
|
576
652
|
if (fastSnapshot) {
|
|
577
|
-
|
|
653
|
+
if (usesInertScope) {
|
|
654
|
+
setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
|
|
655
|
+
}
|
|
656
|
+
const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
|
|
657
|
+
return [returnedSnapshot, []];
|
|
578
658
|
}
|
|
579
659
|
const {
|
|
580
660
|
snapshot: nextSnapshot,
|
|
581
661
|
microsteps
|
|
582
|
-
} = dist_xstateActors.macrostep(snapshot, event,
|
|
583
|
-
|
|
662
|
+
} = dist_xstateActors.macrostep(snapshot, event, resolvedActorScope, []);
|
|
663
|
+
if (usesInertScope) {
|
|
664
|
+
setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
|
|
665
|
+
}
|
|
666
|
+
const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
|
|
667
|
+
return [returnedSnapshot, this._collectEffects(microsteps)];
|
|
668
|
+
}
|
|
669
|
+
_collectEffects(microsteps) {
|
|
670
|
+
return microsteps.flatMap(([, actions]) => actions);
|
|
584
671
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
672
|
+
_attachPureActorRef(snapshot, actorScope) {
|
|
673
|
+
if (isInertActorScope(actorScope)) {
|
|
674
|
+
return snapshot;
|
|
675
|
+
}
|
|
676
|
+
return attachSnapshotActorRef(this, actorScope, snapshot);
|
|
588
677
|
}
|
|
589
678
|
_transitionFast(snapshot, event, actorScope) {
|
|
590
679
|
if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
|
|
@@ -645,6 +734,9 @@ class StateMachine {
|
|
|
645
734
|
return dist_xstateActors.transitionNode(this.root, snapshot.value, snapshot, event, self) || [];
|
|
646
735
|
}
|
|
647
736
|
isInternalEventType(eventType) {
|
|
737
|
+
if (eventType === dist_xstateActors.XSTATE_TIMER) {
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
648
740
|
for (const descriptor of this.internalEventDescriptors) {
|
|
649
741
|
if (dist_xstateActors.matchesEventDescriptor(eventType, descriptor)) {
|
|
650
742
|
return true;
|
|
@@ -711,10 +803,10 @@ class StateMachine {
|
|
|
711
803
|
_nodes: [this.root],
|
|
712
804
|
children: {},
|
|
713
805
|
status: 'active'
|
|
714
|
-
}, this);
|
|
806
|
+
}, this, actorScope.self);
|
|
715
807
|
if (typeof context === 'function') {
|
|
716
808
|
const children = {};
|
|
717
|
-
const spawn = createSpawner(actorScope,
|
|
809
|
+
const spawn = createSpawner(actorScope, this.implementations.actorSources, children);
|
|
718
810
|
const resolvedContext = context({
|
|
719
811
|
spawn,
|
|
720
812
|
input: initEvent.input,
|
|
@@ -744,30 +836,37 @@ class StateMachine {
|
|
|
744
836
|
return this.initialTransition(input, actorScope)[0];
|
|
745
837
|
}
|
|
746
838
|
initialTransition(input, actorScope) {
|
|
839
|
+
const usesInertScope = !actorScope;
|
|
840
|
+
const resolvedActorScope = actorScope ?? createInertActorScope(this);
|
|
747
841
|
const initEvent = dist_xstateActors.createInitEvent(input); // TODO: fix;
|
|
748
842
|
const internalQueue = [];
|
|
749
|
-
const preInitialState = this._getPreInitialState(
|
|
843
|
+
const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
|
|
844
|
+
const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
|
|
750
845
|
let nextState;
|
|
751
846
|
let initialActions = [];
|
|
752
847
|
let microsteps = [];
|
|
753
848
|
let macroState;
|
|
754
849
|
try {
|
|
755
|
-
[nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState,
|
|
850
|
+
[nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
|
|
756
851
|
({
|
|
757
852
|
snapshot: macroState,
|
|
758
853
|
microsteps
|
|
759
|
-
} = dist_xstateActors.macrostep(nextState, initEvent,
|
|
854
|
+
} = dist_xstateActors.macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]));
|
|
760
855
|
} catch (err) {
|
|
761
856
|
if (!this.root.config.onError) {
|
|
762
857
|
throw err;
|
|
763
858
|
}
|
|
764
859
|
const errorEvent = dist_xstateActors.createErrorPlatformEvent('execution', err);
|
|
765
|
-
const errorMacrostep = dist_xstateActors.macrostep(preInitialState, errorEvent,
|
|
860
|
+
const errorMacrostep = dist_xstateActors.macrostep(preInitialState, errorEvent, resolvedActorScope, [], [[preInitialState, contextSpawnEffects]]);
|
|
766
861
|
macroState = errorMacrostep.snapshot;
|
|
767
862
|
microsteps = errorMacrostep.microsteps;
|
|
768
863
|
initialActions = [];
|
|
769
864
|
}
|
|
770
|
-
|
|
865
|
+
if (usesInertScope) {
|
|
866
|
+
setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
|
|
867
|
+
}
|
|
868
|
+
const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
|
|
869
|
+
return [returnedSnapshot, this._collectEffects(microsteps)];
|
|
771
870
|
}
|
|
772
871
|
start(snapshot) {
|
|
773
872
|
// Start rehydrated children that were active when persisted. Freshly
|
|
@@ -795,7 +894,9 @@ class StateMachine {
|
|
|
795
894
|
getPersistedSnapshot(snapshot, options) {
|
|
796
895
|
return dist_xstateActors.getPersistedSnapshot(snapshot, options);
|
|
797
896
|
}
|
|
798
|
-
restoreSnapshot(snapshot,
|
|
897
|
+
restoreSnapshot(snapshot, actorScope) {
|
|
898
|
+
const usesInertScope = !actorScope;
|
|
899
|
+
const resolvedActorScope = actorScope ?? createInertActorScope(this);
|
|
799
900
|
const persistedVersion = snapshot.version;
|
|
800
901
|
if (persistedVersion !== this.version) {
|
|
801
902
|
const migrate = this.config.migrate;
|
|
@@ -815,9 +916,9 @@ class StateMachine {
|
|
|
815
916
|
if (!logic) {
|
|
816
917
|
continue;
|
|
817
918
|
}
|
|
818
|
-
const actor =
|
|
919
|
+
const actor = resolvedActorScope.system.createActorRef(logic, {
|
|
819
920
|
id: actorId,
|
|
820
|
-
parent:
|
|
921
|
+
parent: resolvedActorScope.self,
|
|
821
922
|
syncSnapshot: actorData.syncSnapshot,
|
|
822
923
|
snapshot: childState,
|
|
823
924
|
src,
|
|
@@ -828,6 +929,19 @@ class StateMachine {
|
|
|
828
929
|
actor._rehydrated = true;
|
|
829
930
|
children[actorId] = actor;
|
|
830
931
|
}
|
|
932
|
+
const timers = {};
|
|
933
|
+
const persistedTimers = snapshotData.timers ?? {};
|
|
934
|
+
for (const [id, timer] of Object.entries(persistedTimers)) {
|
|
935
|
+
const target = typeof timer.target === 'string' ? timer.target === 'self' ? 'self' : children[timer.target] : resolvedActorScope.self._parent;
|
|
936
|
+
if (!target) {
|
|
937
|
+
const targetDescription = typeof timer.target === 'string' ? timer.target : timer.target.type;
|
|
938
|
+
throw new Error(`Unable to restore timer '${id}': target actor '${targetDescription}' is unavailable.`);
|
|
939
|
+
}
|
|
940
|
+
timers[id] = {
|
|
941
|
+
...timer,
|
|
942
|
+
target
|
|
943
|
+
};
|
|
944
|
+
}
|
|
831
945
|
const reviveHistoryValue = historyValue => {
|
|
832
946
|
if (!historyValue || typeof historyValue !== 'object') {
|
|
833
947
|
return {};
|
|
@@ -866,11 +980,12 @@ class StateMachine {
|
|
|
866
980
|
const restoredSnapshot = dist_xstateActors.createMachineSnapshot({
|
|
867
981
|
...persistedRest,
|
|
868
982
|
children,
|
|
983
|
+
timers,
|
|
869
984
|
_nodes: nodes,
|
|
870
985
|
value: snapshotData.value,
|
|
871
986
|
historyValue: revivedHistoryValue,
|
|
872
987
|
_stateInputs: snapshotData.stateInputs ?? {}
|
|
873
|
-
}, this);
|
|
988
|
+
}, this, resolvedActorScope.self);
|
|
874
989
|
const seen = new WeakSet();
|
|
875
990
|
function reviveContext(contextPart) {
|
|
876
991
|
if (seen.has(contextPart)) {
|
|
@@ -889,9 +1004,18 @@ class StateMachine {
|
|
|
889
1004
|
}
|
|
890
1005
|
}
|
|
891
1006
|
reviveContext(restoredSnapshot.context);
|
|
892
|
-
|
|
1007
|
+
if (usesInertScope) {
|
|
1008
|
+
setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
|
|
1009
|
+
return attachSnapshotActorRef(this, resolvedActorScope, restoredSnapshot);
|
|
1010
|
+
}
|
|
1011
|
+
return this._attachPureActorRef(restoredSnapshot, resolvedActorScope);
|
|
893
1012
|
}
|
|
894
1013
|
}
|
|
895
1014
|
|
|
896
1015
|
exports.StateMachine = StateMachine;
|
|
897
1016
|
exports.StateNode = StateNode;
|
|
1017
|
+
exports.attachSnapshotActorRef = attachSnapshotActorRef;
|
|
1018
|
+
exports.createInertActorScope = createInertActorScope;
|
|
1019
|
+
exports.getInitialSnapshot = getInitialSnapshot;
|
|
1020
|
+
exports.getNextSnapshot = getNextSnapshot;
|
|
1021
|
+
exports.setInertActorScopeSnapshot = setInertActorScopeSnapshot;
|