xstate 5.0.0-beta.23 → 5.0.0-beta.24
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 +1 -1
- package/actions/dist/xstate-actions.development.cjs.js +1 -1
- package/actions/dist/xstate-actions.development.esm.js +1 -1
- package/actions/dist/xstate-actions.esm.js +1 -1
- 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 +1 -1
- package/actors/dist/xstate-actors.development.cjs.js +1 -1
- package/actors/dist/xstate-actors.development.esm.js +1 -1
- package/actors/dist/xstate-actors.esm.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/{actions-f6b5002d.development.esm.js → actions-194e6dcc.development.esm.js} +91 -134
- package/dist/{actions-0fcd4d15.development.cjs.js → actions-43f7d40e.development.cjs.js} +90 -134
- package/dist/{actions-e83129c5.cjs.js → actions-6a8cda73.cjs.js} +86 -138
- package/dist/{actions-bf7f6679.esm.js → actions-7b182232.esm.js} +87 -138
- package/dist/declarations/src/Machine.d.ts +1 -1
- package/dist/declarations/src/StateMachine.d.ts +8 -6
- package/dist/declarations/src/StateNode.d.ts +6 -6
- package/dist/declarations/src/actions/choose.d.ts +2 -2
- package/dist/declarations/src/actions/pure.d.ts +3 -2
- package/dist/declarations/src/actions/send.d.ts +1 -1
- package/dist/declarations/src/guards.d.ts +17 -8
- package/dist/declarations/src/stateUtils.d.ts +4 -4
- package/dist/declarations/src/typegenTypes.d.ts +6 -2
- package/dist/declarations/src/types.d.ts +77 -113
- package/dist/declarations/src/utils.d.ts +3 -3
- package/dist/xstate.cjs.js +5 -3
- package/dist/xstate.development.cjs.js +5 -3
- package/dist/xstate.development.esm.js +6 -4
- package/dist/xstate.esm.js +6 -4
- 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 -2
- package/guards/dist/xstate-guards.cjs.mjs +1 -2
- package/guards/dist/xstate-guards.development.cjs.js +1 -2
- package/guards/dist/xstate-guards.development.cjs.mjs +1 -2
- 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 +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -1494,148 +1494,109 @@ function invoke({
|
|
|
1494
1494
|
return invoke;
|
|
1495
1495
|
}
|
|
1496
1496
|
|
|
1497
|
+
function checkStateIn(state, _, {
|
|
1498
|
+
stateValue
|
|
1499
|
+
}) {
|
|
1500
|
+
if (typeof stateValue === 'string' && isStateId(stateValue)) {
|
|
1501
|
+
return state.configuration.some(sn => sn.id === stateValue.slice(1));
|
|
1502
|
+
}
|
|
1503
|
+
return state.matches(stateValue);
|
|
1504
|
+
}
|
|
1497
1505
|
function stateIn(stateValue) {
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
stateValue
|
|
1502
|
-
},
|
|
1503
|
-
predicate: ({
|
|
1504
|
-
state
|
|
1505
|
-
}) => {
|
|
1506
|
-
if (typeof stateValue === 'string' && isStateId(stateValue)) {
|
|
1507
|
-
return state.configuration.some(sn => sn.id === stateValue.slice(1));
|
|
1508
|
-
}
|
|
1509
|
-
return state.matches(stateValue);
|
|
1506
|
+
function stateIn(_) {
|
|
1507
|
+
{
|
|
1508
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1510
1509
|
}
|
|
1511
|
-
}
|
|
1510
|
+
}
|
|
1511
|
+
stateIn.check = checkStateIn;
|
|
1512
|
+
stateIn.stateValue = stateValue;
|
|
1513
|
+
return stateIn;
|
|
1514
|
+
}
|
|
1515
|
+
function checkNot(state, {
|
|
1516
|
+
context,
|
|
1517
|
+
event
|
|
1518
|
+
}, {
|
|
1519
|
+
guards
|
|
1520
|
+
}) {
|
|
1521
|
+
return !evaluateGuard(guards[0], context, event, state);
|
|
1512
1522
|
}
|
|
1513
1523
|
function not(guard) {
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
op: 'not'
|
|
1518
|
-
},
|
|
1519
|
-
children: [toGuardDefinition(guard)],
|
|
1520
|
-
predicate: ({
|
|
1521
|
-
evaluate,
|
|
1522
|
-
guard,
|
|
1523
|
-
context,
|
|
1524
|
-
event,
|
|
1525
|
-
state
|
|
1526
|
-
}) => {
|
|
1527
|
-
return !evaluate(guard.children[0], context, event, state);
|
|
1524
|
+
function not(_) {
|
|
1525
|
+
{
|
|
1526
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1528
1527
|
}
|
|
1529
|
-
}
|
|
1528
|
+
}
|
|
1529
|
+
not.check = checkNot;
|
|
1530
|
+
not.guards = [guard];
|
|
1531
|
+
return not;
|
|
1532
|
+
}
|
|
1533
|
+
function checkAnd(state, {
|
|
1534
|
+
context,
|
|
1535
|
+
event
|
|
1536
|
+
}, {
|
|
1537
|
+
guards
|
|
1538
|
+
}) {
|
|
1539
|
+
return guards.every(guard => evaluateGuard(guard, context, event, state));
|
|
1530
1540
|
}
|
|
1531
1541
|
function and(guards) {
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
op: 'and'
|
|
1536
|
-
},
|
|
1537
|
-
children: guards.map(guard => toGuardDefinition(guard)),
|
|
1538
|
-
predicate: ({
|
|
1539
|
-
evaluate,
|
|
1540
|
-
guard,
|
|
1541
|
-
context,
|
|
1542
|
-
event,
|
|
1543
|
-
state
|
|
1544
|
-
}) => {
|
|
1545
|
-
return guard.children.every(childGuard => {
|
|
1546
|
-
return evaluate(childGuard, context, event, state);
|
|
1547
|
-
});
|
|
1542
|
+
function and(_) {
|
|
1543
|
+
{
|
|
1544
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1548
1545
|
}
|
|
1549
|
-
}
|
|
1546
|
+
}
|
|
1547
|
+
and.check = checkAnd;
|
|
1548
|
+
and.guards = guards;
|
|
1549
|
+
return and;
|
|
1550
|
+
}
|
|
1551
|
+
function checkOr(state, {
|
|
1552
|
+
context,
|
|
1553
|
+
event
|
|
1554
|
+
}, {
|
|
1555
|
+
guards
|
|
1556
|
+
}) {
|
|
1557
|
+
return guards.some(guard => evaluateGuard(guard, context, event, state));
|
|
1550
1558
|
}
|
|
1551
1559
|
function or(guards) {
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
op: 'or'
|
|
1556
|
-
},
|
|
1557
|
-
children: guards.map(guard => toGuardDefinition(guard)),
|
|
1558
|
-
predicate: ({
|
|
1559
|
-
evaluate,
|
|
1560
|
-
guard,
|
|
1561
|
-
context,
|
|
1562
|
-
event,
|
|
1563
|
-
state
|
|
1564
|
-
}) => {
|
|
1565
|
-
return guard.children.some(childGuard => {
|
|
1566
|
-
return evaluate(childGuard, context, event, state);
|
|
1567
|
-
});
|
|
1560
|
+
function or(_) {
|
|
1561
|
+
{
|
|
1562
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1568
1563
|
}
|
|
1569
|
-
}
|
|
1564
|
+
}
|
|
1565
|
+
or.check = checkOr;
|
|
1566
|
+
or.guards = guards;
|
|
1567
|
+
return or;
|
|
1570
1568
|
}
|
|
1569
|
+
|
|
1570
|
+
// TODO: throw on cycles (depth check should be enough)
|
|
1571
1571
|
function evaluateGuard(guard, context, event, state) {
|
|
1572
1572
|
const {
|
|
1573
1573
|
machine
|
|
1574
1574
|
} = state;
|
|
1575
|
-
const
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1575
|
+
const isInline = typeof guard === 'function';
|
|
1576
|
+
const resolved = isInline ? guard :
|
|
1577
|
+
// the existing type of `.guards` assumes non-nullable `TExpressionGuard`
|
|
1578
|
+
// it's fine to cast this here to get a common type and lack of errors in the rest of the code
|
|
1579
|
+
// our logic below makes sure that we call those 2 "variants" correctly
|
|
1580
|
+
machine.implementations.guards[typeof guard === 'string' ? guard : guard.type];
|
|
1581
|
+
if (!isInline && !resolved) {
|
|
1582
|
+
throw new Error(`Guard '${typeof guard === 'string' ? guard : guard.type}' is not implemented.'.`);
|
|
1583
|
+
}
|
|
1584
|
+
if (typeof resolved !== 'function') {
|
|
1585
|
+
return evaluateGuard(resolved, context, event, state);
|
|
1586
|
+
}
|
|
1587
|
+
const guardArgs = {
|
|
1580
1588
|
context,
|
|
1581
1589
|
event,
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
}
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
// TODO: check for cycles and consider a refactor to more lazily evaluated guards
|
|
1589
|
-
// TODO: resolve this more recursively: https://github.com/statelyai/xstate/pull/4064#discussion_r1229915724
|
|
1590
|
-
if (typeof guardConfig === 'string') {
|
|
1591
|
-
const predicateOrDef = getPredicate?.(guardConfig);
|
|
1592
|
-
if (typeof predicateOrDef === 'function') {
|
|
1593
|
-
return {
|
|
1594
|
-
type: guardConfig,
|
|
1595
|
-
predicate: predicateOrDef,
|
|
1596
|
-
params: {
|
|
1597
|
-
type: guardConfig
|
|
1598
|
-
}
|
|
1599
|
-
};
|
|
1600
|
-
} else if (predicateOrDef) {
|
|
1601
|
-
return predicateOrDef;
|
|
1602
|
-
} else {
|
|
1603
|
-
return {
|
|
1604
|
-
type: guardConfig,
|
|
1605
|
-
params: {
|
|
1606
|
-
type: guardConfig
|
|
1607
|
-
}
|
|
1608
|
-
};
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
1611
|
-
if (typeof guardConfig === 'function') {
|
|
1612
|
-
return {
|
|
1613
|
-
type: guardConfig.name,
|
|
1614
|
-
predicate: guardConfig,
|
|
1615
|
-
params: {
|
|
1616
|
-
type: guardConfig.name,
|
|
1617
|
-
name: guardConfig.name
|
|
1618
|
-
}
|
|
1619
|
-
};
|
|
1620
|
-
}
|
|
1621
|
-
const predicateOrDef = getPredicate?.(guardConfig.type);
|
|
1622
|
-
if (typeof predicateOrDef === 'function') {
|
|
1623
|
-
return {
|
|
1624
|
-
type: guardConfig.type,
|
|
1625
|
-
params: guardConfig.params || guardConfig,
|
|
1626
|
-
children: guardConfig.children?.map(childGuard => toGuardDefinition(childGuard, getPredicate)),
|
|
1627
|
-
predicate: getPredicate?.(guardConfig.type) || guardConfig.predicate
|
|
1628
|
-
};
|
|
1629
|
-
} else if (predicateOrDef) {
|
|
1630
|
-
return predicateOrDef;
|
|
1631
|
-
} else {
|
|
1632
|
-
return {
|
|
1633
|
-
type: guardConfig.type,
|
|
1634
|
-
params: guardConfig.params || guardConfig,
|
|
1635
|
-
children: guardConfig.children?.map(childGuard => toGuardDefinition(childGuard, getPredicate)),
|
|
1636
|
-
predicate: guardConfig.predicate
|
|
1637
|
-
};
|
|
1590
|
+
guard: isInline ? undefined : typeof guard === 'string' ? {
|
|
1591
|
+
type: guard
|
|
1592
|
+
} : guard
|
|
1593
|
+
};
|
|
1594
|
+
if (!('check' in resolved)) {
|
|
1595
|
+
return resolved(guardArgs);
|
|
1638
1596
|
}
|
|
1597
|
+
const builtinGuard = resolved;
|
|
1598
|
+
return builtinGuard.check(state, guardArgs, resolved // this holds all params
|
|
1599
|
+
);
|
|
1639
1600
|
}
|
|
1640
1601
|
|
|
1641
1602
|
function getOutput(configuration, context, event, self) {
|
|
@@ -1831,9 +1792,6 @@ function getDelayedTransitions(stateNode) {
|
|
|
1831
1792
|
function formatTransition(stateNode, descriptor, transitionConfig) {
|
|
1832
1793
|
const normalizedTarget = normalizeTarget(transitionConfig.target);
|
|
1833
1794
|
const reenter = transitionConfig.reenter ?? false;
|
|
1834
|
-
const {
|
|
1835
|
-
guards
|
|
1836
|
-
} = stateNode.machine.implementations;
|
|
1837
1795
|
const target = resolveTarget(stateNode, normalizedTarget);
|
|
1838
1796
|
|
|
1839
1797
|
// TODO: should this be part of a lint rule instead?
|
|
@@ -1843,7 +1801,7 @@ function formatTransition(stateNode, descriptor, transitionConfig) {
|
|
|
1843
1801
|
const transition = {
|
|
1844
1802
|
...transitionConfig,
|
|
1845
1803
|
actions: toArray(transitionConfig.actions),
|
|
1846
|
-
guard: transitionConfig.guard
|
|
1804
|
+
guard: transitionConfig.guard,
|
|
1847
1805
|
target,
|
|
1848
1806
|
source: stateNode,
|
|
1849
1807
|
reenter,
|
|
@@ -2448,7 +2406,7 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
|
|
|
2448
2406
|
if (!resolved) {
|
|
2449
2407
|
continue;
|
|
2450
2408
|
}
|
|
2451
|
-
const
|
|
2409
|
+
const actionArgs = {
|
|
2452
2410
|
context: intermediateState.context,
|
|
2453
2411
|
event,
|
|
2454
2412
|
self: actorCtx?.self,
|
|
@@ -2459,14 +2417,14 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
|
|
|
2459
2417
|
};
|
|
2460
2418
|
if (!('resolve' in resolved)) {
|
|
2461
2419
|
if (actorCtx?.self.status === ActorStatus.Running) {
|
|
2462
|
-
resolved(
|
|
2420
|
+
resolved(actionArgs);
|
|
2463
2421
|
} else {
|
|
2464
|
-
actorCtx?.defer(() => resolved(
|
|
2422
|
+
actorCtx?.defer(() => resolved(actionArgs));
|
|
2465
2423
|
}
|
|
2466
2424
|
continue;
|
|
2467
2425
|
}
|
|
2468
2426
|
const builtinAction = resolved;
|
|
2469
|
-
const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState,
|
|
2427
|
+
const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolved // this holds all params
|
|
2470
2428
|
);
|
|
2471
2429
|
|
|
2472
2430
|
intermediateState = nextState;
|
|
@@ -3032,8 +2990,7 @@ function resolve$1(_, state, actionArgs, {
|
|
|
3032
2990
|
branches
|
|
3033
2991
|
}) {
|
|
3034
2992
|
const matchedActions = branches.find(condition => {
|
|
3035
|
-
|
|
3036
|
-
return !guard || evaluateGuard(guard, state.context, actionArgs.event, state);
|
|
2993
|
+
return !condition.guard || evaluateGuard(condition.guard, state.context, actionArgs.event, state);
|
|
3037
2994
|
})?.actions;
|
|
3038
2995
|
return [state, undefined, toArray(matchedActions)];
|
|
3039
2996
|
}
|
|
@@ -3132,4 +3089,4 @@ function createInitEvent(input) {
|
|
|
3132
3089
|
};
|
|
3133
3090
|
}
|
|
3134
3091
|
|
|
3135
|
-
export { fromObservable as $, microstep as A, isAtomicStateNode as B, isStateId as C, getStateNodeByPath as D, getPersistedState as E, resolveReferencedActor as F, createActor as G, matchesState as H, sendTo as I, sendParent as J, forwardTo as K, interpret as L, Actor as M, NULL_EVENT as N, ActorStatus as O, InterpreterStatus as P, doneInvoke as Q, cancel as R, STATE_DELIMITER as S, choose as T, log as U, pure as V, raise as W, stop as X, pathToStateValue as Y, toObserver as Z, fromPromise as _, toTransitionConfigArray as a, fromCallback as a0, fromEventObservable as a1, fromTransition as a2, stateIn as a3, not as a4, and as a5, or as a6, ConstantPrefix as a7, SpecialTargets as a8, startSignalType as a9, stopSignalType as aa, startSignal as ab, stopSignal as ac, isSignal as ad, isActorRef as ae, toActorRef as af, createEmptyActor as ag,
|
|
3092
|
+
export { fromObservable as $, microstep as A, isAtomicStateNode as B, isStateId as C, getStateNodeByPath as D, getPersistedState as E, resolveReferencedActor as F, createActor as G, matchesState as H, sendTo as I, sendParent as J, forwardTo as K, interpret as L, Actor as M, NULL_EVENT as N, ActorStatus as O, InterpreterStatus as P, doneInvoke as Q, cancel as R, STATE_DELIMITER as S, choose as T, log as U, pure as V, raise as W, stop as X, pathToStateValue as Y, toObserver as Z, fromPromise as _, toTransitionConfigArray as a, fromCallback as a0, fromEventObservable as a1, fromTransition as a2, stateIn as a3, not as a4, and as a5, or as a6, ConstantPrefix as a7, SpecialTargets as a8, startSignalType as a9, stopSignalType as aa, startSignal as ab, stopSignal as ac, isSignal as ad, isActorRef as ae, toActorRef as af, createEmptyActor as ag, constantPrefixes as ah, after as ai, done as aj, error as ak, escalate as al, formatTransition as b, memo as c, flatten as d, evaluateGuard as e, formatTransitions as f, createInvokeId as g, getDelayedTransitions as h, formatInitialTransition as i, getCandidates as j, toInvokeConfig as k, getConfiguration as l, mapValues as m, getStateNodes as n, isInFinalState as o, State as p, isErrorEvent as q, resolveStateValue as r, cloneState as s, toArray as t, macrostep as u, transitionNode as v, getInitialConfiguration as w, resolveActionsAndContext as x, assign as y, createInitEvent as z };
|
|
@@ -1496,148 +1496,109 @@ function invoke({
|
|
|
1496
1496
|
return invoke;
|
|
1497
1497
|
}
|
|
1498
1498
|
|
|
1499
|
+
function checkStateIn(state, _, {
|
|
1500
|
+
stateValue
|
|
1501
|
+
}) {
|
|
1502
|
+
if (typeof stateValue === 'string' && isStateId(stateValue)) {
|
|
1503
|
+
return state.configuration.some(sn => sn.id === stateValue.slice(1));
|
|
1504
|
+
}
|
|
1505
|
+
return state.matches(stateValue);
|
|
1506
|
+
}
|
|
1499
1507
|
function stateIn(stateValue) {
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
stateValue
|
|
1504
|
-
},
|
|
1505
|
-
predicate: ({
|
|
1506
|
-
state
|
|
1507
|
-
}) => {
|
|
1508
|
-
if (typeof stateValue === 'string' && isStateId(stateValue)) {
|
|
1509
|
-
return state.configuration.some(sn => sn.id === stateValue.slice(1));
|
|
1510
|
-
}
|
|
1511
|
-
return state.matches(stateValue);
|
|
1508
|
+
function stateIn(_) {
|
|
1509
|
+
{
|
|
1510
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1512
1511
|
}
|
|
1513
|
-
}
|
|
1512
|
+
}
|
|
1513
|
+
stateIn.check = checkStateIn;
|
|
1514
|
+
stateIn.stateValue = stateValue;
|
|
1515
|
+
return stateIn;
|
|
1516
|
+
}
|
|
1517
|
+
function checkNot(state, {
|
|
1518
|
+
context,
|
|
1519
|
+
event
|
|
1520
|
+
}, {
|
|
1521
|
+
guards
|
|
1522
|
+
}) {
|
|
1523
|
+
return !evaluateGuard(guards[0], context, event, state);
|
|
1514
1524
|
}
|
|
1515
1525
|
function not(guard) {
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
op: 'not'
|
|
1520
|
-
},
|
|
1521
|
-
children: [toGuardDefinition(guard)],
|
|
1522
|
-
predicate: ({
|
|
1523
|
-
evaluate,
|
|
1524
|
-
guard,
|
|
1525
|
-
context,
|
|
1526
|
-
event,
|
|
1527
|
-
state
|
|
1528
|
-
}) => {
|
|
1529
|
-
return !evaluate(guard.children[0], context, event, state);
|
|
1526
|
+
function not(_) {
|
|
1527
|
+
{
|
|
1528
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1530
1529
|
}
|
|
1531
|
-
}
|
|
1530
|
+
}
|
|
1531
|
+
not.check = checkNot;
|
|
1532
|
+
not.guards = [guard];
|
|
1533
|
+
return not;
|
|
1534
|
+
}
|
|
1535
|
+
function checkAnd(state, {
|
|
1536
|
+
context,
|
|
1537
|
+
event
|
|
1538
|
+
}, {
|
|
1539
|
+
guards
|
|
1540
|
+
}) {
|
|
1541
|
+
return guards.every(guard => evaluateGuard(guard, context, event, state));
|
|
1532
1542
|
}
|
|
1533
1543
|
function and(guards) {
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
op: 'and'
|
|
1538
|
-
},
|
|
1539
|
-
children: guards.map(guard => toGuardDefinition(guard)),
|
|
1540
|
-
predicate: ({
|
|
1541
|
-
evaluate,
|
|
1542
|
-
guard,
|
|
1543
|
-
context,
|
|
1544
|
-
event,
|
|
1545
|
-
state
|
|
1546
|
-
}) => {
|
|
1547
|
-
return guard.children.every(childGuard => {
|
|
1548
|
-
return evaluate(childGuard, context, event, state);
|
|
1549
|
-
});
|
|
1544
|
+
function and(_) {
|
|
1545
|
+
{
|
|
1546
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1550
1547
|
}
|
|
1551
|
-
}
|
|
1548
|
+
}
|
|
1549
|
+
and.check = checkAnd;
|
|
1550
|
+
and.guards = guards;
|
|
1551
|
+
return and;
|
|
1552
|
+
}
|
|
1553
|
+
function checkOr(state, {
|
|
1554
|
+
context,
|
|
1555
|
+
event
|
|
1556
|
+
}, {
|
|
1557
|
+
guards
|
|
1558
|
+
}) {
|
|
1559
|
+
return guards.some(guard => evaluateGuard(guard, context, event, state));
|
|
1552
1560
|
}
|
|
1553
1561
|
function or(guards) {
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
op: 'or'
|
|
1558
|
-
},
|
|
1559
|
-
children: guards.map(guard => toGuardDefinition(guard)),
|
|
1560
|
-
predicate: ({
|
|
1561
|
-
evaluate,
|
|
1562
|
-
guard,
|
|
1563
|
-
context,
|
|
1564
|
-
event,
|
|
1565
|
-
state
|
|
1566
|
-
}) => {
|
|
1567
|
-
return guard.children.some(childGuard => {
|
|
1568
|
-
return evaluate(childGuard, context, event, state);
|
|
1569
|
-
});
|
|
1562
|
+
function or(_) {
|
|
1563
|
+
{
|
|
1564
|
+
throw new Error(`This isn't supposed to be called`);
|
|
1570
1565
|
}
|
|
1571
|
-
}
|
|
1566
|
+
}
|
|
1567
|
+
or.check = checkOr;
|
|
1568
|
+
or.guards = guards;
|
|
1569
|
+
return or;
|
|
1572
1570
|
}
|
|
1571
|
+
|
|
1572
|
+
// TODO: throw on cycles (depth check should be enough)
|
|
1573
1573
|
function evaluateGuard(guard, context, event, state) {
|
|
1574
1574
|
const {
|
|
1575
1575
|
machine
|
|
1576
1576
|
} = state;
|
|
1577
|
-
const
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1577
|
+
const isInline = typeof guard === 'function';
|
|
1578
|
+
const resolved = isInline ? guard :
|
|
1579
|
+
// the existing type of `.guards` assumes non-nullable `TExpressionGuard`
|
|
1580
|
+
// it's fine to cast this here to get a common type and lack of errors in the rest of the code
|
|
1581
|
+
// our logic below makes sure that we call those 2 "variants" correctly
|
|
1582
|
+
machine.implementations.guards[typeof guard === 'string' ? guard : guard.type];
|
|
1583
|
+
if (!isInline && !resolved) {
|
|
1584
|
+
throw new Error(`Guard '${typeof guard === 'string' ? guard : guard.type}' is not implemented.'.`);
|
|
1585
|
+
}
|
|
1586
|
+
if (typeof resolved !== 'function') {
|
|
1587
|
+
return evaluateGuard(resolved, context, event, state);
|
|
1588
|
+
}
|
|
1589
|
+
const guardArgs = {
|
|
1582
1590
|
context,
|
|
1583
1591
|
event,
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
// TODO: check for cycles and consider a refactor to more lazily evaluated guards
|
|
1591
|
-
// TODO: resolve this more recursively: https://github.com/statelyai/xstate/pull/4064#discussion_r1229915724
|
|
1592
|
-
if (typeof guardConfig === 'string') {
|
|
1593
|
-
const predicateOrDef = getPredicate?.(guardConfig);
|
|
1594
|
-
if (typeof predicateOrDef === 'function') {
|
|
1595
|
-
return {
|
|
1596
|
-
type: guardConfig,
|
|
1597
|
-
predicate: predicateOrDef,
|
|
1598
|
-
params: {
|
|
1599
|
-
type: guardConfig
|
|
1600
|
-
}
|
|
1601
|
-
};
|
|
1602
|
-
} else if (predicateOrDef) {
|
|
1603
|
-
return predicateOrDef;
|
|
1604
|
-
} else {
|
|
1605
|
-
return {
|
|
1606
|
-
type: guardConfig,
|
|
1607
|
-
params: {
|
|
1608
|
-
type: guardConfig
|
|
1609
|
-
}
|
|
1610
|
-
};
|
|
1611
|
-
}
|
|
1612
|
-
}
|
|
1613
|
-
if (typeof guardConfig === 'function') {
|
|
1614
|
-
return {
|
|
1615
|
-
type: guardConfig.name,
|
|
1616
|
-
predicate: guardConfig,
|
|
1617
|
-
params: {
|
|
1618
|
-
type: guardConfig.name,
|
|
1619
|
-
name: guardConfig.name
|
|
1620
|
-
}
|
|
1621
|
-
};
|
|
1622
|
-
}
|
|
1623
|
-
const predicateOrDef = getPredicate?.(guardConfig.type);
|
|
1624
|
-
if (typeof predicateOrDef === 'function') {
|
|
1625
|
-
return {
|
|
1626
|
-
type: guardConfig.type,
|
|
1627
|
-
params: guardConfig.params || guardConfig,
|
|
1628
|
-
children: guardConfig.children?.map(childGuard => toGuardDefinition(childGuard, getPredicate)),
|
|
1629
|
-
predicate: getPredicate?.(guardConfig.type) || guardConfig.predicate
|
|
1630
|
-
};
|
|
1631
|
-
} else if (predicateOrDef) {
|
|
1632
|
-
return predicateOrDef;
|
|
1633
|
-
} else {
|
|
1634
|
-
return {
|
|
1635
|
-
type: guardConfig.type,
|
|
1636
|
-
params: guardConfig.params || guardConfig,
|
|
1637
|
-
children: guardConfig.children?.map(childGuard => toGuardDefinition(childGuard, getPredicate)),
|
|
1638
|
-
predicate: guardConfig.predicate
|
|
1639
|
-
};
|
|
1592
|
+
guard: isInline ? undefined : typeof guard === 'string' ? {
|
|
1593
|
+
type: guard
|
|
1594
|
+
} : guard
|
|
1595
|
+
};
|
|
1596
|
+
if (!('check' in resolved)) {
|
|
1597
|
+
return resolved(guardArgs);
|
|
1640
1598
|
}
|
|
1599
|
+
const builtinGuard = resolved;
|
|
1600
|
+
return builtinGuard.check(state, guardArgs, resolved // this holds all params
|
|
1601
|
+
);
|
|
1641
1602
|
}
|
|
1642
1603
|
|
|
1643
1604
|
function getOutput(configuration, context, event, self) {
|
|
@@ -1833,9 +1794,6 @@ function getDelayedTransitions(stateNode) {
|
|
|
1833
1794
|
function formatTransition(stateNode, descriptor, transitionConfig) {
|
|
1834
1795
|
const normalizedTarget = normalizeTarget(transitionConfig.target);
|
|
1835
1796
|
const reenter = transitionConfig.reenter ?? false;
|
|
1836
|
-
const {
|
|
1837
|
-
guards
|
|
1838
|
-
} = stateNode.machine.implementations;
|
|
1839
1797
|
const target = resolveTarget(stateNode, normalizedTarget);
|
|
1840
1798
|
|
|
1841
1799
|
// TODO: should this be part of a lint rule instead?
|
|
@@ -1845,7 +1803,7 @@ function formatTransition(stateNode, descriptor, transitionConfig) {
|
|
|
1845
1803
|
const transition = {
|
|
1846
1804
|
...transitionConfig,
|
|
1847
1805
|
actions: toArray(transitionConfig.actions),
|
|
1848
|
-
guard: transitionConfig.guard
|
|
1806
|
+
guard: transitionConfig.guard,
|
|
1849
1807
|
target,
|
|
1850
1808
|
source: stateNode,
|
|
1851
1809
|
reenter,
|
|
@@ -2450,7 +2408,7 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
|
|
|
2450
2408
|
if (!resolved) {
|
|
2451
2409
|
continue;
|
|
2452
2410
|
}
|
|
2453
|
-
const
|
|
2411
|
+
const actionArgs = {
|
|
2454
2412
|
context: intermediateState.context,
|
|
2455
2413
|
event,
|
|
2456
2414
|
self: actorCtx?.self,
|
|
@@ -2461,14 +2419,14 @@ function resolveActionsAndContext(actions, event, currentState, actorCtx) {
|
|
|
2461
2419
|
};
|
|
2462
2420
|
if (!('resolve' in resolved)) {
|
|
2463
2421
|
if (actorCtx?.self.status === ActorStatus.Running) {
|
|
2464
|
-
resolved(
|
|
2422
|
+
resolved(actionArgs);
|
|
2465
2423
|
} else {
|
|
2466
|
-
actorCtx?.defer(() => resolved(
|
|
2424
|
+
actorCtx?.defer(() => resolved(actionArgs));
|
|
2467
2425
|
}
|
|
2468
2426
|
continue;
|
|
2469
2427
|
}
|
|
2470
2428
|
const builtinAction = resolved;
|
|
2471
|
-
const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState,
|
|
2429
|
+
const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolved // this holds all params
|
|
2472
2430
|
);
|
|
2473
2431
|
|
|
2474
2432
|
intermediateState = nextState;
|
|
@@ -3034,8 +2992,7 @@ function resolve$1(_, state, actionArgs, {
|
|
|
3034
2992
|
branches
|
|
3035
2993
|
}) {
|
|
3036
2994
|
const matchedActions = branches.find(condition => {
|
|
3037
|
-
|
|
3038
|
-
return !guard || evaluateGuard(guard, state.context, actionArgs.event, state);
|
|
2995
|
+
return !condition.guard || evaluateGuard(condition.guard, state.context, actionArgs.event, state);
|
|
3039
2996
|
})?.actions;
|
|
3040
2997
|
return [state, undefined, toArray(matchedActions)];
|
|
3041
2998
|
}
|
|
@@ -3206,7 +3163,6 @@ exports.stopSignal = stopSignal;
|
|
|
3206
3163
|
exports.stopSignalType = stopSignalType;
|
|
3207
3164
|
exports.toActorRef = toActorRef;
|
|
3208
3165
|
exports.toArray = toArray;
|
|
3209
|
-
exports.toGuardDefinition = toGuardDefinition;
|
|
3210
3166
|
exports.toInvokeConfig = toInvokeConfig;
|
|
3211
3167
|
exports.toObserver = toObserver;
|
|
3212
3168
|
exports.toTransitionConfigArray = toTransitionConfigArray;
|