xstate 5.4.1 → 5.5.1
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 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- 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 +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/declarations/src/State.d.ts +30 -3
- package/dist/declarations/src/StateMachine.d.ts +3 -3
- package/dist/declarations/src/StateNode.d.ts +0 -4
- package/dist/declarations/src/actions/assign.d.ts +28 -1
- package/dist/declarations/src/actions/cancel.d.ts +23 -4
- package/dist/declarations/src/actions/enqueueActions.d.ts +20 -0
- package/dist/declarations/src/actions/send.d.ts +2 -2
- package/dist/declarations/src/createActor.d.ts +0 -1
- package/dist/declarations/src/createMachine.d.ts +40 -0
- package/dist/declarations/src/getNextSnapshot.d.ts +32 -0
- package/dist/declarations/src/guards.d.ts +86 -0
- package/dist/declarations/src/index.d.ts +1 -0
- package/dist/declarations/src/spawn.d.ts +2 -2
- package/dist/declarations/src/typegenTypes.d.ts +26 -1
- package/dist/declarations/src/types.d.ts +9 -29
- package/dist/{log-88b333eb.esm.js → log-22b3587f.esm.js} +67 -10
- package/dist/{log-11038f00.development.esm.js → log-285f62db.development.esm.js} +67 -10
- package/dist/{log-2580e864.cjs.js → log-742895c6.cjs.js} +67 -10
- package/dist/{log-18eb632d.development.cjs.js → log-da322832.development.cjs.js} +67 -10
- package/dist/{raise-3b380e4b.esm.js → raise-0e64ee6e.esm.js} +124 -13
- package/dist/{raise-3d3d6d51.development.cjs.js → raise-7af39710.development.cjs.js} +124 -13
- package/dist/{raise-057d17af.development.esm.js → raise-8da27ebb.development.esm.js} +124 -13
- package/dist/{raise-5c58eb8e.cjs.js → raise-ad8bb7c2.cjs.js} +124 -13
- package/dist/xstate.cjs.js +105 -4
- package/dist/xstate.cjs.mjs +1 -0
- package/dist/xstate.development.cjs.js +105 -4
- package/dist/xstate.development.cjs.mjs +1 -0
- package/dist/xstate.development.esm.js +107 -7
- package/dist/xstate.esm.js +107 -7
- 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
|
@@ -436,6 +436,7 @@ class Actor {
|
|
|
436
436
|
this._processingStatus = ProcessingStatus.NotStarted;
|
|
437
437
|
// Actor Ref
|
|
438
438
|
this._parent = void 0;
|
|
439
|
+
/** @internal */
|
|
439
440
|
this._syncSnapshot = void 0;
|
|
440
441
|
this.ref = void 0;
|
|
441
442
|
// TODO: add typings for system
|
|
@@ -1015,11 +1016,30 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1015
1016
|
});
|
|
1016
1017
|
}
|
|
1017
1018
|
/**
|
|
1018
|
-
* Cancels
|
|
1019
|
-
*
|
|
1020
|
-
* (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
|
|
1019
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The canceled `sendTo(...)` action
|
|
1020
|
+
* will not send its event or execute, unless the `delay` has already elapsed before `cancel(...)` is called.
|
|
1021
1021
|
*
|
|
1022
|
-
* @param sendId The `id` of the `
|
|
1022
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1023
|
+
*
|
|
1024
|
+
* @example
|
|
1025
|
+
```ts
|
|
1026
|
+
import { createMachine, sendTo, cancel } from 'xstate';
|
|
1027
|
+
|
|
1028
|
+
const machine = createMachine({
|
|
1029
|
+
// ...
|
|
1030
|
+
on: {
|
|
1031
|
+
sendEvent: {
|
|
1032
|
+
actions: sendTo('some-actor', { type: 'someEvent' }, {
|
|
1033
|
+
id: 'some-id',
|
|
1034
|
+
delay: 1000
|
|
1035
|
+
})
|
|
1036
|
+
},
|
|
1037
|
+
cancelEvent: {
|
|
1038
|
+
actions: cancel('some-id')
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
});
|
|
1042
|
+
```
|
|
1023
1043
|
*/
|
|
1024
1044
|
function cancel(sendId) {
|
|
1025
1045
|
function cancel(args, params) {
|
|
@@ -1048,13 +1068,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
|
|
|
1048
1068
|
actorRef = createActor(logic, {
|
|
1049
1069
|
id: resolvedId,
|
|
1050
1070
|
src,
|
|
1051
|
-
parent: actorScope
|
|
1071
|
+
parent: actorScope.self,
|
|
1052
1072
|
syncSnapshot,
|
|
1053
1073
|
systemId,
|
|
1054
1074
|
input: typeof input === 'function' ? input({
|
|
1055
1075
|
context: snapshot.context,
|
|
1056
1076
|
event: actionArgs.event,
|
|
1057
|
-
self: actorScope
|
|
1077
|
+
self: actorScope.self
|
|
1058
1078
|
}) : input
|
|
1059
1079
|
});
|
|
1060
1080
|
}
|
|
@@ -1199,6 +1219,33 @@ function checkNot(snapshot, {
|
|
|
1199
1219
|
}) {
|
|
1200
1220
|
return !evaluateGuard(guards[0], context, event, snapshot);
|
|
1201
1221
|
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it evaluates to `false`.
|
|
1225
|
+
*
|
|
1226
|
+
* @category Guards
|
|
1227
|
+
* @example
|
|
1228
|
+
```ts
|
|
1229
|
+
import { setup, not } from 'xstate';
|
|
1230
|
+
|
|
1231
|
+
const machine = setup({
|
|
1232
|
+
guards: {
|
|
1233
|
+
someNamedGuard: () => false
|
|
1234
|
+
}
|
|
1235
|
+
}).createMachine({
|
|
1236
|
+
on: {
|
|
1237
|
+
someEvent: {
|
|
1238
|
+
guard: not('someNamedGuard'),
|
|
1239
|
+
actions: () => {
|
|
1240
|
+
// will be executed if guard in `not(...)`
|
|
1241
|
+
// evaluates to `false`
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
});
|
|
1246
|
+
```
|
|
1247
|
+
* @returns A guard
|
|
1248
|
+
*/
|
|
1202
1249
|
function not(guard) {
|
|
1203
1250
|
function not(args, params) {
|
|
1204
1251
|
{
|
|
@@ -1217,6 +1264,37 @@ function checkAnd(snapshot, {
|
|
|
1217
1264
|
}) {
|
|
1218
1265
|
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1219
1266
|
}
|
|
1267
|
+
|
|
1268
|
+
/**
|
|
1269
|
+
* Higher-order guard that evaluates to `true` if all `guards` passed to it
|
|
1270
|
+
* evaluate to `true`.
|
|
1271
|
+
*
|
|
1272
|
+
* @category Guards
|
|
1273
|
+
* @example
|
|
1274
|
+
```ts
|
|
1275
|
+
import { setup, and } from 'xstate';
|
|
1276
|
+
|
|
1277
|
+
const machine = setup({
|
|
1278
|
+
guards: {
|
|
1279
|
+
someNamedGuard: () => true
|
|
1280
|
+
}
|
|
1281
|
+
}).createMachine({
|
|
1282
|
+
on: {
|
|
1283
|
+
someEvent: {
|
|
1284
|
+
guard: and([
|
|
1285
|
+
({ context }) => context.value > 0,
|
|
1286
|
+
'someNamedGuard'
|
|
1287
|
+
]),
|
|
1288
|
+
actions: () => {
|
|
1289
|
+
// will be executed if all guards in `and(...)`
|
|
1290
|
+
// evaluate to true
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
});
|
|
1295
|
+
```
|
|
1296
|
+
* @returns A guard action object
|
|
1297
|
+
*/
|
|
1220
1298
|
function and(guards) {
|
|
1221
1299
|
function and(args, params) {
|
|
1222
1300
|
{
|
|
@@ -1235,6 +1313,37 @@ function checkOr(snapshot, {
|
|
|
1235
1313
|
}) {
|
|
1236
1314
|
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1237
1315
|
}
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to it
|
|
1319
|
+
* evaluate to `true`.
|
|
1320
|
+
*
|
|
1321
|
+
* @category Guards
|
|
1322
|
+
* @example
|
|
1323
|
+
```ts
|
|
1324
|
+
import { setup, or } from 'xstate';
|
|
1325
|
+
|
|
1326
|
+
const machine = setup({
|
|
1327
|
+
guards: {
|
|
1328
|
+
someNamedGuard: () => true
|
|
1329
|
+
}
|
|
1330
|
+
}).createMachine({
|
|
1331
|
+
on: {
|
|
1332
|
+
someEvent: {
|
|
1333
|
+
guard: or([
|
|
1334
|
+
({ context }) => context.value > 0,
|
|
1335
|
+
'someNamedGuard'
|
|
1336
|
+
]),
|
|
1337
|
+
actions: () => {
|
|
1338
|
+
// will be executed if any of the guards in `or(...)`
|
|
1339
|
+
// evaluate to true
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
});
|
|
1344
|
+
```
|
|
1345
|
+
* @returns A guard action object
|
|
1346
|
+
*/
|
|
1238
1347
|
function or(guards) {
|
|
1239
1348
|
function or(args, params) {
|
|
1240
1349
|
{
|
|
@@ -2092,18 +2201,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2092
2201
|
const actionArgs = {
|
|
2093
2202
|
context: intermediateSnapshot.context,
|
|
2094
2203
|
event,
|
|
2095
|
-
self: actorScope
|
|
2096
|
-
system: actorScope
|
|
2204
|
+
self: actorScope.self,
|
|
2205
|
+
system: actorScope.system
|
|
2097
2206
|
};
|
|
2098
2207
|
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
|
|
2099
2208
|
context: intermediateSnapshot.context,
|
|
2100
2209
|
event
|
|
2101
2210
|
}) : action.params : undefined;
|
|
2102
2211
|
if (!('resolve' in resolvedAction)) {
|
|
2103
|
-
if (actorScope
|
|
2212
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2104
2213
|
resolvedAction(actionArgs, actionParams);
|
|
2105
2214
|
} else {
|
|
2106
|
-
actorScope
|
|
2215
|
+
actorScope.defer(() => {
|
|
2107
2216
|
resolvedAction(actionArgs, actionParams);
|
|
2108
2217
|
});
|
|
2109
2218
|
}
|
|
@@ -2118,10 +2227,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2118
2227
|
retries?.push([builtinAction, params]);
|
|
2119
2228
|
}
|
|
2120
2229
|
if ('execute' in builtinAction) {
|
|
2121
|
-
if (actorScope
|
|
2230
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2122
2231
|
builtinAction.execute(actorScope, params);
|
|
2123
2232
|
} else {
|
|
2124
|
-
actorScope
|
|
2233
|
+
actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
|
|
2125
2234
|
}
|
|
2126
2235
|
}
|
|
2127
2236
|
if (actions) {
|
|
@@ -2181,7 +2290,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
|
|
|
2181
2290
|
microstates: states
|
|
2182
2291
|
};
|
|
2183
2292
|
}
|
|
2184
|
-
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2293
|
+
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2294
|
+
// isInitial
|
|
2295
|
+
internalQueue);
|
|
2185
2296
|
states.push(nextSnapshot);
|
|
2186
2297
|
}
|
|
2187
2298
|
let shouldSelectEventlessTransitions = true;
|
|
@@ -434,6 +434,7 @@ class Actor {
|
|
|
434
434
|
this._processingStatus = ProcessingStatus.NotStarted;
|
|
435
435
|
// Actor Ref
|
|
436
436
|
this._parent = void 0;
|
|
437
|
+
/** @internal */
|
|
437
438
|
this._syncSnapshot = void 0;
|
|
438
439
|
this.ref = void 0;
|
|
439
440
|
// TODO: add typings for system
|
|
@@ -1013,11 +1014,30 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1013
1014
|
});
|
|
1014
1015
|
}
|
|
1015
1016
|
/**
|
|
1016
|
-
* Cancels
|
|
1017
|
-
*
|
|
1018
|
-
* (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
|
|
1017
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The canceled `sendTo(...)` action
|
|
1018
|
+
* will not send its event or execute, unless the `delay` has already elapsed before `cancel(...)` is called.
|
|
1019
1019
|
*
|
|
1020
|
-
* @param sendId The `id` of the `
|
|
1020
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1021
|
+
*
|
|
1022
|
+
* @example
|
|
1023
|
+
```ts
|
|
1024
|
+
import { createMachine, sendTo, cancel } from 'xstate';
|
|
1025
|
+
|
|
1026
|
+
const machine = createMachine({
|
|
1027
|
+
// ...
|
|
1028
|
+
on: {
|
|
1029
|
+
sendEvent: {
|
|
1030
|
+
actions: sendTo('some-actor', { type: 'someEvent' }, {
|
|
1031
|
+
id: 'some-id',
|
|
1032
|
+
delay: 1000
|
|
1033
|
+
})
|
|
1034
|
+
},
|
|
1035
|
+
cancelEvent: {
|
|
1036
|
+
actions: cancel('some-id')
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
```
|
|
1021
1041
|
*/
|
|
1022
1042
|
function cancel(sendId) {
|
|
1023
1043
|
function cancel(args, params) {
|
|
@@ -1046,13 +1066,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
|
|
|
1046
1066
|
actorRef = createActor(logic, {
|
|
1047
1067
|
id: resolvedId,
|
|
1048
1068
|
src,
|
|
1049
|
-
parent: actorScope
|
|
1069
|
+
parent: actorScope.self,
|
|
1050
1070
|
syncSnapshot,
|
|
1051
1071
|
systemId,
|
|
1052
1072
|
input: typeof input === 'function' ? input({
|
|
1053
1073
|
context: snapshot.context,
|
|
1054
1074
|
event: actionArgs.event,
|
|
1055
|
-
self: actorScope
|
|
1075
|
+
self: actorScope.self
|
|
1056
1076
|
}) : input
|
|
1057
1077
|
});
|
|
1058
1078
|
}
|
|
@@ -1197,6 +1217,33 @@ function checkNot(snapshot, {
|
|
|
1197
1217
|
}) {
|
|
1198
1218
|
return !evaluateGuard(guards[0], context, event, snapshot);
|
|
1199
1219
|
}
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it evaluates to `false`.
|
|
1223
|
+
*
|
|
1224
|
+
* @category Guards
|
|
1225
|
+
* @example
|
|
1226
|
+
```ts
|
|
1227
|
+
import { setup, not } from 'xstate';
|
|
1228
|
+
|
|
1229
|
+
const machine = setup({
|
|
1230
|
+
guards: {
|
|
1231
|
+
someNamedGuard: () => false
|
|
1232
|
+
}
|
|
1233
|
+
}).createMachine({
|
|
1234
|
+
on: {
|
|
1235
|
+
someEvent: {
|
|
1236
|
+
guard: not('someNamedGuard'),
|
|
1237
|
+
actions: () => {
|
|
1238
|
+
// will be executed if guard in `not(...)`
|
|
1239
|
+
// evaluates to `false`
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
});
|
|
1244
|
+
```
|
|
1245
|
+
* @returns A guard
|
|
1246
|
+
*/
|
|
1200
1247
|
function not(guard) {
|
|
1201
1248
|
function not(args, params) {
|
|
1202
1249
|
{
|
|
@@ -1215,6 +1262,37 @@ function checkAnd(snapshot, {
|
|
|
1215
1262
|
}) {
|
|
1216
1263
|
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1217
1264
|
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* Higher-order guard that evaluates to `true` if all `guards` passed to it
|
|
1268
|
+
* evaluate to `true`.
|
|
1269
|
+
*
|
|
1270
|
+
* @category Guards
|
|
1271
|
+
* @example
|
|
1272
|
+
```ts
|
|
1273
|
+
import { setup, and } from 'xstate';
|
|
1274
|
+
|
|
1275
|
+
const machine = setup({
|
|
1276
|
+
guards: {
|
|
1277
|
+
someNamedGuard: () => true
|
|
1278
|
+
}
|
|
1279
|
+
}).createMachine({
|
|
1280
|
+
on: {
|
|
1281
|
+
someEvent: {
|
|
1282
|
+
guard: and([
|
|
1283
|
+
({ context }) => context.value > 0,
|
|
1284
|
+
'someNamedGuard'
|
|
1285
|
+
]),
|
|
1286
|
+
actions: () => {
|
|
1287
|
+
// will be executed if all guards in `and(...)`
|
|
1288
|
+
// evaluate to true
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
});
|
|
1293
|
+
```
|
|
1294
|
+
* @returns A guard action object
|
|
1295
|
+
*/
|
|
1218
1296
|
function and(guards) {
|
|
1219
1297
|
function and(args, params) {
|
|
1220
1298
|
{
|
|
@@ -1233,6 +1311,37 @@ function checkOr(snapshot, {
|
|
|
1233
1311
|
}) {
|
|
1234
1312
|
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1235
1313
|
}
|
|
1314
|
+
|
|
1315
|
+
/**
|
|
1316
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to it
|
|
1317
|
+
* evaluate to `true`.
|
|
1318
|
+
*
|
|
1319
|
+
* @category Guards
|
|
1320
|
+
* @example
|
|
1321
|
+
```ts
|
|
1322
|
+
import { setup, or } from 'xstate';
|
|
1323
|
+
|
|
1324
|
+
const machine = setup({
|
|
1325
|
+
guards: {
|
|
1326
|
+
someNamedGuard: () => true
|
|
1327
|
+
}
|
|
1328
|
+
}).createMachine({
|
|
1329
|
+
on: {
|
|
1330
|
+
someEvent: {
|
|
1331
|
+
guard: or([
|
|
1332
|
+
({ context }) => context.value > 0,
|
|
1333
|
+
'someNamedGuard'
|
|
1334
|
+
]),
|
|
1335
|
+
actions: () => {
|
|
1336
|
+
// will be executed if any of the guards in `or(...)`
|
|
1337
|
+
// evaluate to true
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
```
|
|
1343
|
+
* @returns A guard action object
|
|
1344
|
+
*/
|
|
1236
1345
|
function or(guards) {
|
|
1237
1346
|
function or(args, params) {
|
|
1238
1347
|
{
|
|
@@ -2090,18 +2199,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2090
2199
|
const actionArgs = {
|
|
2091
2200
|
context: intermediateSnapshot.context,
|
|
2092
2201
|
event,
|
|
2093
|
-
self: actorScope
|
|
2094
|
-
system: actorScope
|
|
2202
|
+
self: actorScope.self,
|
|
2203
|
+
system: actorScope.system
|
|
2095
2204
|
};
|
|
2096
2205
|
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
|
|
2097
2206
|
context: intermediateSnapshot.context,
|
|
2098
2207
|
event
|
|
2099
2208
|
}) : action.params : undefined;
|
|
2100
2209
|
if (!('resolve' in resolvedAction)) {
|
|
2101
|
-
if (actorScope
|
|
2210
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2102
2211
|
resolvedAction(actionArgs, actionParams);
|
|
2103
2212
|
} else {
|
|
2104
|
-
actorScope
|
|
2213
|
+
actorScope.defer(() => {
|
|
2105
2214
|
resolvedAction(actionArgs, actionParams);
|
|
2106
2215
|
});
|
|
2107
2216
|
}
|
|
@@ -2116,10 +2225,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2116
2225
|
retries?.push([builtinAction, params]);
|
|
2117
2226
|
}
|
|
2118
2227
|
if ('execute' in builtinAction) {
|
|
2119
|
-
if (actorScope
|
|
2228
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2120
2229
|
builtinAction.execute(actorScope, params);
|
|
2121
2230
|
} else {
|
|
2122
|
-
actorScope
|
|
2231
|
+
actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
|
|
2123
2232
|
}
|
|
2124
2233
|
}
|
|
2125
2234
|
if (actions) {
|
|
@@ -2179,7 +2288,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
|
|
|
2179
2288
|
microstates: states
|
|
2180
2289
|
};
|
|
2181
2290
|
}
|
|
2182
|
-
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2291
|
+
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2292
|
+
// isInitial
|
|
2293
|
+
internalQueue);
|
|
2183
2294
|
states.push(nextSnapshot);
|
|
2184
2295
|
}
|
|
2185
2296
|
let shouldSelectEventlessTransitions = true;
|
|
@@ -433,6 +433,7 @@ class Actor {
|
|
|
433
433
|
this._processingStatus = ProcessingStatus.NotStarted;
|
|
434
434
|
// Actor Ref
|
|
435
435
|
this._parent = void 0;
|
|
436
|
+
/** @internal */
|
|
436
437
|
this._syncSnapshot = void 0;
|
|
437
438
|
this.ref = void 0;
|
|
438
439
|
// TODO: add typings for system
|
|
@@ -1001,11 +1002,30 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1001
1002
|
});
|
|
1002
1003
|
}
|
|
1003
1004
|
/**
|
|
1004
|
-
* Cancels
|
|
1005
|
-
*
|
|
1006
|
-
* (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
|
|
1005
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The canceled `sendTo(...)` action
|
|
1006
|
+
* will not send its event or execute, unless the `delay` has already elapsed before `cancel(...)` is called.
|
|
1007
1007
|
*
|
|
1008
|
-
* @param sendId The `id` of the `
|
|
1008
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1009
|
+
*
|
|
1010
|
+
* @example
|
|
1011
|
+
```ts
|
|
1012
|
+
import { createMachine, sendTo, cancel } from 'xstate';
|
|
1013
|
+
|
|
1014
|
+
const machine = createMachine({
|
|
1015
|
+
// ...
|
|
1016
|
+
on: {
|
|
1017
|
+
sendEvent: {
|
|
1018
|
+
actions: sendTo('some-actor', { type: 'someEvent' }, {
|
|
1019
|
+
id: 'some-id',
|
|
1020
|
+
delay: 1000
|
|
1021
|
+
})
|
|
1022
|
+
},
|
|
1023
|
+
cancelEvent: {
|
|
1024
|
+
actions: cancel('some-id')
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
```
|
|
1009
1029
|
*/
|
|
1010
1030
|
function cancel(sendId) {
|
|
1011
1031
|
function cancel(args, params) {
|
|
@@ -1031,13 +1051,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
|
|
|
1031
1051
|
actorRef = createActor(logic, {
|
|
1032
1052
|
id: resolvedId,
|
|
1033
1053
|
src,
|
|
1034
|
-
parent: actorScope
|
|
1054
|
+
parent: actorScope.self,
|
|
1035
1055
|
syncSnapshot,
|
|
1036
1056
|
systemId,
|
|
1037
1057
|
input: typeof input === 'function' ? input({
|
|
1038
1058
|
context: snapshot.context,
|
|
1039
1059
|
event: actionArgs.event,
|
|
1040
|
-
self: actorScope
|
|
1060
|
+
self: actorScope.self
|
|
1041
1061
|
}) : input
|
|
1042
1062
|
});
|
|
1043
1063
|
}
|
|
@@ -1171,6 +1191,33 @@ function checkNot(snapshot, {
|
|
|
1171
1191
|
}) {
|
|
1172
1192
|
return !evaluateGuard(guards[0], context, event, snapshot);
|
|
1173
1193
|
}
|
|
1194
|
+
|
|
1195
|
+
/**
|
|
1196
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it evaluates to `false`.
|
|
1197
|
+
*
|
|
1198
|
+
* @category Guards
|
|
1199
|
+
* @example
|
|
1200
|
+
```ts
|
|
1201
|
+
import { setup, not } from 'xstate';
|
|
1202
|
+
|
|
1203
|
+
const machine = setup({
|
|
1204
|
+
guards: {
|
|
1205
|
+
someNamedGuard: () => false
|
|
1206
|
+
}
|
|
1207
|
+
}).createMachine({
|
|
1208
|
+
on: {
|
|
1209
|
+
someEvent: {
|
|
1210
|
+
guard: not('someNamedGuard'),
|
|
1211
|
+
actions: () => {
|
|
1212
|
+
// will be executed if guard in `not(...)`
|
|
1213
|
+
// evaluates to `false`
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
});
|
|
1218
|
+
```
|
|
1219
|
+
* @returns A guard
|
|
1220
|
+
*/
|
|
1174
1221
|
function not(guard) {
|
|
1175
1222
|
function not(args, params) {
|
|
1176
1223
|
return false;
|
|
@@ -1187,6 +1234,37 @@ function checkAnd(snapshot, {
|
|
|
1187
1234
|
}) {
|
|
1188
1235
|
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1189
1236
|
}
|
|
1237
|
+
|
|
1238
|
+
/**
|
|
1239
|
+
* Higher-order guard that evaluates to `true` if all `guards` passed to it
|
|
1240
|
+
* evaluate to `true`.
|
|
1241
|
+
*
|
|
1242
|
+
* @category Guards
|
|
1243
|
+
* @example
|
|
1244
|
+
```ts
|
|
1245
|
+
import { setup, and } from 'xstate';
|
|
1246
|
+
|
|
1247
|
+
const machine = setup({
|
|
1248
|
+
guards: {
|
|
1249
|
+
someNamedGuard: () => true
|
|
1250
|
+
}
|
|
1251
|
+
}).createMachine({
|
|
1252
|
+
on: {
|
|
1253
|
+
someEvent: {
|
|
1254
|
+
guard: and([
|
|
1255
|
+
({ context }) => context.value > 0,
|
|
1256
|
+
'someNamedGuard'
|
|
1257
|
+
]),
|
|
1258
|
+
actions: () => {
|
|
1259
|
+
// will be executed if all guards in `and(...)`
|
|
1260
|
+
// evaluate to true
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
});
|
|
1265
|
+
```
|
|
1266
|
+
* @returns A guard action object
|
|
1267
|
+
*/
|
|
1190
1268
|
function and(guards) {
|
|
1191
1269
|
function and(args, params) {
|
|
1192
1270
|
return false;
|
|
@@ -1203,6 +1281,37 @@ function checkOr(snapshot, {
|
|
|
1203
1281
|
}) {
|
|
1204
1282
|
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1205
1283
|
}
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to it
|
|
1287
|
+
* evaluate to `true`.
|
|
1288
|
+
*
|
|
1289
|
+
* @category Guards
|
|
1290
|
+
* @example
|
|
1291
|
+
```ts
|
|
1292
|
+
import { setup, or } from 'xstate';
|
|
1293
|
+
|
|
1294
|
+
const machine = setup({
|
|
1295
|
+
guards: {
|
|
1296
|
+
someNamedGuard: () => true
|
|
1297
|
+
}
|
|
1298
|
+
}).createMachine({
|
|
1299
|
+
on: {
|
|
1300
|
+
someEvent: {
|
|
1301
|
+
guard: or([
|
|
1302
|
+
({ context }) => context.value > 0,
|
|
1303
|
+
'someNamedGuard'
|
|
1304
|
+
]),
|
|
1305
|
+
actions: () => {
|
|
1306
|
+
// will be executed if any of the guards in `or(...)`
|
|
1307
|
+
// evaluate to true
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
});
|
|
1312
|
+
```
|
|
1313
|
+
* @returns A guard action object
|
|
1314
|
+
*/
|
|
1206
1315
|
function or(guards) {
|
|
1207
1316
|
function or(args, params) {
|
|
1208
1317
|
return false;
|
|
@@ -2047,18 +2156,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2047
2156
|
const actionArgs = {
|
|
2048
2157
|
context: intermediateSnapshot.context,
|
|
2049
2158
|
event,
|
|
2050
|
-
self: actorScope
|
|
2051
|
-
system: actorScope
|
|
2159
|
+
self: actorScope.self,
|
|
2160
|
+
system: actorScope.system
|
|
2052
2161
|
};
|
|
2053
2162
|
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
|
|
2054
2163
|
context: intermediateSnapshot.context,
|
|
2055
2164
|
event
|
|
2056
2165
|
}) : action.params : undefined;
|
|
2057
2166
|
if (!('resolve' in resolvedAction)) {
|
|
2058
|
-
if (actorScope
|
|
2167
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2059
2168
|
resolvedAction(actionArgs, actionParams);
|
|
2060
2169
|
} else {
|
|
2061
|
-
actorScope
|
|
2170
|
+
actorScope.defer(() => {
|
|
2062
2171
|
resolvedAction(actionArgs, actionParams);
|
|
2063
2172
|
});
|
|
2064
2173
|
}
|
|
@@ -2073,10 +2182,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2073
2182
|
retries?.push([builtinAction, params]);
|
|
2074
2183
|
}
|
|
2075
2184
|
if ('execute' in builtinAction) {
|
|
2076
|
-
if (actorScope
|
|
2185
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2077
2186
|
builtinAction.execute(actorScope, params);
|
|
2078
2187
|
} else {
|
|
2079
|
-
actorScope
|
|
2188
|
+
actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
|
|
2080
2189
|
}
|
|
2081
2190
|
}
|
|
2082
2191
|
if (actions) {
|
|
@@ -2133,7 +2242,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
|
|
|
2133
2242
|
microstates: states
|
|
2134
2243
|
};
|
|
2135
2244
|
}
|
|
2136
|
-
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2245
|
+
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2246
|
+
// isInitial
|
|
2247
|
+
internalQueue);
|
|
2137
2248
|
states.push(nextSnapshot);
|
|
2138
2249
|
}
|
|
2139
2250
|
let shouldSelectEventlessTransitions = true;
|