xstate 5.0.0-beta.52 → 5.0.0-beta.54
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/actions/dist/xstate-actions.cjs.js +2 -3
- package/actions/dist/xstate-actions.cjs.mjs +0 -1
- package/actions/dist/xstate-actions.development.cjs.js +2 -3
- package/actions/dist/xstate-actions.development.cjs.mjs +0 -1
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -2
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +17 -17
- package/actors/dist/xstate-actors.development.cjs.js +17 -17
- package/actors/dist/xstate-actors.development.esm.js +17 -17
- package/actors/dist/xstate-actors.esm.js +17 -17
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/State.d.ts +2 -2
- package/dist/declarations/src/StateMachine.d.ts +4 -4
- package/dist/declarations/src/StateNode.d.ts +1 -1
- package/dist/declarations/src/actions/send.d.ts +1 -9
- package/dist/declarations/src/actions.d.ts +1 -1
- package/dist/declarations/src/actors/transition.d.ts +1 -1
- package/dist/declarations/src/guards.d.ts +1 -1
- package/dist/declarations/src/interpreter.d.ts +5 -5
- package/dist/declarations/src/stateUtils.d.ts +9 -9
- package/dist/declarations/src/types.d.ts +15 -11
- package/dist/{log-d160285c.development.cjs.js → log-516ee274.development.cjs.js} +23 -40
- package/dist/{log-3d815f5e.cjs.js → log-5b24d580.cjs.js} +23 -40
- package/dist/{log-826c9895.development.esm.js → log-5ebdc815.development.esm.js} +24 -40
- package/dist/{log-6bc0e1e7.esm.js → log-ee8cb6d6.esm.js} +24 -40
- package/dist/{raise-9dd3e757.development.cjs.js → raise-57f3c654.development.cjs.js} +130 -130
- package/dist/{raise-367eeb6f.cjs.js → raise-5f22d333.cjs.js} +130 -130
- package/dist/{raise-f71460d6.esm.js → raise-6f820d79.esm.js} +130 -130
- package/dist/{raise-1caefe80.development.esm.js → raise-a6c478f8.development.esm.js} +130 -130
- package/dist/xstate.cjs.js +17 -18
- package/dist/xstate.cjs.mjs +0 -1
- package/dist/xstate.development.cjs.js +17 -18
- package/dist/xstate.development.cjs.mjs +0 -1
- package/dist/xstate.development.esm.js +19 -19
- package/dist/xstate.esm.js +19 -19
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -100,10 +100,10 @@ function createDoneActorEvent(invokeId, output) {
|
|
|
100
100
|
output
|
|
101
101
|
};
|
|
102
102
|
}
|
|
103
|
-
function createErrorActorEvent(id,
|
|
103
|
+
function createErrorActorEvent(id, error) {
|
|
104
104
|
return {
|
|
105
105
|
type: `xstate.error.actor.${id}`,
|
|
106
|
-
|
|
106
|
+
error
|
|
107
107
|
};
|
|
108
108
|
}
|
|
109
109
|
function createInitEvent(input) {
|
|
@@ -351,7 +351,7 @@ class Actor {
|
|
|
351
351
|
/**
|
|
352
352
|
* The current internal state of the actor.
|
|
353
353
|
*/
|
|
354
|
-
this.
|
|
354
|
+
this._snapshot = void 0;
|
|
355
355
|
/**
|
|
356
356
|
* The clock that is responsible for setting and clearing timeouts, such as delayed events and transitions.
|
|
357
357
|
*/
|
|
@@ -441,19 +441,19 @@ class Actor {
|
|
|
441
441
|
this._systemId = systemId;
|
|
442
442
|
this.system._set(systemId, this);
|
|
443
443
|
}
|
|
444
|
-
this._initState(options?.state);
|
|
445
|
-
if (systemId && this.
|
|
444
|
+
this._initState(options?.snapshot ?? options?.state);
|
|
445
|
+
if (systemId && this._snapshot.status !== 'active') {
|
|
446
446
|
this.system._unregister(this);
|
|
447
447
|
}
|
|
448
448
|
}
|
|
449
449
|
_initState(persistedState) {
|
|
450
450
|
try {
|
|
451
|
-
this.
|
|
451
|
+
this._snapshot = persistedState ? this.logic.restoreSnapshot ? this.logic.restoreSnapshot(persistedState, this._actorScope) : persistedState : this.logic.getInitialState(this._actorScope, this.options?.input);
|
|
452
452
|
} catch (err) {
|
|
453
|
-
// if we get here then it means that we assign a value to this.
|
|
453
|
+
// if we get here then it means that we assign a value to this._snapshot that is not of the correct type
|
|
454
454
|
// we can't get the true `TSnapshot & { status: 'error'; }`, it's impossible
|
|
455
455
|
// so right now this is a lie of sorts
|
|
456
|
-
this.
|
|
456
|
+
this._snapshot = {
|
|
457
457
|
status: 'error',
|
|
458
458
|
output: undefined,
|
|
459
459
|
error: err
|
|
@@ -462,7 +462,7 @@ class Actor {
|
|
|
462
462
|
}
|
|
463
463
|
update(snapshot, event) {
|
|
464
464
|
// Update state
|
|
465
|
-
this.
|
|
465
|
+
this._snapshot = snapshot;
|
|
466
466
|
|
|
467
467
|
// Execute deferred effects
|
|
468
468
|
let deferredFn;
|
|
@@ -476,14 +476,14 @@ class Actor {
|
|
|
476
476
|
// no "builtin deferred" should actually throw an error since they are either safe
|
|
477
477
|
// or the control flow is passed through the mailbox and errors should be caught by the `_process` used by the mailbox
|
|
478
478
|
this._deferred.length = 0;
|
|
479
|
-
this.
|
|
479
|
+
this._snapshot = {
|
|
480
480
|
...snapshot,
|
|
481
481
|
status: 'error',
|
|
482
482
|
error: err
|
|
483
483
|
};
|
|
484
484
|
}
|
|
485
485
|
}
|
|
486
|
-
switch (this.
|
|
486
|
+
switch (this._snapshot.status) {
|
|
487
487
|
case 'active':
|
|
488
488
|
for (const observer of this.observers) {
|
|
489
489
|
try {
|
|
@@ -509,13 +509,13 @@ class Actor {
|
|
|
509
509
|
}
|
|
510
510
|
this._stopProcedure();
|
|
511
511
|
this._complete();
|
|
512
|
-
this._doneEvent = createDoneActorEvent(this.id, this.
|
|
512
|
+
this._doneEvent = createDoneActorEvent(this.id, this._snapshot.output);
|
|
513
513
|
if (this._parent) {
|
|
514
514
|
this.system._relay(this, this._parent, this._doneEvent);
|
|
515
515
|
}
|
|
516
516
|
break;
|
|
517
517
|
case 'error':
|
|
518
|
-
this._error(this.
|
|
518
|
+
this._error(this._snapshot.error);
|
|
519
519
|
break;
|
|
520
520
|
}
|
|
521
521
|
this.system._sendInspectionEvent({
|
|
@@ -628,24 +628,24 @@ class Actor {
|
|
|
628
628
|
actorRef: this,
|
|
629
629
|
event: initEvent
|
|
630
630
|
});
|
|
631
|
-
const status = this.
|
|
631
|
+
const status = this._snapshot.status;
|
|
632
632
|
switch (status) {
|
|
633
633
|
case 'done':
|
|
634
634
|
// a state machine can be "done" upon initialization (it could reach a final state using initial microsteps)
|
|
635
635
|
// we still need to complete observers, flush deferreds etc
|
|
636
|
-
this.update(this.
|
|
636
|
+
this.update(this._snapshot, initEvent);
|
|
637
637
|
// TODO: rethink cleanup of observers, mailbox, etc
|
|
638
638
|
return this;
|
|
639
639
|
case 'error':
|
|
640
|
-
this._error(this.
|
|
640
|
+
this._error(this._snapshot.error);
|
|
641
641
|
return this;
|
|
642
642
|
}
|
|
643
643
|
if (this.logic.start) {
|
|
644
644
|
try {
|
|
645
|
-
this.logic.start(this.
|
|
645
|
+
this.logic.start(this._snapshot, this._actorScope);
|
|
646
646
|
} catch (err) {
|
|
647
|
-
this.
|
|
648
|
-
...this.
|
|
647
|
+
this._snapshot = {
|
|
648
|
+
...this._snapshot,
|
|
649
649
|
status: 'error',
|
|
650
650
|
error: err
|
|
651
651
|
};
|
|
@@ -657,7 +657,7 @@ class Actor {
|
|
|
657
657
|
// TODO: this notifies all subscribers but usually this is redundant
|
|
658
658
|
// there is no real change happening here
|
|
659
659
|
// we need to rethink if this needs to be refactored
|
|
660
|
-
this.update(this.
|
|
660
|
+
this.update(this._snapshot, initEvent);
|
|
661
661
|
if (this.options.devTools) {
|
|
662
662
|
this.attachDevTools();
|
|
663
663
|
}
|
|
@@ -668,7 +668,7 @@ class Actor {
|
|
|
668
668
|
let nextState;
|
|
669
669
|
let caughtError;
|
|
670
670
|
try {
|
|
671
|
-
nextState = this.logic.transition(this.
|
|
671
|
+
nextState = this.logic.transition(this._snapshot, event, this._actorScope);
|
|
672
672
|
} catch (err) {
|
|
673
673
|
// we wrap it in a box so we can rethrow it later even if falsy value gets caught here
|
|
674
674
|
caughtError = {
|
|
@@ -679,8 +679,8 @@ class Actor {
|
|
|
679
679
|
const {
|
|
680
680
|
err
|
|
681
681
|
} = caughtError;
|
|
682
|
-
this.
|
|
683
|
-
...this.
|
|
682
|
+
this._snapshot = {
|
|
683
|
+
...this._snapshot,
|
|
684
684
|
status: 'error',
|
|
685
685
|
error: err
|
|
686
686
|
};
|
|
@@ -860,8 +860,8 @@ class Actor {
|
|
|
860
860
|
* @see https://stately.ai/docs/persistence
|
|
861
861
|
*/
|
|
862
862
|
|
|
863
|
-
|
|
864
|
-
return this.logic.
|
|
863
|
+
getPersistedSnapshot(options) {
|
|
864
|
+
return this.logic.getPersistedSnapshot(this._snapshot, options);
|
|
865
865
|
}
|
|
866
866
|
[symbolObservable]() {
|
|
867
867
|
return this;
|
|
@@ -879,10 +879,10 @@ class Actor {
|
|
|
879
879
|
* Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
|
|
880
880
|
*
|
|
881
881
|
* @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
|
|
882
|
-
* @see {@link Actor.
|
|
882
|
+
* @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot).
|
|
883
883
|
*/
|
|
884
884
|
getSnapshot() {
|
|
885
|
-
return this.
|
|
885
|
+
return this._snapshot;
|
|
886
886
|
}
|
|
887
887
|
}
|
|
888
888
|
|
|
@@ -935,11 +935,11 @@ const interpret = createActor;
|
|
|
935
935
|
* @deprecated Use `Actor` instead.
|
|
936
936
|
*/
|
|
937
937
|
|
|
938
|
-
function resolveCancel(_,
|
|
938
|
+
function resolveCancel(_, snapshot, actionArgs, actionParams, {
|
|
939
939
|
sendId
|
|
940
940
|
}) {
|
|
941
941
|
const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
|
|
942
|
-
return [
|
|
942
|
+
return [snapshot, resolvedSendId];
|
|
943
943
|
}
|
|
944
944
|
function executeCancel(actorScope, resolvedSendId) {
|
|
945
945
|
actorScope.self.cancel(resolvedSendId);
|
|
@@ -961,14 +961,14 @@ function cancel(sendId) {
|
|
|
961
961
|
return cancel;
|
|
962
962
|
}
|
|
963
963
|
|
|
964
|
-
function resolveSpawn(actorScope,
|
|
964
|
+
function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
|
|
965
965
|
id,
|
|
966
966
|
systemId,
|
|
967
967
|
src,
|
|
968
968
|
input,
|
|
969
969
|
syncSnapshot
|
|
970
970
|
}) {
|
|
971
|
-
const logic = typeof src === 'string' ? resolveReferencedActor(
|
|
971
|
+
const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
|
|
972
972
|
const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
|
|
973
973
|
let actorRef;
|
|
974
974
|
if (logic) {
|
|
@@ -979,15 +979,15 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
|
|
|
979
979
|
syncSnapshot,
|
|
980
980
|
systemId,
|
|
981
981
|
input: typeof input === 'function' ? input({
|
|
982
|
-
context:
|
|
982
|
+
context: snapshot.context,
|
|
983
983
|
event: actionArgs.event,
|
|
984
984
|
self: actorScope?.self
|
|
985
985
|
}) : input
|
|
986
986
|
});
|
|
987
987
|
}
|
|
988
|
-
return [cloneMachineSnapshot(
|
|
988
|
+
return [cloneMachineSnapshot(snapshot, {
|
|
989
989
|
children: {
|
|
990
|
-
...
|
|
990
|
+
...snapshot.children,
|
|
991
991
|
[resolvedId]: actorRef
|
|
992
992
|
}
|
|
993
993
|
}), {
|
|
@@ -1017,7 +1017,7 @@ function spawnChild(...[src, {
|
|
|
1017
1017
|
} = {}]) {
|
|
1018
1018
|
function spawnChild(args, params) {
|
|
1019
1019
|
}
|
|
1020
|
-
spawnChild.type = '
|
|
1020
|
+
spawnChild.type = 'snapshot.spawnChild';
|
|
1021
1021
|
spawnChild.id = id;
|
|
1022
1022
|
spawnChild.systemId = systemId;
|
|
1023
1023
|
spawnChild.src = src;
|
|
@@ -1028,19 +1028,19 @@ function spawnChild(...[src, {
|
|
|
1028
1028
|
return spawnChild;
|
|
1029
1029
|
}
|
|
1030
1030
|
|
|
1031
|
-
function resolveStop(_,
|
|
1031
|
+
function resolveStop(_, snapshot, args, actionParams, {
|
|
1032
1032
|
actorRef
|
|
1033
1033
|
}) {
|
|
1034
1034
|
const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
|
|
1035
|
-
const resolvedActorRef = typeof actorRefOrString === 'string' ?
|
|
1036
|
-
let children =
|
|
1035
|
+
const resolvedActorRef = typeof actorRefOrString === 'string' ? snapshot.children[actorRefOrString] : actorRefOrString;
|
|
1036
|
+
let children = snapshot.children;
|
|
1037
1037
|
if (resolvedActorRef) {
|
|
1038
1038
|
children = {
|
|
1039
1039
|
...children
|
|
1040
1040
|
};
|
|
1041
1041
|
delete children[resolvedActorRef.id];
|
|
1042
1042
|
}
|
|
1043
|
-
return [cloneMachineSnapshot(
|
|
1043
|
+
return [cloneMachineSnapshot(snapshot, {
|
|
1044
1044
|
children
|
|
1045
1045
|
}), resolvedActorRef];
|
|
1046
1046
|
}
|
|
@@ -1090,14 +1090,14 @@ function stopChild(actorRef) {
|
|
|
1090
1090
|
*/
|
|
1091
1091
|
const stop = stopChild;
|
|
1092
1092
|
|
|
1093
|
-
function checkStateIn(
|
|
1093
|
+
function checkStateIn(snapshot, _, {
|
|
1094
1094
|
stateValue
|
|
1095
1095
|
}) {
|
|
1096
1096
|
if (typeof stateValue === 'string' && isStateId(stateValue)) {
|
|
1097
|
-
const target =
|
|
1098
|
-
return
|
|
1097
|
+
const target = snapshot.machine.getStateNodeById(stateValue);
|
|
1098
|
+
return snapshot._nodes.some(sn => sn === target);
|
|
1099
1099
|
}
|
|
1100
|
-
return
|
|
1100
|
+
return snapshot.matches(stateValue);
|
|
1101
1101
|
}
|
|
1102
1102
|
function stateIn(stateValue) {
|
|
1103
1103
|
function stateIn(args, params) {
|
|
@@ -1107,13 +1107,13 @@ function stateIn(stateValue) {
|
|
|
1107
1107
|
stateIn.stateValue = stateValue;
|
|
1108
1108
|
return stateIn;
|
|
1109
1109
|
}
|
|
1110
|
-
function checkNot(
|
|
1110
|
+
function checkNot(snapshot, {
|
|
1111
1111
|
context,
|
|
1112
1112
|
event
|
|
1113
1113
|
}, {
|
|
1114
1114
|
guards
|
|
1115
1115
|
}) {
|
|
1116
|
-
return !evaluateGuard(guards[0], context, event,
|
|
1116
|
+
return !evaluateGuard(guards[0], context, event, snapshot);
|
|
1117
1117
|
}
|
|
1118
1118
|
function not(guard) {
|
|
1119
1119
|
function not(args, params) {
|
|
@@ -1123,13 +1123,13 @@ function not(guard) {
|
|
|
1123
1123
|
not.guards = [guard];
|
|
1124
1124
|
return not;
|
|
1125
1125
|
}
|
|
1126
|
-
function checkAnd(
|
|
1126
|
+
function checkAnd(snapshot, {
|
|
1127
1127
|
context,
|
|
1128
1128
|
event
|
|
1129
1129
|
}, {
|
|
1130
1130
|
guards
|
|
1131
1131
|
}) {
|
|
1132
|
-
return guards.every(guard => evaluateGuard(guard, context, event,
|
|
1132
|
+
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1133
1133
|
}
|
|
1134
1134
|
function and(guards) {
|
|
1135
1135
|
function and(args, params) {
|
|
@@ -1139,13 +1139,13 @@ function and(guards) {
|
|
|
1139
1139
|
and.guards = guards;
|
|
1140
1140
|
return and;
|
|
1141
1141
|
}
|
|
1142
|
-
function checkOr(
|
|
1142
|
+
function checkOr(snapshot, {
|
|
1143
1143
|
context,
|
|
1144
1144
|
event
|
|
1145
1145
|
}, {
|
|
1146
1146
|
guards
|
|
1147
1147
|
}) {
|
|
1148
|
-
return guards.some(guard => evaluateGuard(guard, context, event,
|
|
1148
|
+
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1149
1149
|
}
|
|
1150
1150
|
function or(guards) {
|
|
1151
1151
|
function or(args, params) {
|
|
@@ -1157,17 +1157,17 @@ function or(guards) {
|
|
|
1157
1157
|
}
|
|
1158
1158
|
|
|
1159
1159
|
// TODO: throw on cycles (depth check should be enough)
|
|
1160
|
-
function evaluateGuard(guard, context, event,
|
|
1160
|
+
function evaluateGuard(guard, context, event, snapshot) {
|
|
1161
1161
|
const {
|
|
1162
1162
|
machine
|
|
1163
|
-
} =
|
|
1163
|
+
} = snapshot;
|
|
1164
1164
|
const isInline = typeof guard === 'function';
|
|
1165
1165
|
const resolved = isInline ? guard : machine.implementations.guards[typeof guard === 'string' ? guard : guard.type];
|
|
1166
1166
|
if (!isInline && !resolved) {
|
|
1167
1167
|
throw new Error(`Guard '${typeof guard === 'string' ? guard : guard.type}' is not implemented.'.`);
|
|
1168
1168
|
}
|
|
1169
1169
|
if (typeof resolved !== 'function') {
|
|
1170
|
-
return evaluateGuard(resolved, context, event,
|
|
1170
|
+
return evaluateGuard(resolved, context, event, snapshot);
|
|
1171
1171
|
}
|
|
1172
1172
|
const guardArgs = {
|
|
1173
1173
|
context,
|
|
@@ -1184,7 +1184,7 @@ function evaluateGuard(guard, context, event, state) {
|
|
|
1184
1184
|
return resolved(guardArgs, guardParams);
|
|
1185
1185
|
}
|
|
1186
1186
|
const builtinGuard = resolved;
|
|
1187
|
-
return builtinGuard.check(
|
|
1187
|
+
return builtinGuard.check(snapshot, guardArgs, resolved // this holds all params
|
|
1188
1188
|
);
|
|
1189
1189
|
}
|
|
1190
1190
|
|
|
@@ -1573,24 +1573,24 @@ function getStateNodes(stateNode, stateValue) {
|
|
|
1573
1573
|
return allSubStateNodes.concat(subStateNodes);
|
|
1574
1574
|
}, []));
|
|
1575
1575
|
}
|
|
1576
|
-
function transitionAtomicNode(stateNode, stateValue,
|
|
1576
|
+
function transitionAtomicNode(stateNode, stateValue, snapshot, event) {
|
|
1577
1577
|
const childStateNode = getStateNode(stateNode, stateValue);
|
|
1578
|
-
const next = childStateNode.next(
|
|
1578
|
+
const next = childStateNode.next(snapshot, event);
|
|
1579
1579
|
if (!next || !next.length) {
|
|
1580
|
-
return stateNode.next(
|
|
1580
|
+
return stateNode.next(snapshot, event);
|
|
1581
1581
|
}
|
|
1582
1582
|
return next;
|
|
1583
1583
|
}
|
|
1584
|
-
function transitionCompoundNode(stateNode, stateValue,
|
|
1584
|
+
function transitionCompoundNode(stateNode, stateValue, snapshot, event) {
|
|
1585
1585
|
const subStateKeys = Object.keys(stateValue);
|
|
1586
1586
|
const childStateNode = getStateNode(stateNode, subStateKeys[0]);
|
|
1587
|
-
const next = transitionNode(childStateNode, stateValue[subStateKeys[0]],
|
|
1587
|
+
const next = transitionNode(childStateNode, stateValue[subStateKeys[0]], snapshot, event);
|
|
1588
1588
|
if (!next || !next.length) {
|
|
1589
|
-
return stateNode.next(
|
|
1589
|
+
return stateNode.next(snapshot, event);
|
|
1590
1590
|
}
|
|
1591
1591
|
return next;
|
|
1592
1592
|
}
|
|
1593
|
-
function transitionParallelNode(stateNode, stateValue,
|
|
1593
|
+
function transitionParallelNode(stateNode, stateValue, snapshot, event) {
|
|
1594
1594
|
const allInnerTransitions = [];
|
|
1595
1595
|
for (const subStateKey of Object.keys(stateValue)) {
|
|
1596
1596
|
const subStateValue = stateValue[subStateKey];
|
|
@@ -1598,29 +1598,29 @@ function transitionParallelNode(stateNode, stateValue, state, event) {
|
|
|
1598
1598
|
continue;
|
|
1599
1599
|
}
|
|
1600
1600
|
const subStateNode = getStateNode(stateNode, subStateKey);
|
|
1601
|
-
const innerTransitions = transitionNode(subStateNode, subStateValue,
|
|
1601
|
+
const innerTransitions = transitionNode(subStateNode, subStateValue, snapshot, event);
|
|
1602
1602
|
if (innerTransitions) {
|
|
1603
1603
|
allInnerTransitions.push(...innerTransitions);
|
|
1604
1604
|
}
|
|
1605
1605
|
}
|
|
1606
1606
|
if (!allInnerTransitions.length) {
|
|
1607
|
-
return stateNode.next(
|
|
1607
|
+
return stateNode.next(snapshot, event);
|
|
1608
1608
|
}
|
|
1609
1609
|
return allInnerTransitions;
|
|
1610
1610
|
}
|
|
1611
|
-
function transitionNode(stateNode, stateValue,
|
|
1611
|
+
function transitionNode(stateNode, stateValue, snapshot, event) {
|
|
1612
1612
|
// leaf node
|
|
1613
1613
|
if (typeof stateValue === 'string') {
|
|
1614
|
-
return transitionAtomicNode(stateNode, stateValue,
|
|
1614
|
+
return transitionAtomicNode(stateNode, stateValue, snapshot, event);
|
|
1615
1615
|
}
|
|
1616
1616
|
|
|
1617
1617
|
// compound node
|
|
1618
1618
|
if (Object.keys(stateValue).length === 1) {
|
|
1619
|
-
return transitionCompoundNode(stateNode, stateValue,
|
|
1619
|
+
return transitionCompoundNode(stateNode, stateValue, snapshot, event);
|
|
1620
1620
|
}
|
|
1621
1621
|
|
|
1622
1622
|
// parallel node
|
|
1623
|
-
return transitionParallelNode(stateNode, stateValue,
|
|
1623
|
+
return transitionParallelNode(stateNode, stateValue, snapshot, event);
|
|
1624
1624
|
}
|
|
1625
1625
|
function getHistoryNodes(stateNode) {
|
|
1626
1626
|
return Object.keys(stateNode.states).map(key => stateNode.states[key]).filter(sn => sn.type === 'history');
|
|
@@ -1752,14 +1752,14 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
|
|
|
1752
1752
|
/**
|
|
1753
1753
|
* https://www.w3.org/TR/scxml/#microstepProcedure
|
|
1754
1754
|
*/
|
|
1755
|
-
function microstep(transitions,
|
|
1755
|
+
function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
|
|
1756
1756
|
if (!transitions.length) {
|
|
1757
|
-
return
|
|
1757
|
+
return currentSnapshot;
|
|
1758
1758
|
}
|
|
1759
|
-
const mutStateNodeSet = new Set(
|
|
1760
|
-
let historyValue =
|
|
1759
|
+
const mutStateNodeSet = new Set(currentSnapshot._nodes);
|
|
1760
|
+
let historyValue = currentSnapshot.historyValue;
|
|
1761
1761
|
const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
|
|
1762
|
-
let nextState =
|
|
1762
|
+
let nextState = currentSnapshot;
|
|
1763
1763
|
|
|
1764
1764
|
// Exit states
|
|
1765
1765
|
if (!isInitial) {
|
|
@@ -1776,7 +1776,7 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
|
|
|
1776
1776
|
nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue);
|
|
1777
1777
|
}
|
|
1778
1778
|
try {
|
|
1779
|
-
if (historyValue ===
|
|
1779
|
+
if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
|
|
1780
1780
|
return nextState;
|
|
1781
1781
|
}
|
|
1782
1782
|
return cloneMachineSnapshot(nextState, {
|
|
@@ -1789,15 +1789,15 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
|
|
|
1789
1789
|
throw e;
|
|
1790
1790
|
}
|
|
1791
1791
|
}
|
|
1792
|
-
function getMachineOutput(
|
|
1792
|
+
function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
|
|
1793
1793
|
if (!rootNode.output) {
|
|
1794
1794
|
return;
|
|
1795
1795
|
}
|
|
1796
|
-
const doneStateEvent = createDoneStateEvent(rootCompletionNode.id, rootCompletionNode.output && rootCompletionNode.parent ? resolveOutput(rootCompletionNode.output,
|
|
1797
|
-
return resolveOutput(rootNode.output,
|
|
1796
|
+
const doneStateEvent = createDoneStateEvent(rootCompletionNode.id, rootCompletionNode.output && rootCompletionNode.parent ? resolveOutput(rootCompletionNode.output, snapshot.context, event, actorScope.self) : undefined);
|
|
1797
|
+
return resolveOutput(rootNode.output, snapshot.context, doneStateEvent, actorScope.self);
|
|
1798
1798
|
}
|
|
1799
|
-
function enterStates(
|
|
1800
|
-
let
|
|
1799
|
+
function enterStates(currentSnapshot, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial) {
|
|
1800
|
+
let nextSnapshot = currentSnapshot;
|
|
1801
1801
|
const statesToEnter = new Set();
|
|
1802
1802
|
// those are states that were directly targeted or indirectly targeted by the explicit target
|
|
1803
1803
|
// in other words, those are states for which initial actions should be executed
|
|
@@ -1807,7 +1807,7 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutSt
|
|
|
1807
1807
|
|
|
1808
1808
|
// In the initial state, the root state node is "entered".
|
|
1809
1809
|
if (isInitial) {
|
|
1810
|
-
statesForDefaultEntry.add(
|
|
1810
|
+
statesForDefaultEntry.add(currentSnapshot.machine.root);
|
|
1811
1811
|
}
|
|
1812
1812
|
const completedNodes = new Set();
|
|
1813
1813
|
for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
|
|
@@ -1826,13 +1826,13 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutSt
|
|
|
1826
1826
|
const initialActions = stateNodeToEnter.initial.actions;
|
|
1827
1827
|
actions.push(...initialActions);
|
|
1828
1828
|
}
|
|
1829
|
-
|
|
1829
|
+
nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, actions, internalQueue, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
|
|
1830
1830
|
if (stateNodeToEnter.type === 'final') {
|
|
1831
1831
|
const parent = stateNodeToEnter.parent;
|
|
1832
1832
|
let ancestorMarker = parent?.type === 'parallel' ? parent : parent?.parent;
|
|
1833
1833
|
let rootCompletionNode = ancestorMarker || stateNodeToEnter;
|
|
1834
1834
|
if (parent?.type === 'compound') {
|
|
1835
|
-
internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output,
|
|
1835
|
+
internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output, nextSnapshot.context, event, actorScope.self) : undefined));
|
|
1836
1836
|
}
|
|
1837
1837
|
while (ancestorMarker?.type === 'parallel' && !completedNodes.has(ancestorMarker) && isInFinalState(mutStateNodeSet, ancestorMarker)) {
|
|
1838
1838
|
completedNodes.add(ancestorMarker);
|
|
@@ -1843,13 +1843,13 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutSt
|
|
|
1843
1843
|
if (ancestorMarker) {
|
|
1844
1844
|
continue;
|
|
1845
1845
|
}
|
|
1846
|
-
|
|
1846
|
+
nextSnapshot = cloneMachineSnapshot(nextSnapshot, {
|
|
1847
1847
|
status: 'done',
|
|
1848
|
-
output: getMachineOutput(
|
|
1848
|
+
output: getMachineOutput(nextSnapshot, event, actorScope, nextSnapshot.machine.root, rootCompletionNode)
|
|
1849
1849
|
});
|
|
1850
1850
|
}
|
|
1851
1851
|
}
|
|
1852
|
-
return
|
|
1852
|
+
return nextSnapshot;
|
|
1853
1853
|
}
|
|
1854
1854
|
function computeEntrySet(transitions, historyValue, statesForDefaultEntry, statesToEnter) {
|
|
1855
1855
|
for (const t of transitions) {
|
|
@@ -1944,8 +1944,8 @@ function addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultE
|
|
|
1944
1944
|
function addProperAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, historyValue, statesForDefaultEntry) {
|
|
1945
1945
|
addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, getProperAncestors(stateNode, toStateNode));
|
|
1946
1946
|
}
|
|
1947
|
-
function exitStates(
|
|
1948
|
-
let
|
|
1947
|
+
function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNodeSet, historyValue, internalQueue) {
|
|
1948
|
+
let nextSnapshot = currentSnapshot;
|
|
1949
1949
|
const statesToExit = computeExitSet(transitions, mutStateNodeSet, historyValue);
|
|
1950
1950
|
statesToExit.sort((a, b) => b.order - a.order);
|
|
1951
1951
|
let changedHistory;
|
|
@@ -1968,16 +1968,16 @@ function exitStates(currentState, event, actorScope, transitions, mutStateNodeSe
|
|
|
1968
1968
|
}
|
|
1969
1969
|
}
|
|
1970
1970
|
for (const s of statesToExit) {
|
|
1971
|
-
|
|
1971
|
+
nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, [...s.exit, ...s.invoke.map(def => stopChild(def.id))], internalQueue);
|
|
1972
1972
|
mutStateNodeSet.delete(s);
|
|
1973
1973
|
}
|
|
1974
|
-
return [
|
|
1974
|
+
return [nextSnapshot, changedHistory || historyValue];
|
|
1975
1975
|
}
|
|
1976
|
-
function resolveActionsAndContextWorker(
|
|
1976
|
+
function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, actions, extra, retries) {
|
|
1977
1977
|
const {
|
|
1978
1978
|
machine
|
|
1979
|
-
} =
|
|
1980
|
-
let
|
|
1979
|
+
} = currentSnapshot;
|
|
1980
|
+
let intermediateSnapshot = currentSnapshot;
|
|
1981
1981
|
for (const action of actions) {
|
|
1982
1982
|
const isInline = typeof action === 'function';
|
|
1983
1983
|
const resolvedAction = isInline ? action :
|
|
@@ -1989,13 +1989,13 @@ function resolveActionsAndContextWorker(currentState, event, actorScope, actions
|
|
|
1989
1989
|
continue;
|
|
1990
1990
|
}
|
|
1991
1991
|
const actionArgs = {
|
|
1992
|
-
context:
|
|
1992
|
+
context: intermediateSnapshot.context,
|
|
1993
1993
|
event,
|
|
1994
1994
|
self: actorScope?.self,
|
|
1995
1995
|
system: actorScope?.system
|
|
1996
1996
|
};
|
|
1997
1997
|
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
|
|
1998
|
-
context:
|
|
1998
|
+
context: intermediateSnapshot.context,
|
|
1999
1999
|
event
|
|
2000
2000
|
}) : action.params : undefined;
|
|
2001
2001
|
if (!('resolve' in resolvedAction)) {
|
|
@@ -2009,10 +2009,10 @@ function resolveActionsAndContextWorker(currentState, event, actorScope, actions
|
|
|
2009
2009
|
continue;
|
|
2010
2010
|
}
|
|
2011
2011
|
const builtinAction = resolvedAction;
|
|
2012
|
-
const [nextState, params, actions] = builtinAction.resolve(actorScope,
|
|
2012
|
+
const [nextState, params, actions] = builtinAction.resolve(actorScope, intermediateSnapshot, actionArgs, actionParams, resolvedAction,
|
|
2013
2013
|
// this holds all params
|
|
2014
2014
|
extra);
|
|
2015
|
-
|
|
2015
|
+
intermediateSnapshot = nextState;
|
|
2016
2016
|
if ('retryResolve' in builtinAction) {
|
|
2017
2017
|
retries?.push([builtinAction, params]);
|
|
2018
2018
|
}
|
|
@@ -2024,14 +2024,14 @@ function resolveActionsAndContextWorker(currentState, event, actorScope, actions
|
|
|
2024
2024
|
}
|
|
2025
2025
|
}
|
|
2026
2026
|
if (actions) {
|
|
2027
|
-
|
|
2027
|
+
intermediateSnapshot = resolveActionsAndContextWorker(intermediateSnapshot, event, actorScope, actions, extra, retries);
|
|
2028
2028
|
}
|
|
2029
2029
|
}
|
|
2030
|
-
return
|
|
2030
|
+
return intermediateSnapshot;
|
|
2031
2031
|
}
|
|
2032
|
-
function resolveActionsAndContext(
|
|
2032
|
+
function resolveActionsAndContext(currentSnapshot, event, actorScope, actions, internalQueue, deferredActorIds) {
|
|
2033
2033
|
const retries = deferredActorIds ? [] : undefined;
|
|
2034
|
-
const nextState = resolveActionsAndContextWorker(
|
|
2034
|
+
const nextState = resolveActionsAndContextWorker(currentSnapshot, event, actorScope, actions, {
|
|
2035
2035
|
internalQueue,
|
|
2036
2036
|
deferredActorIds
|
|
2037
2037
|
}, retries);
|
|
@@ -2040,18 +2040,18 @@ function resolveActionsAndContext(currentState, event, actorScope, actions, inte
|
|
|
2040
2040
|
});
|
|
2041
2041
|
return nextState;
|
|
2042
2042
|
}
|
|
2043
|
-
function macrostep(
|
|
2044
|
-
let
|
|
2043
|
+
function macrostep(snapshot, event, actorScope, internalQueue = []) {
|
|
2044
|
+
let nextSnapshot = snapshot;
|
|
2045
2045
|
const states = [];
|
|
2046
2046
|
|
|
2047
2047
|
// Handle stop event
|
|
2048
2048
|
if (event.type === XSTATE_STOP) {
|
|
2049
|
-
|
|
2049
|
+
nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
|
|
2050
2050
|
status: 'stopped'
|
|
2051
2051
|
});
|
|
2052
|
-
states.push(
|
|
2052
|
+
states.push(nextSnapshot);
|
|
2053
2053
|
return {
|
|
2054
|
-
|
|
2054
|
+
snapshot: nextSnapshot,
|
|
2055
2055
|
microstates: states
|
|
2056
2056
|
};
|
|
2057
2057
|
}
|
|
@@ -2062,47 +2062,47 @@ function macrostep(state, event, actorScope, internalQueue = []) {
|
|
|
2062
2062
|
if (nextEvent.type !== XSTATE_INIT) {
|
|
2063
2063
|
const currentEvent = nextEvent;
|
|
2064
2064
|
const isErr = isErrorActorEvent(currentEvent);
|
|
2065
|
-
const transitions = selectTransitions(currentEvent,
|
|
2065
|
+
const transitions = selectTransitions(currentEvent, nextSnapshot);
|
|
2066
2066
|
if (isErr && !transitions.length) {
|
|
2067
2067
|
// TODO: we should likely only allow transitions selected by very explicit descriptors
|
|
2068
2068
|
// `*` shouldn't be matched, likely `xstate.error.*` shouldnt be either
|
|
2069
2069
|
// similarly `xstate.error.actor.*` and `xstate.error.actor.todo.*` have to be considered too
|
|
2070
|
-
|
|
2070
|
+
nextSnapshot = cloneMachineSnapshot(snapshot, {
|
|
2071
2071
|
status: 'error',
|
|
2072
|
-
error: currentEvent.
|
|
2072
|
+
error: currentEvent.error
|
|
2073
2073
|
});
|
|
2074
|
-
states.push(
|
|
2074
|
+
states.push(nextSnapshot);
|
|
2075
2075
|
return {
|
|
2076
|
-
|
|
2076
|
+
snapshot: nextSnapshot,
|
|
2077
2077
|
microstates: states
|
|
2078
2078
|
};
|
|
2079
2079
|
}
|
|
2080
|
-
|
|
2081
|
-
states.push(
|
|
2080
|
+
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false, internalQueue);
|
|
2081
|
+
states.push(nextSnapshot);
|
|
2082
2082
|
}
|
|
2083
2083
|
let shouldSelectEventlessTransitions = true;
|
|
2084
|
-
while (
|
|
2085
|
-
let enabledTransitions = shouldSelectEventlessTransitions ? selectEventlessTransitions(
|
|
2084
|
+
while (nextSnapshot.status === 'active') {
|
|
2085
|
+
let enabledTransitions = shouldSelectEventlessTransitions ? selectEventlessTransitions(nextSnapshot, nextEvent) : [];
|
|
2086
2086
|
|
|
2087
2087
|
// eventless transitions should always be selected after selecting *regular* transitions
|
|
2088
2088
|
// by assigning `undefined` to `previousState` we ensure that `shouldSelectEventlessTransitions` gets always computed to true in such a case
|
|
2089
|
-
const previousState = enabledTransitions.length ?
|
|
2089
|
+
const previousState = enabledTransitions.length ? nextSnapshot : undefined;
|
|
2090
2090
|
if (!enabledTransitions.length) {
|
|
2091
2091
|
if (!internalQueue.length) {
|
|
2092
2092
|
break;
|
|
2093
2093
|
}
|
|
2094
2094
|
nextEvent = internalQueue.shift();
|
|
2095
|
-
enabledTransitions = selectTransitions(nextEvent,
|
|
2095
|
+
enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
|
|
2096
2096
|
}
|
|
2097
|
-
|
|
2098
|
-
shouldSelectEventlessTransitions =
|
|
2099
|
-
states.push(
|
|
2097
|
+
nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
|
|
2098
|
+
shouldSelectEventlessTransitions = nextSnapshot !== previousState;
|
|
2099
|
+
states.push(nextSnapshot);
|
|
2100
2100
|
}
|
|
2101
|
-
if (
|
|
2102
|
-
stopChildren(
|
|
2101
|
+
if (nextSnapshot.status !== 'active') {
|
|
2102
|
+
stopChildren(nextSnapshot, nextEvent, actorScope);
|
|
2103
2103
|
}
|
|
2104
2104
|
return {
|
|
2105
|
-
|
|
2105
|
+
snapshot: nextSnapshot,
|
|
2106
2106
|
microstates: states
|
|
2107
2107
|
};
|
|
2108
2108
|
}
|
|
@@ -2200,13 +2200,13 @@ function createMachineSnapshot(config, machine) {
|
|
|
2200
2200
|
toJSON: machineSnapshotToJSON
|
|
2201
2201
|
};
|
|
2202
2202
|
}
|
|
2203
|
-
function cloneMachineSnapshot(
|
|
2203
|
+
function cloneMachineSnapshot(snapshot, config = {}) {
|
|
2204
2204
|
return createMachineSnapshot({
|
|
2205
|
-
...
|
|
2205
|
+
...snapshot,
|
|
2206
2206
|
...config
|
|
2207
|
-
},
|
|
2207
|
+
}, snapshot.machine);
|
|
2208
2208
|
}
|
|
2209
|
-
function
|
|
2209
|
+
function getPersistedSnapshot(snapshot, options) {
|
|
2210
2210
|
const {
|
|
2211
2211
|
_nodes: nodes,
|
|
2212
2212
|
tags,
|
|
@@ -2219,12 +2219,12 @@ function getPersistedState(state, options) {
|
|
|
2219
2219
|
getMeta,
|
|
2220
2220
|
toJSON,
|
|
2221
2221
|
...jsonValues
|
|
2222
|
-
} =
|
|
2222
|
+
} = snapshot;
|
|
2223
2223
|
const childrenJson = {};
|
|
2224
2224
|
for (const id in children) {
|
|
2225
2225
|
const child = children[id];
|
|
2226
2226
|
childrenJson[id] = {
|
|
2227
|
-
|
|
2227
|
+
snapshot: child.getPersistedSnapshot(options),
|
|
2228
2228
|
src: child.src,
|
|
2229
2229
|
systemId: child._systemId,
|
|
2230
2230
|
syncSnapshot: child._syncSnapshot
|
|
@@ -2264,14 +2264,14 @@ function persistContext(contextPart) {
|
|
|
2264
2264
|
return copy ?? contextPart;
|
|
2265
2265
|
}
|
|
2266
2266
|
|
|
2267
|
-
function resolveRaise(_,
|
|
2267
|
+
function resolveRaise(_, snapshot, args, actionParams, {
|
|
2268
2268
|
event: eventOrExpr,
|
|
2269
2269
|
id,
|
|
2270
2270
|
delay
|
|
2271
2271
|
}, {
|
|
2272
2272
|
internalQueue
|
|
2273
2273
|
}) {
|
|
2274
|
-
const delaysMap =
|
|
2274
|
+
const delaysMap = snapshot.machine.implementations.delays;
|
|
2275
2275
|
if (typeof eventOrExpr === 'string') {
|
|
2276
2276
|
throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
|
|
2277
2277
|
}
|
|
@@ -2286,7 +2286,7 @@ function resolveRaise(_, state, args, actionParams, {
|
|
|
2286
2286
|
if (typeof resolvedDelay !== 'number') {
|
|
2287
2287
|
internalQueue.push(resolvedEvent);
|
|
2288
2288
|
}
|
|
2289
|
-
return [
|
|
2289
|
+
return [snapshot, {
|
|
2290
2290
|
event: resolvedEvent,
|
|
2291
2291
|
id,
|
|
2292
2292
|
delay: resolvedDelay
|
|
@@ -2340,7 +2340,7 @@ exports.getAllStateNodes = getAllStateNodes;
|
|
|
2340
2340
|
exports.getCandidates = getCandidates;
|
|
2341
2341
|
exports.getDelayedTransitions = getDelayedTransitions;
|
|
2342
2342
|
exports.getInitialStateNodes = getInitialStateNodes;
|
|
2343
|
-
exports.
|
|
2343
|
+
exports.getPersistedSnapshot = getPersistedSnapshot;
|
|
2344
2344
|
exports.getStateNodeByPath = getStateNodeByPath;
|
|
2345
2345
|
exports.getStateNodes = getStateNodes;
|
|
2346
2346
|
exports.interpret = interpret;
|