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
|
@@ -1,56 +1,127 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var dist_xstateActors = require('./index-
|
|
3
|
+
var dist_xstateActors = require('./index-434fac23.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);
|
|
@@ -543,19 +614,37 @@ class StateMachine {
|
|
|
543
614
|
* @param event The received event
|
|
544
615
|
*/
|
|
545
616
|
transition(snapshot, event, actorScope) {
|
|
546
|
-
const
|
|
617
|
+
const usesInertScope = !actorScope;
|
|
618
|
+
const resolvedActorScope = actorScope ?? createInertActorScope(this, snapshot);
|
|
619
|
+
if (usesInertScope) {
|
|
620
|
+
setInertActorScopeSnapshot(resolvedActorScope, snapshot, false);
|
|
621
|
+
}
|
|
622
|
+
const fastSnapshot = this._transitionFast(snapshot, event, resolvedActorScope);
|
|
547
623
|
if (fastSnapshot) {
|
|
548
|
-
|
|
624
|
+
if (usesInertScope) {
|
|
625
|
+
setInertActorScopeSnapshot(resolvedActorScope, fastSnapshot, false);
|
|
626
|
+
}
|
|
627
|
+
const returnedSnapshot = usesInertScope && fastSnapshot !== snapshot ? attachSnapshotActorRef(this, resolvedActorScope, fastSnapshot) : this._attachPureActorRef(fastSnapshot, resolvedActorScope);
|
|
628
|
+
return [returnedSnapshot, []];
|
|
549
629
|
}
|
|
550
630
|
const {
|
|
551
631
|
snapshot: nextSnapshot,
|
|
552
632
|
microsteps
|
|
553
|
-
} = dist_xstateActors.macrostep(snapshot, event,
|
|
554
|
-
|
|
633
|
+
} = dist_xstateActors.macrostep(snapshot, event, resolvedActorScope, []);
|
|
634
|
+
if (usesInertScope) {
|
|
635
|
+
setInertActorScopeSnapshot(resolvedActorScope, nextSnapshot, false);
|
|
636
|
+
}
|
|
637
|
+
const returnedSnapshot = usesInertScope ? nextSnapshot === snapshot ? nextSnapshot : attachSnapshotActorRef(this, resolvedActorScope, nextSnapshot) : this._attachPureActorRef(nextSnapshot, resolvedActorScope);
|
|
638
|
+
return [returnedSnapshot, this._collectEffects(microsteps)];
|
|
639
|
+
}
|
|
640
|
+
_collectEffects(microsteps) {
|
|
641
|
+
return microsteps.flatMap(([, actions]) => actions);
|
|
555
642
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
643
|
+
_attachPureActorRef(snapshot, actorScope) {
|
|
644
|
+
if (isInertActorScope(actorScope)) {
|
|
645
|
+
return snapshot;
|
|
646
|
+
}
|
|
647
|
+
return attachSnapshotActorRef(this, actorScope, snapshot);
|
|
559
648
|
}
|
|
560
649
|
_transitionFast(snapshot, event, actorScope) {
|
|
561
650
|
if (snapshot.status !== 'active' || typeof snapshot.value !== 'string' || this.root.always?.length) {
|
|
@@ -616,6 +705,9 @@ class StateMachine {
|
|
|
616
705
|
return dist_xstateActors.transitionNode(this.root, snapshot.value, snapshot, event, self) || [];
|
|
617
706
|
}
|
|
618
707
|
isInternalEventType(eventType) {
|
|
708
|
+
if (eventType === dist_xstateActors.XSTATE_TIMER) {
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
619
711
|
for (const descriptor of this.internalEventDescriptors) {
|
|
620
712
|
if (dist_xstateActors.matchesEventDescriptor(eventType, descriptor)) {
|
|
621
713
|
return true;
|
|
@@ -682,10 +774,10 @@ class StateMachine {
|
|
|
682
774
|
_nodes: [this.root],
|
|
683
775
|
children: {},
|
|
684
776
|
status: 'active'
|
|
685
|
-
}, this);
|
|
777
|
+
}, this, actorScope.self);
|
|
686
778
|
if (typeof context === 'function') {
|
|
687
779
|
const children = {};
|
|
688
|
-
const spawn = createSpawner(actorScope,
|
|
780
|
+
const spawn = createSpawner(actorScope, this.implementations.actorSources, children);
|
|
689
781
|
const resolvedContext = context({
|
|
690
782
|
spawn,
|
|
691
783
|
input: initEvent.input,
|
|
@@ -715,30 +807,37 @@ class StateMachine {
|
|
|
715
807
|
return this.initialTransition(input, actorScope)[0];
|
|
716
808
|
}
|
|
717
809
|
initialTransition(input, actorScope) {
|
|
810
|
+
const usesInertScope = !actorScope;
|
|
811
|
+
const resolvedActorScope = actorScope ?? createInertActorScope(this);
|
|
718
812
|
const initEvent = dist_xstateActors.createInitEvent(input); // TODO: fix;
|
|
719
813
|
const internalQueue = [];
|
|
720
|
-
const preInitialState = this._getPreInitialState(
|
|
814
|
+
const preInitialState = this._getPreInitialState(resolvedActorScope, initEvent);
|
|
815
|
+
const contextSpawnEffects = Object.values(preInitialState.children).filter(Boolean).map(actor => dist_xstateActors.createSpawnEffect(actor));
|
|
721
816
|
let nextState;
|
|
722
817
|
let initialActions = [];
|
|
723
818
|
let microsteps = [];
|
|
724
819
|
let macroState;
|
|
725
820
|
try {
|
|
726
|
-
[nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState,
|
|
821
|
+
[nextState, initialActions] = dist_xstateActors.initialMicrostep(this.root, preInitialState, resolvedActorScope, initEvent, internalQueue);
|
|
727
822
|
({
|
|
728
823
|
snapshot: macroState,
|
|
729
824
|
microsteps
|
|
730
|
-
} = dist_xstateActors.macrostep(nextState, initEvent,
|
|
825
|
+
} = dist_xstateActors.macrostep(nextState, initEvent, resolvedActorScope, internalQueue, [[nextState, [...contextSpawnEffects, ...initialActions]]]));
|
|
731
826
|
} catch (err) {
|
|
732
827
|
if (!this.root.config.onError) {
|
|
733
828
|
throw err;
|
|
734
829
|
}
|
|
735
830
|
const errorEvent = dist_xstateActors.createErrorPlatformEvent('execution', err);
|
|
736
|
-
const errorMacrostep = dist_xstateActors.macrostep(preInitialState, errorEvent,
|
|
831
|
+
const errorMacrostep = dist_xstateActors.macrostep(preInitialState, errorEvent, resolvedActorScope, [], [[preInitialState, contextSpawnEffects]]);
|
|
737
832
|
macroState = errorMacrostep.snapshot;
|
|
738
833
|
microsteps = errorMacrostep.microsteps;
|
|
739
834
|
initialActions = [];
|
|
740
835
|
}
|
|
741
|
-
|
|
836
|
+
if (usesInertScope) {
|
|
837
|
+
setInertActorScopeSnapshot(resolvedActorScope, macroState, false);
|
|
838
|
+
}
|
|
839
|
+
const returnedSnapshot = usesInertScope ? attachSnapshotActorRef(this, resolvedActorScope, macroState) : this._attachPureActorRef(macroState, resolvedActorScope);
|
|
840
|
+
return [returnedSnapshot, this._collectEffects(microsteps)];
|
|
742
841
|
}
|
|
743
842
|
start(snapshot) {
|
|
744
843
|
// Start rehydrated children that were active when persisted. Freshly
|
|
@@ -766,7 +865,9 @@ class StateMachine {
|
|
|
766
865
|
getPersistedSnapshot(snapshot, options) {
|
|
767
866
|
return dist_xstateActors.getPersistedSnapshot(snapshot, options);
|
|
768
867
|
}
|
|
769
|
-
restoreSnapshot(snapshot,
|
|
868
|
+
restoreSnapshot(snapshot, actorScope) {
|
|
869
|
+
const usesInertScope = !actorScope;
|
|
870
|
+
const resolvedActorScope = actorScope ?? createInertActorScope(this);
|
|
770
871
|
const persistedVersion = snapshot.version;
|
|
771
872
|
if (persistedVersion !== this.version) {
|
|
772
873
|
const migrate = this.config.migrate;
|
|
@@ -786,9 +887,9 @@ class StateMachine {
|
|
|
786
887
|
if (!logic) {
|
|
787
888
|
continue;
|
|
788
889
|
}
|
|
789
|
-
const actor =
|
|
890
|
+
const actor = resolvedActorScope.system.createActorRef(logic, {
|
|
790
891
|
id: actorId,
|
|
791
|
-
parent:
|
|
892
|
+
parent: resolvedActorScope.self,
|
|
792
893
|
syncSnapshot: actorData.syncSnapshot,
|
|
793
894
|
snapshot: childState,
|
|
794
895
|
src,
|
|
@@ -799,6 +900,19 @@ class StateMachine {
|
|
|
799
900
|
actor._rehydrated = true;
|
|
800
901
|
children[actorId] = actor;
|
|
801
902
|
}
|
|
903
|
+
const timers = {};
|
|
904
|
+
const persistedTimers = snapshotData.timers ?? {};
|
|
905
|
+
for (const [id, timer] of Object.entries(persistedTimers)) {
|
|
906
|
+
const target = typeof timer.target === 'string' ? timer.target === 'self' ? 'self' : children[timer.target] : resolvedActorScope.self._parent;
|
|
907
|
+
if (!target) {
|
|
908
|
+
const targetDescription = typeof timer.target === 'string' ? timer.target : timer.target.type;
|
|
909
|
+
throw new Error(`Unable to restore timer '${id}': target actor '${targetDescription}' is unavailable.`);
|
|
910
|
+
}
|
|
911
|
+
timers[id] = {
|
|
912
|
+
...timer,
|
|
913
|
+
target
|
|
914
|
+
};
|
|
915
|
+
}
|
|
802
916
|
const reviveHistoryValue = historyValue => {
|
|
803
917
|
if (!historyValue || typeof historyValue !== 'object') {
|
|
804
918
|
return {};
|
|
@@ -834,11 +948,12 @@ class StateMachine {
|
|
|
834
948
|
const restoredSnapshot = dist_xstateActors.createMachineSnapshot({
|
|
835
949
|
...persistedRest,
|
|
836
950
|
children,
|
|
951
|
+
timers,
|
|
837
952
|
_nodes: nodes,
|
|
838
953
|
value: snapshotData.value,
|
|
839
954
|
historyValue: revivedHistoryValue,
|
|
840
955
|
_stateInputs: snapshotData.stateInputs ?? {}
|
|
841
|
-
}, this);
|
|
956
|
+
}, this, resolvedActorScope.self);
|
|
842
957
|
const seen = new WeakSet();
|
|
843
958
|
function reviveContext(contextPart) {
|
|
844
959
|
if (seen.has(contextPart)) {
|
|
@@ -857,9 +972,18 @@ class StateMachine {
|
|
|
857
972
|
}
|
|
858
973
|
}
|
|
859
974
|
reviveContext(restoredSnapshot.context);
|
|
860
|
-
|
|
975
|
+
if (usesInertScope) {
|
|
976
|
+
setInertActorScopeSnapshot(resolvedActorScope, restoredSnapshot, false);
|
|
977
|
+
return attachSnapshotActorRef(this, resolvedActorScope, restoredSnapshot);
|
|
978
|
+
}
|
|
979
|
+
return this._attachPureActorRef(restoredSnapshot, resolvedActorScope);
|
|
861
980
|
}
|
|
862
981
|
}
|
|
863
982
|
|
|
864
983
|
exports.StateMachine = StateMachine;
|
|
865
984
|
exports.StateNode = StateNode;
|
|
985
|
+
exports.attachSnapshotActorRef = attachSnapshotActorRef;
|
|
986
|
+
exports.createInertActorScope = createInertActorScope;
|
|
987
|
+
exports.getInitialSnapshot = getInitialSnapshot;
|
|
988
|
+
exports.getNextSnapshot = getNextSnapshot;
|
|
989
|
+
exports.setInertActorScopeSnapshot = setInertActorScopeSnapshot;
|