xstate 5.0.0-beta.33 → 5.0.0-beta.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/README.md +1 -1
- package/actions/dist/xstate-actions.cjs.js +3 -3
- package/actions/dist/xstate-actions.development.cjs.js +3 -3
- package/actions/dist/xstate-actions.development.esm.js +3 -3
- package/actions/dist/xstate-actions.esm.js +3 -3
- 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 +3 -3
- package/actors/dist/xstate-actors.development.cjs.js +3 -3
- package/actors/dist/xstate-actors.development.esm.js +3 -3
- package/actors/dist/xstate-actors.esm.js +3 -3
- 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/index.d.ts +1 -0
- package/dist/declarations/src/interpreter.d.ts +3 -1
- package/dist/declarations/src/stateUtils.d.ts +6 -5
- package/dist/declarations/src/types.d.ts +3 -5
- package/dist/{interpreter-825f3d6e.development.esm.js → interpreter-5c4e6634.development.esm.js} +67 -65
- package/dist/{interpreter-054e9fb7.cjs.js → interpreter-69605bf0.cjs.js} +67 -65
- package/dist/{interpreter-0c630f66.development.cjs.js → interpreter-d3567419.development.cjs.js} +67 -65
- package/dist/{interpreter-03737810.esm.js → interpreter-de5217bc.esm.js} +67 -65
- package/dist/{raise-987c242e.cjs.js → raise-0ff57677.cjs.js} +142 -124
- package/dist/{raise-4c6a5a96.development.cjs.js → raise-26e4d83c.development.cjs.js} +145 -124
- package/dist/{raise-2d92eae8.esm.js → raise-511399cc.esm.js} +142 -124
- package/dist/{raise-46f122aa.development.esm.js → raise-cdcdf834.development.esm.js} +145 -124
- package/dist/{send-bd1bd0e3.cjs.js → send-19ffc568.cjs.js} +16 -8
- package/dist/{send-4d5b92dc.development.esm.js → send-1de74f4d.development.esm.js} +16 -8
- package/dist/{send-0edee2b4.esm.js → send-211a2a94.esm.js} +16 -8
- package/dist/{send-0e8675c8.development.cjs.js → send-894c4b18.development.cjs.js} +16 -8
- package/dist/xstate.cjs.js +28 -9
- package/dist/xstate.development.cjs.js +28 -9
- package/dist/xstate.development.esm.js +31 -12
- package/dist/xstate.esm.js +31 -12
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +2 -2
- package/guards/dist/xstate-guards.development.cjs.js +2 -2
- package/guards/dist/xstate-guards.development.esm.js +2 -2
- package/guards/dist/xstate-guards.esm.js +2 -2
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { r as resolveReferencedActor, d as createActor, f as ActorStatus,
|
|
1
|
+
import { r as resolveReferencedActor, d as createActor, f as ActorStatus, j as createErrorActorEvent, k as toStateValue, l as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, o as toStatePath, q as createDoneStateEvent, s as resolveOutput, X as XSTATE_STOP, u as XSTATE_INIT, W as WILDCARD, v as createAfterEvent, w as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-de5217bc.esm.js';
|
|
2
2
|
|
|
3
3
|
const cache = new WeakMap();
|
|
4
4
|
function memo(object, key, fn) {
|
|
@@ -277,20 +277,6 @@ function evaluateGuard(guard, context, event, state) {
|
|
|
277
277
|
);
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
-
function getOutput(configuration, context, event, self) {
|
|
281
|
-
const {
|
|
282
|
-
machine
|
|
283
|
-
} = configuration[0];
|
|
284
|
-
const {
|
|
285
|
-
root
|
|
286
|
-
} = machine;
|
|
287
|
-
if (!root.output) {
|
|
288
|
-
return undefined;
|
|
289
|
-
}
|
|
290
|
-
const finalChildStateNode = configuration.find(stateNode => stateNode.type === 'final' && stateNode.parent === machine.root);
|
|
291
|
-
const doneStateEvent = createDoneStateEvent(finalChildStateNode.id, finalChildStateNode.output ? resolveOutput(finalChildStateNode.output, context, event, self) : undefined);
|
|
292
|
-
return resolveOutput(root.output, context, doneStateEvent, self);
|
|
293
|
-
}
|
|
294
280
|
const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
|
|
295
281
|
function getChildren(stateNode) {
|
|
296
282
|
return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
|
|
@@ -315,7 +301,7 @@ function getConfiguration(stateNodes) {
|
|
|
315
301
|
for (const s of configuration) {
|
|
316
302
|
// if previously active, add existing child nodes
|
|
317
303
|
if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
|
|
318
|
-
|
|
304
|
+
getInitialStateNodesWithTheirAncestors(s).forEach(sn => configurationSet.add(sn));
|
|
319
305
|
} else {
|
|
320
306
|
if (s.type === 'parallel') {
|
|
321
307
|
for (const child of getChildren(s)) {
|
|
@@ -323,7 +309,8 @@ function getConfiguration(stateNodes) {
|
|
|
323
309
|
continue;
|
|
324
310
|
}
|
|
325
311
|
if (!configurationSet.has(child)) {
|
|
326
|
-
|
|
312
|
+
const initialStates = getInitialStateNodesWithTheirAncestors(child);
|
|
313
|
+
for (const initialStateNode of initialStates) {
|
|
327
314
|
configurationSet.add(initialStateNode);
|
|
328
315
|
}
|
|
329
316
|
}
|
|
@@ -383,14 +370,14 @@ function getStateValue(rootNode, configuration) {
|
|
|
383
370
|
const config = getConfiguration(configuration);
|
|
384
371
|
return getValueFromAdj(rootNode, getAdjList(config));
|
|
385
372
|
}
|
|
386
|
-
function isInFinalState(configuration, stateNode
|
|
373
|
+
function isInFinalState(configuration, stateNode) {
|
|
387
374
|
if (stateNode.type === 'compound') {
|
|
388
|
-
return getChildren(stateNode).some(s => s.type === 'final' && configuration.
|
|
375
|
+
return getChildren(stateNode).some(s => s.type === 'final' && configuration.has(s));
|
|
389
376
|
}
|
|
390
377
|
if (stateNode.type === 'parallel') {
|
|
391
378
|
return getChildren(stateNode).every(sn => isInFinalState(configuration, sn));
|
|
392
379
|
}
|
|
393
|
-
return
|
|
380
|
+
return stateNode.type === 'final';
|
|
394
381
|
}
|
|
395
382
|
const isStateId = str => str[0] === STATE_IDENTIFIER;
|
|
396
383
|
function getCandidates(stateNode, receivedEventType) {
|
|
@@ -440,13 +427,7 @@ function getDelayedTransitions(stateNode) {
|
|
|
440
427
|
stateNode.exit.push(cancel(eventType));
|
|
441
428
|
return eventType;
|
|
442
429
|
};
|
|
443
|
-
const delayedTransitions =
|
|
444
|
-
const eventType = mutateEntryExit(transition.delay, i);
|
|
445
|
-
return {
|
|
446
|
-
...transition,
|
|
447
|
-
event: eventType
|
|
448
|
-
};
|
|
449
|
-
}) : Object.keys(afterConfig).flatMap((delay, i) => {
|
|
430
|
+
const delayedTransitions = Object.keys(afterConfig).flatMap((delay, i) => {
|
|
450
431
|
const configTransition = afterConfig[delay];
|
|
451
432
|
const resolvedTransition = typeof configTransition === 'string' ? {
|
|
452
433
|
target: configTransition
|
|
@@ -529,43 +510,23 @@ function formatTransitions(stateNode) {
|
|
|
529
510
|
return transitions;
|
|
530
511
|
}
|
|
531
512
|
function formatInitialTransition(stateNode, _target) {
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
// to their state node
|
|
536
|
-
const descStateNode = typeof t === 'string' ? isStateId(t) ? stateNode.machine.getStateNodeById(t) : stateNode.states[t] : t;
|
|
537
|
-
if (!descStateNode) {
|
|
538
|
-
throw new Error(`Initial state node "${t}" not found on parent state node #${stateNode.id}`);
|
|
539
|
-
}
|
|
540
|
-
if (!isDescendant(descStateNode, stateNode)) {
|
|
541
|
-
throw new Error(`Invalid initial target: state node #${descStateNode.id} is not a descendant of #${stateNode.id}`);
|
|
542
|
-
}
|
|
543
|
-
return descStateNode;
|
|
544
|
-
});
|
|
545
|
-
const resolvedTarget = resolveTarget(stateNode, targets);
|
|
546
|
-
const transition = {
|
|
547
|
-
source: stateNode,
|
|
548
|
-
actions: [],
|
|
549
|
-
eventType: null,
|
|
550
|
-
reenter: false,
|
|
551
|
-
target: resolvedTarget,
|
|
552
|
-
toJSON: () => ({
|
|
553
|
-
...transition,
|
|
554
|
-
source: `#${stateNode.id}`,
|
|
555
|
-
target: resolvedTarget ? resolvedTarget.map(t => `#${t.id}`) : undefined
|
|
556
|
-
})
|
|
557
|
-
};
|
|
558
|
-
return transition;
|
|
513
|
+
const resolvedTarget = typeof _target === 'string' ? stateNode.states[_target] : _target ? stateNode.states[_target.target] : undefined;
|
|
514
|
+
if (!resolvedTarget && _target) {
|
|
515
|
+
throw new Error(`Initial state node "${_target}" not found on parent state node #${stateNode.id}`);
|
|
559
516
|
}
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
517
|
+
const transition = {
|
|
518
|
+
source: stateNode,
|
|
519
|
+
actions: !_target || typeof _target === 'string' ? [] : toArray(_target.actions),
|
|
520
|
+
eventType: null,
|
|
521
|
+
reenter: false,
|
|
522
|
+
target: resolvedTarget ? [resolvedTarget] : [],
|
|
523
|
+
toJSON: () => ({
|
|
524
|
+
...transition,
|
|
525
|
+
source: `#${stateNode.id}`,
|
|
526
|
+
target: resolvedTarget ? [`#${resolvedTarget.id}`] : []
|
|
527
|
+
})
|
|
528
|
+
};
|
|
529
|
+
return transition;
|
|
569
530
|
}
|
|
570
531
|
function resolveTarget(stateNode, targets) {
|
|
571
532
|
if (targets === undefined) {
|
|
@@ -608,6 +569,15 @@ function resolveHistoryTarget(stateNode) {
|
|
|
608
569
|
function isHistoryNode(stateNode) {
|
|
609
570
|
return stateNode.type === 'history';
|
|
610
571
|
}
|
|
572
|
+
function getInitialStateNodesWithTheirAncestors(stateNode) {
|
|
573
|
+
const states = getInitialStateNodes(stateNode);
|
|
574
|
+
for (const initialState of states) {
|
|
575
|
+
for (const ancestor of getProperAncestors(initialState, stateNode)) {
|
|
576
|
+
states.add(ancestor);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return states;
|
|
580
|
+
}
|
|
611
581
|
function getInitialStateNodes(stateNode) {
|
|
612
582
|
const set = new Set();
|
|
613
583
|
function iter(descStateNode) {
|
|
@@ -616,12 +586,7 @@ function getInitialStateNodes(stateNode) {
|
|
|
616
586
|
}
|
|
617
587
|
set.add(descStateNode);
|
|
618
588
|
if (descStateNode.type === 'compound') {
|
|
619
|
-
|
|
620
|
-
for (const a of getProperAncestors(targetStateNode, stateNode)) {
|
|
621
|
-
set.add(a);
|
|
622
|
-
}
|
|
623
|
-
iter(targetStateNode);
|
|
624
|
-
}
|
|
589
|
+
iter(descStateNode.initial.target[0]);
|
|
625
590
|
} else if (descStateNode.type === 'parallel') {
|
|
626
591
|
for (const child of getChildren(descStateNode)) {
|
|
627
592
|
iter(child);
|
|
@@ -629,7 +594,7 @@ function getInitialStateNodes(stateNode) {
|
|
|
629
594
|
}
|
|
630
595
|
}
|
|
631
596
|
iter(stateNode);
|
|
632
|
-
return
|
|
597
|
+
return set;
|
|
633
598
|
}
|
|
634
599
|
/**
|
|
635
600
|
* Returns the child state node from its relative `stateKey`, or throws.
|
|
@@ -885,41 +850,37 @@ function microstep(transitions, currentState, actorCtx, event, isInitial) {
|
|
|
885
850
|
}
|
|
886
851
|
|
|
887
852
|
function microstepProcedure(transitions, currentState, mutConfiguration, event, actorCtx, isInitial) {
|
|
888
|
-
const actions = [];
|
|
889
853
|
const historyValue = {
|
|
890
854
|
...currentState.historyValue
|
|
891
855
|
};
|
|
892
856
|
const filteredTransitions = removeConflictingTransitions(transitions, mutConfiguration, historyValue);
|
|
893
857
|
const internalQueue = [...currentState._internalQueue];
|
|
858
|
+
// TODO: this `cloneState` is really just a hack to prevent infinite loops
|
|
859
|
+
// we need to take another look at how internal queue is managed
|
|
860
|
+
let nextState = cloneState(currentState, {
|
|
861
|
+
_internalQueue: []
|
|
862
|
+
});
|
|
894
863
|
|
|
895
864
|
// Exit states
|
|
896
865
|
if (!isInitial) {
|
|
897
|
-
exitStates(filteredTransitions, mutConfiguration, historyValue
|
|
866
|
+
nextState = exitStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, historyValue);
|
|
898
867
|
}
|
|
899
868
|
|
|
900
869
|
// Execute transition content
|
|
901
|
-
|
|
870
|
+
nextState = resolveActionsAndContext(nextState, event, actorCtx, filteredTransitions.flatMap(t => t.actions));
|
|
902
871
|
|
|
903
872
|
// Enter states
|
|
904
|
-
enterStates(event, filteredTransitions, mutConfiguration,
|
|
873
|
+
nextState = enterStates(nextState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial);
|
|
905
874
|
const nextConfiguration = [...mutConfiguration];
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
const finalActions = nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit);
|
|
909
|
-
actions.push(...finalActions);
|
|
875
|
+
if (nextState.status === 'done') {
|
|
876
|
+
nextState = resolveActionsAndContext(nextState, event, actorCtx, nextConfiguration.sort((a, b) => b.order - a.order).flatMap(state => state.exit));
|
|
910
877
|
}
|
|
911
878
|
try {
|
|
912
|
-
const nextState = resolveActionsAndContext(actions, event, currentState, actorCtx);
|
|
913
|
-
const output = done ? getOutput(nextConfiguration, nextState.context, event, actorCtx.self) : undefined;
|
|
914
879
|
internalQueue.push(...nextState._internalQueue);
|
|
915
|
-
return cloneState(
|
|
880
|
+
return cloneState(nextState, {
|
|
916
881
|
configuration: nextConfiguration,
|
|
917
882
|
historyValue,
|
|
918
|
-
_internalQueue: internalQueue
|
|
919
|
-
context: nextState.context,
|
|
920
|
-
status: done ? 'done' : currentState.status,
|
|
921
|
-
output,
|
|
922
|
-
children: nextState.children
|
|
883
|
+
_internalQueue: internalQueue
|
|
923
884
|
});
|
|
924
885
|
} catch (e) {
|
|
925
886
|
// TODO: Refactor this once proper error handling is implemented.
|
|
@@ -927,7 +888,15 @@ function microstepProcedure(transitions, currentState, mutConfiguration, event,
|
|
|
927
888
|
throw e;
|
|
928
889
|
}
|
|
929
890
|
}
|
|
930
|
-
function
|
|
891
|
+
function getMachineOutput(state, event, actorCtx, rootNode, rootCompletionNode) {
|
|
892
|
+
if (!rootNode.output) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
const doneStateEvent = createDoneStateEvent(rootCompletionNode.id, rootCompletionNode.output && rootCompletionNode.parent ? resolveOutput(rootCompletionNode.output, state.context, event, actorCtx.self) : undefined);
|
|
896
|
+
return resolveOutput(rootNode.output, state.context, doneStateEvent, actorCtx.self);
|
|
897
|
+
}
|
|
898
|
+
function enterStates(currentState, event, actorCtx, filteredTransitions, mutConfiguration, internalQueue, historyValue, isInitial) {
|
|
899
|
+
let nextState = currentState;
|
|
931
900
|
const statesToEnter = new Set();
|
|
932
901
|
const statesForDefaultEntry = new Set();
|
|
933
902
|
computeEntrySet(filteredTransitions, historyValue, statesForDefaultEntry, statesToEnter);
|
|
@@ -936,36 +905,48 @@ function enterStates(event, filteredTransitions, mutConfiguration, actions, inte
|
|
|
936
905
|
if (isInitial) {
|
|
937
906
|
statesForDefaultEntry.add(currentState.machine.root);
|
|
938
907
|
}
|
|
908
|
+
const completedNodes = new Set();
|
|
939
909
|
for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
|
|
940
910
|
mutConfiguration.add(stateNodeToEnter);
|
|
941
|
-
|
|
942
|
-
actions.push(invoke(invokeDef));
|
|
943
|
-
}
|
|
911
|
+
const actions = [];
|
|
944
912
|
|
|
945
913
|
// Add entry actions
|
|
946
914
|
actions.push(...stateNodeToEnter.entry);
|
|
915
|
+
for (const invokeDef of stateNodeToEnter.invoke) {
|
|
916
|
+
actions.push(invoke(invokeDef));
|
|
917
|
+
}
|
|
947
918
|
if (statesForDefaultEntry.has(stateNodeToEnter)) {
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
actions.push(...initialActions);
|
|
951
|
-
}
|
|
919
|
+
const initialActions = stateNodeToEnter.initial.actions;
|
|
920
|
+
actions.push(...initialActions);
|
|
952
921
|
}
|
|
922
|
+
nextState = resolveActionsAndContext(nextState, event, actorCtx, actions, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
|
|
953
923
|
if (stateNodeToEnter.type === 'final') {
|
|
954
924
|
const parent = stateNodeToEnter.parent;
|
|
955
|
-
if (
|
|
925
|
+
if (completedNodes.has(parent)) {
|
|
956
926
|
continue;
|
|
957
927
|
}
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
928
|
+
completedNodes.add(parent);
|
|
929
|
+
let rootCompletionNode = parent?.type === 'parallel' ? parent : stateNodeToEnter;
|
|
930
|
+
let ancestorMarker = parent?.parent;
|
|
931
|
+
if (ancestorMarker) {
|
|
932
|
+
internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output ? resolveOutput(stateNodeToEnter.output, nextState.context, event, actorCtx.self) : undefined));
|
|
933
|
+
while (ancestorMarker?.type === 'parallel' && !completedNodes.has(ancestorMarker) && isInFinalState(mutConfiguration, ancestorMarker)) {
|
|
934
|
+
completedNodes.add(ancestorMarker);
|
|
935
|
+
internalQueue.push(createDoneStateEvent(ancestorMarker.id));
|
|
936
|
+
rootCompletionNode = ancestorMarker;
|
|
937
|
+
ancestorMarker = ancestorMarker.parent;
|
|
965
938
|
}
|
|
966
939
|
}
|
|
940
|
+
if (ancestorMarker) {
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
nextState = cloneState(nextState, {
|
|
944
|
+
status: 'done',
|
|
945
|
+
output: getMachineOutput(nextState, event, actorCtx, currentState.configuration[0].machine.root, rootCompletionNode)
|
|
946
|
+
});
|
|
967
947
|
}
|
|
968
948
|
}
|
|
949
|
+
return nextState;
|
|
969
950
|
}
|
|
970
951
|
function computeEntrySet(transitions, historyValue, statesForDefaultEntry, statesToEnter) {
|
|
971
952
|
for (const t of transitions) {
|
|
@@ -1008,13 +989,9 @@ function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEnt
|
|
|
1008
989
|
statesToEnter.add(stateNode);
|
|
1009
990
|
if (stateNode.type === 'compound') {
|
|
1010
991
|
statesForDefaultEntry.add(stateNode);
|
|
1011
|
-
const
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
}
|
|
1015
|
-
for (const initialState of initialStates) {
|
|
1016
|
-
addAncestorStatesToEnter(initialState, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
|
|
1017
|
-
}
|
|
992
|
+
const [initialState] = stateNode.initial.target;
|
|
993
|
+
addDescendantStatesToEnter(initialState, historyValue, statesForDefaultEntry, statesToEnter);
|
|
994
|
+
addAncestorStatesToEnter(initialState, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
|
|
1018
995
|
} else {
|
|
1019
996
|
if (stateNode.type === 'parallel') {
|
|
1020
997
|
for (const child of getChildren(stateNode).filter(sn => !isHistoryNode(sn))) {
|
|
@@ -1039,7 +1016,8 @@ function addAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, history
|
|
|
1039
1016
|
}
|
|
1040
1017
|
}
|
|
1041
1018
|
}
|
|
1042
|
-
function exitStates(transitions, mutConfiguration, historyValue
|
|
1019
|
+
function exitStates(currentState, event, actorCtx, transitions, mutConfiguration, historyValue) {
|
|
1020
|
+
let nextState = currentState;
|
|
1043
1021
|
const statesToExit = computeExitSet(transitions, mutConfiguration, historyValue);
|
|
1044
1022
|
statesToExit.sort((a, b) => b.order - a.order);
|
|
1045
1023
|
|
|
@@ -1058,19 +1036,16 @@ function exitStates(transitions, mutConfiguration, historyValue, actions) {
|
|
|
1058
1036
|
}
|
|
1059
1037
|
}
|
|
1060
1038
|
for (const s of statesToExit) {
|
|
1061
|
-
|
|
1039
|
+
nextState = resolveActionsAndContext(nextState, event, actorCtx, [...s.exit, ...s.invoke.map(def => stop(def.id))]);
|
|
1062
1040
|
mutConfiguration.delete(s);
|
|
1063
1041
|
}
|
|
1042
|
+
return nextState;
|
|
1064
1043
|
}
|
|
1065
|
-
function
|
|
1044
|
+
function resolveActionsAndContextWorker(currentState, event, actorCtx, actions, extra, retries) {
|
|
1066
1045
|
const {
|
|
1067
1046
|
machine
|
|
1068
1047
|
} = currentState;
|
|
1069
|
-
|
|
1070
|
-
// we need to take another look at how internal queue is managed
|
|
1071
|
-
let intermediateState = cloneState(currentState, {
|
|
1072
|
-
_internalQueue: []
|
|
1073
|
-
});
|
|
1048
|
+
let intermediateState = currentState;
|
|
1074
1049
|
for (const action of actions) {
|
|
1075
1050
|
const isInline = typeof action === 'function';
|
|
1076
1051
|
const resolvedAction = isInline ? action :
|
|
@@ -1109,11 +1084,14 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
|
|
|
1109
1084
|
continue;
|
|
1110
1085
|
}
|
|
1111
1086
|
const builtinAction = resolvedAction;
|
|
1112
|
-
const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction
|
|
1113
|
-
|
|
1114
|
-
|
|
1087
|
+
const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
|
|
1088
|
+
// this holds all params
|
|
1089
|
+
extra);
|
|
1115
1090
|
intermediateState = nextState;
|
|
1116
|
-
if ('
|
|
1091
|
+
if ('retryResolve' in builtinAction) {
|
|
1092
|
+
retries?.push([builtinAction, params]);
|
|
1093
|
+
}
|
|
1094
|
+
if ('execute' in builtinAction) {
|
|
1117
1095
|
if (actorCtx?.self.status === ActorStatus.Running) {
|
|
1118
1096
|
builtinAction.execute(actorCtx, params);
|
|
1119
1097
|
} else {
|
|
@@ -1121,11 +1099,21 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
|
|
|
1121
1099
|
}
|
|
1122
1100
|
}
|
|
1123
1101
|
if (actions) {
|
|
1124
|
-
intermediateState =
|
|
1102
|
+
intermediateState = resolveActionsAndContextWorker(intermediateState, event, actorCtx, actions, extra, retries);
|
|
1125
1103
|
}
|
|
1126
1104
|
}
|
|
1127
1105
|
return intermediateState;
|
|
1128
1106
|
}
|
|
1107
|
+
function resolveActionsAndContext(currentState, event, actorCtx, actions, deferredActorIds) {
|
|
1108
|
+
const retries = deferredActorIds ? [] : undefined;
|
|
1109
|
+
const nextState = resolveActionsAndContextWorker(currentState, event, actorCtx, actions, deferredActorIds && {
|
|
1110
|
+
deferredActorIds
|
|
1111
|
+
}, retries);
|
|
1112
|
+
retries?.forEach(([builtinAction, params]) => {
|
|
1113
|
+
builtinAction.retryResolve(actorCtx, nextState, params);
|
|
1114
|
+
});
|
|
1115
|
+
return nextState;
|
|
1116
|
+
}
|
|
1129
1117
|
function macrostep(state, event, actorCtx) {
|
|
1130
1118
|
let nextState = state;
|
|
1131
1119
|
const states = [];
|
|
@@ -1182,7 +1170,7 @@ function stopStep(event, nextState, actorCtx) {
|
|
|
1182
1170
|
for (const child of Object.values(nextState.children)) {
|
|
1183
1171
|
actions.push(stop(child));
|
|
1184
1172
|
}
|
|
1185
|
-
return resolveActionsAndContext(
|
|
1173
|
+
return resolveActionsAndContext(nextState, event, actorCtx, actions);
|
|
1186
1174
|
}
|
|
1187
1175
|
function selectTransitions(event, nextState) {
|
|
1188
1176
|
return nextState.machine.getTransitionData(nextState, event);
|
|
@@ -1391,6 +1379,7 @@ function getPersistedState(state) {
|
|
|
1391
1379
|
tags,
|
|
1392
1380
|
machine,
|
|
1393
1381
|
children,
|
|
1382
|
+
context,
|
|
1394
1383
|
...jsonValues
|
|
1395
1384
|
} = state;
|
|
1396
1385
|
const childrenJson = {};
|
|
@@ -1403,9 +1392,38 @@ function getPersistedState(state) {
|
|
|
1403
1392
|
}
|
|
1404
1393
|
return {
|
|
1405
1394
|
...jsonValues,
|
|
1395
|
+
// TODO: this makes `PersistedMachineState`'s type kind of a lie
|
|
1396
|
+
// it doesn't truly use `TContext` but rather some kind of a derived form of it
|
|
1397
|
+
context: persistContext(context),
|
|
1406
1398
|
children: childrenJson
|
|
1407
1399
|
};
|
|
1408
1400
|
}
|
|
1401
|
+
function persistContext(contextPart) {
|
|
1402
|
+
let copy;
|
|
1403
|
+
for (const key in contextPart) {
|
|
1404
|
+
const value = contextPart[key];
|
|
1405
|
+
if (value && typeof value === 'object') {
|
|
1406
|
+
if ('sessionId' in value && 'send' in value && 'ref' in value) {
|
|
1407
|
+
copy ??= Array.isArray(contextPart) ? contextPart.slice() : {
|
|
1408
|
+
...contextPart
|
|
1409
|
+
};
|
|
1410
|
+
copy[key] = {
|
|
1411
|
+
xstate$$type: $$ACTOR_TYPE,
|
|
1412
|
+
id: value.id
|
|
1413
|
+
};
|
|
1414
|
+
} else {
|
|
1415
|
+
const result = persistContext(value);
|
|
1416
|
+
if (result !== value) {
|
|
1417
|
+
copy ??= Array.isArray(contextPart) ? contextPart.slice() : {
|
|
1418
|
+
...contextPart
|
|
1419
|
+
};
|
|
1420
|
+
copy[key] = result;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
return copy ?? contextPart;
|
|
1426
|
+
}
|
|
1409
1427
|
|
|
1410
1428
|
function resolveRaise(_, state, args, {
|
|
1411
1429
|
event: eventOrExpr,
|
|
@@ -1456,4 +1474,4 @@ function raise(eventOrExpr, options) {
|
|
|
1456
1474
|
return raise;
|
|
1457
1475
|
}
|
|
1458
1476
|
|
|
1459
|
-
export { raise as A, stop as B, State as S, formatTransition as a, formatInitialTransition as b, getCandidates as c, getConfiguration as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getStateNodes as h, isInFinalState as i, cloneState as j, macrostep as k, getInitialConfiguration as l, memo as m, resolveActionsAndContext as n, microstep as o,
|
|
1477
|
+
export { raise as A, stop as B, State as S, formatTransition as a, formatInitialTransition as b, getCandidates as c, getConfiguration as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getStateNodes as h, isInFinalState as i, cloneState as j, macrostep as k, getInitialConfiguration as l, memo as m, resolveActionsAndContext as n, microstep as o, getInitialStateNodes as p, isStateId as q, resolveStateValue as r, getStateNodeByPath as s, transitionNode as t, getPersistedState as u, and as v, not as w, or as x, stateIn as y, cancel as z };
|