xstate 5.4.0 → 5.5.0
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 +43 -17
- package/dist/declarations/src/StateMachine.d.ts +16 -15
- 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/{interpreter.d.ts → createActor.d.ts} +2 -4
- 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 +2 -1
- package/dist/declarations/src/spawn.d.ts +2 -2
- package/dist/declarations/src/typegenTypes.d.ts +26 -14
- package/dist/declarations/src/types.d.ts +17 -36
- package/dist/{log-e870aec8.esm.js → log-22b3587f.esm.js} +67 -10
- package/dist/{log-580765a2.development.esm.js → log-285f62db.development.esm.js} +67 -10
- package/dist/{log-a32b44b3.cjs.js → log-742895c6.cjs.js} +67 -10
- package/dist/{log-cd3d7c14.development.cjs.js → log-da322832.development.cjs.js} +67 -10
- package/dist/{raise-7df513e7.esm.js → raise-0e64ee6e.esm.js} +125 -16
- package/dist/{raise-e3ff3de1.development.cjs.js → raise-7af39710.development.cjs.js} +125 -16
- package/dist/{raise-0fc3a80c.development.esm.js → raise-8da27ebb.development.esm.js} +125 -16
- package/dist/{raise-c3bbdd3a.cjs.js → raise-ad8bb7c2.cjs.js} +125 -16
- 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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var guards_dist_xstateGuards = require('./raise-
|
|
3
|
+
var guards_dist_xstateGuards = require('./raise-7af39710.development.cjs.js');
|
|
4
4
|
|
|
5
5
|
// it's likely-ish that `(TActor & { src: TSrc })['logic']` would be faster
|
|
6
6
|
// but it's only possible to do it since https://github.com/microsoft/TypeScript/pull/53098 (TS 5.1)
|
|
@@ -69,8 +69,8 @@ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
|
|
|
69
69
|
context: snapshot.context,
|
|
70
70
|
event: actionArgs.event,
|
|
71
71
|
spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
|
|
72
|
-
self: actorScope
|
|
73
|
-
system: actorScope
|
|
72
|
+
self: actorScope.self,
|
|
73
|
+
system: actorScope.system
|
|
74
74
|
};
|
|
75
75
|
let partialUpdate = {};
|
|
76
76
|
if (typeof assignment === 'function') {
|
|
@@ -93,7 +93,34 @@ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
|
|
|
93
93
|
/**
|
|
94
94
|
* Updates the current context of the machine.
|
|
95
95
|
*
|
|
96
|
-
* @param assignment An object that represents the partial context to update
|
|
96
|
+
* @param assignment An object that represents the partial context to update, or a
|
|
97
|
+
* function that returns an object that represents the partial context to update.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
```ts
|
|
101
|
+
import { createMachine, assign } from 'xstate';
|
|
102
|
+
|
|
103
|
+
const countMachine = createMachine({
|
|
104
|
+
context: {
|
|
105
|
+
count: 0,
|
|
106
|
+
message: ''
|
|
107
|
+
},
|
|
108
|
+
on: {
|
|
109
|
+
inc: {
|
|
110
|
+
actions: assign({
|
|
111
|
+
count: ({ context }) => context.count + 1
|
|
112
|
+
})
|
|
113
|
+
},
|
|
114
|
+
updateMessage: {
|
|
115
|
+
actions: assign(({ context, event }) => {
|
|
116
|
+
return {
|
|
117
|
+
message: event.message.trim()
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
```
|
|
97
124
|
*/
|
|
98
125
|
function assign(assignment) {
|
|
99
126
|
function assign(args, params) {
|
|
@@ -143,7 +170,9 @@ function assign(assignment) {
|
|
|
143
170
|
/** @deprecated use `AnyMachineSnapshot` instead */
|
|
144
171
|
|
|
145
172
|
// TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
|
|
146
|
-
|
|
173
|
+
/**
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
147
176
|
let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
|
|
148
177
|
SpecialTargets["Parent"] = "#_parent";
|
|
149
178
|
SpecialTargets["Internal"] = "#_internal";
|
|
@@ -156,6 +185,14 @@ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
|
|
|
156
185
|
|
|
157
186
|
// Based on RxJS types
|
|
158
187
|
|
|
188
|
+
/**
|
|
189
|
+
* @internal
|
|
190
|
+
*/
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @internal
|
|
194
|
+
*/
|
|
195
|
+
|
|
159
196
|
/**
|
|
160
197
|
* @deprecated Use `Actor<T>` instead.
|
|
161
198
|
*/
|
|
@@ -193,9 +230,9 @@ function resolveSendTo(actorScope, snapshot, args, actionParams, {
|
|
|
193
230
|
let targetActorRef;
|
|
194
231
|
if (typeof resolvedTarget === 'string') {
|
|
195
232
|
if (resolvedTarget === SpecialTargets.Parent) {
|
|
196
|
-
targetActorRef = actorScope
|
|
233
|
+
targetActorRef = actorScope.self._parent;
|
|
197
234
|
} else if (resolvedTarget === SpecialTargets.Internal) {
|
|
198
|
-
targetActorRef = actorScope
|
|
235
|
+
targetActorRef = actorScope.self;
|
|
199
236
|
} else if (resolvedTarget.startsWith('#_')) {
|
|
200
237
|
// SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
|
|
201
238
|
// #_invokeid. If the target is the special term '#_invokeid', where invokeid is the invokeid of an SCXML session that the sending session has created by <invoke>, the Processor must add the event to the external queue of that session.
|
|
@@ -207,7 +244,7 @@ function resolveSendTo(actorScope, snapshot, args, actionParams, {
|
|
|
207
244
|
throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${snapshot.machine.id}'.`);
|
|
208
245
|
}
|
|
209
246
|
} else {
|
|
210
|
-
targetActorRef = resolvedTarget || actorScope
|
|
247
|
+
targetActorRef = resolvedTarget || actorScope.self;
|
|
211
248
|
}
|
|
212
249
|
return [snapshot, {
|
|
213
250
|
to: targetActorRef,
|
|
@@ -277,9 +314,9 @@ function sendParent(event, options) {
|
|
|
277
314
|
return sendTo(SpecialTargets.Parent, event, options);
|
|
278
315
|
}
|
|
279
316
|
/**
|
|
280
|
-
* Forwards (sends) an event to
|
|
317
|
+
* Forwards (sends) an event to the `target` actor.
|
|
281
318
|
*
|
|
282
|
-
* @param target The target
|
|
319
|
+
* @param target The target actor to forward the event to.
|
|
283
320
|
* @param options Options to pass into the send action creator.
|
|
284
321
|
*/
|
|
285
322
|
function forwardTo(target, options) {
|
|
@@ -333,6 +370,26 @@ function resolveEnqueueActions(actorScope, snapshot, args, _actionParams, {
|
|
|
333
370
|
});
|
|
334
371
|
return [snapshot, undefined, actions];
|
|
335
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Creates an action object that will execute actions that are queued by the `enqueue(action)` function.
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
```ts
|
|
378
|
+
import { createMachine, enqueueActions } from 'xstate';
|
|
379
|
+
|
|
380
|
+
const machine = createMachine({
|
|
381
|
+
entry: enqueueActions(({ enqueue, check }) => {
|
|
382
|
+
enqueue.assign({ count: 0 });
|
|
383
|
+
|
|
384
|
+
if (check('someGuard')) {
|
|
385
|
+
enqueue.assign({ count: 1 });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
enqueue('someAction');
|
|
389
|
+
})
|
|
390
|
+
})
|
|
391
|
+
```
|
|
392
|
+
*/
|
|
336
393
|
function enqueueActions(collect) {
|
|
337
394
|
function enqueueActions(args, params) {
|
|
338
395
|
{
|
|
@@ -431,6 +431,7 @@ class Actor {
|
|
|
431
431
|
this._processingStatus = ProcessingStatus.NotStarted;
|
|
432
432
|
// Actor Ref
|
|
433
433
|
this._parent = void 0;
|
|
434
|
+
/** @internal */
|
|
434
435
|
this._syncSnapshot = void 0;
|
|
435
436
|
this.ref = void 0;
|
|
436
437
|
// TODO: add typings for system
|
|
@@ -972,10 +973,8 @@ class Actor {
|
|
|
972
973
|
* @param logic - The actor logic to create an actor from. For a state machine actor logic creator, see {@link createMachine}. Other actor logic creators include {@link fromCallback}, {@link fromEventObservable}, {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
|
|
973
974
|
* @param options - Actor options
|
|
974
975
|
*/
|
|
975
|
-
|
|
976
976
|
function createActor(logic, options) {
|
|
977
|
-
|
|
978
|
-
return interpreter;
|
|
977
|
+
return new Actor(logic, options);
|
|
979
978
|
}
|
|
980
979
|
|
|
981
980
|
/**
|
|
@@ -1001,11 +1000,30 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1001
1000
|
});
|
|
1002
1001
|
}
|
|
1003
1002
|
/**
|
|
1004
|
-
* Cancels
|
|
1005
|
-
*
|
|
1006
|
-
* (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
|
|
1003
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The canceled `sendTo(...)` action
|
|
1004
|
+
* will not send its event or execute, unless the `delay` has already elapsed before `cancel(...)` is called.
|
|
1007
1005
|
*
|
|
1008
|
-
* @param sendId The `id` of the `
|
|
1006
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1007
|
+
*
|
|
1008
|
+
* @example
|
|
1009
|
+
```ts
|
|
1010
|
+
import { createMachine, sendTo, cancel } from 'xstate';
|
|
1011
|
+
|
|
1012
|
+
const machine = createMachine({
|
|
1013
|
+
// ...
|
|
1014
|
+
on: {
|
|
1015
|
+
sendEvent: {
|
|
1016
|
+
actions: sendTo('some-actor', { type: 'someEvent' }, {
|
|
1017
|
+
id: 'some-id',
|
|
1018
|
+
delay: 1000
|
|
1019
|
+
})
|
|
1020
|
+
},
|
|
1021
|
+
cancelEvent: {
|
|
1022
|
+
actions: cancel('some-id')
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
```
|
|
1009
1027
|
*/
|
|
1010
1028
|
function cancel(sendId) {
|
|
1011
1029
|
function cancel(args, params) {
|
|
@@ -1031,13 +1049,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
|
|
|
1031
1049
|
actorRef = createActor(logic, {
|
|
1032
1050
|
id: resolvedId,
|
|
1033
1051
|
src,
|
|
1034
|
-
parent: actorScope
|
|
1052
|
+
parent: actorScope.self,
|
|
1035
1053
|
syncSnapshot,
|
|
1036
1054
|
systemId,
|
|
1037
1055
|
input: typeof input === 'function' ? input({
|
|
1038
1056
|
context: snapshot.context,
|
|
1039
1057
|
event: actionArgs.event,
|
|
1040
|
-
self: actorScope
|
|
1058
|
+
self: actorScope.self
|
|
1041
1059
|
}) : input
|
|
1042
1060
|
});
|
|
1043
1061
|
}
|
|
@@ -1171,6 +1189,33 @@ function checkNot(snapshot, {
|
|
|
1171
1189
|
}) {
|
|
1172
1190
|
return !evaluateGuard(guards[0], context, event, snapshot);
|
|
1173
1191
|
}
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it evaluates to `false`.
|
|
1195
|
+
*
|
|
1196
|
+
* @category Guards
|
|
1197
|
+
* @example
|
|
1198
|
+
```ts
|
|
1199
|
+
import { setup, not } from 'xstate';
|
|
1200
|
+
|
|
1201
|
+
const machine = setup({
|
|
1202
|
+
guards: {
|
|
1203
|
+
someNamedGuard: () => false
|
|
1204
|
+
}
|
|
1205
|
+
}).createMachine({
|
|
1206
|
+
on: {
|
|
1207
|
+
someEvent: {
|
|
1208
|
+
guard: not('someNamedGuard'),
|
|
1209
|
+
actions: () => {
|
|
1210
|
+
// will be executed if guard in `not(...)`
|
|
1211
|
+
// evaluates to `false`
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
```
|
|
1217
|
+
* @returns A guard
|
|
1218
|
+
*/
|
|
1174
1219
|
function not(guard) {
|
|
1175
1220
|
function not(args, params) {
|
|
1176
1221
|
return false;
|
|
@@ -1187,6 +1232,37 @@ function checkAnd(snapshot, {
|
|
|
1187
1232
|
}) {
|
|
1188
1233
|
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1189
1234
|
}
|
|
1235
|
+
|
|
1236
|
+
/**
|
|
1237
|
+
* Higher-order guard that evaluates to `true` if all `guards` passed to it
|
|
1238
|
+
* evaluate to `true`.
|
|
1239
|
+
*
|
|
1240
|
+
* @category Guards
|
|
1241
|
+
* @example
|
|
1242
|
+
```ts
|
|
1243
|
+
import { setup, and } from 'xstate';
|
|
1244
|
+
|
|
1245
|
+
const machine = setup({
|
|
1246
|
+
guards: {
|
|
1247
|
+
someNamedGuard: () => true
|
|
1248
|
+
}
|
|
1249
|
+
}).createMachine({
|
|
1250
|
+
on: {
|
|
1251
|
+
someEvent: {
|
|
1252
|
+
guard: and([
|
|
1253
|
+
({ context }) => context.value > 0,
|
|
1254
|
+
'someNamedGuard'
|
|
1255
|
+
]),
|
|
1256
|
+
actions: () => {
|
|
1257
|
+
// will be executed if all guards in `and(...)`
|
|
1258
|
+
// evaluate to true
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
});
|
|
1263
|
+
```
|
|
1264
|
+
* @returns A guard action object
|
|
1265
|
+
*/
|
|
1190
1266
|
function and(guards) {
|
|
1191
1267
|
function and(args, params) {
|
|
1192
1268
|
return false;
|
|
@@ -1203,6 +1279,37 @@ function checkOr(snapshot, {
|
|
|
1203
1279
|
}) {
|
|
1204
1280
|
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1205
1281
|
}
|
|
1282
|
+
|
|
1283
|
+
/**
|
|
1284
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to it
|
|
1285
|
+
* evaluate to `true`.
|
|
1286
|
+
*
|
|
1287
|
+
* @category Guards
|
|
1288
|
+
* @example
|
|
1289
|
+
```ts
|
|
1290
|
+
import { setup, or } from 'xstate';
|
|
1291
|
+
|
|
1292
|
+
const machine = setup({
|
|
1293
|
+
guards: {
|
|
1294
|
+
someNamedGuard: () => true
|
|
1295
|
+
}
|
|
1296
|
+
}).createMachine({
|
|
1297
|
+
on: {
|
|
1298
|
+
someEvent: {
|
|
1299
|
+
guard: or([
|
|
1300
|
+
({ context }) => context.value > 0,
|
|
1301
|
+
'someNamedGuard'
|
|
1302
|
+
]),
|
|
1303
|
+
actions: () => {
|
|
1304
|
+
// will be executed if any of the guards in `or(...)`
|
|
1305
|
+
// evaluate to true
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
});
|
|
1310
|
+
```
|
|
1311
|
+
* @returns A guard action object
|
|
1312
|
+
*/
|
|
1206
1313
|
function or(guards) {
|
|
1207
1314
|
function or(args, params) {
|
|
1208
1315
|
return false;
|
|
@@ -2047,18 +2154,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2047
2154
|
const actionArgs = {
|
|
2048
2155
|
context: intermediateSnapshot.context,
|
|
2049
2156
|
event,
|
|
2050
|
-
self: actorScope
|
|
2051
|
-
system: actorScope
|
|
2157
|
+
self: actorScope.self,
|
|
2158
|
+
system: actorScope.system
|
|
2052
2159
|
};
|
|
2053
2160
|
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
|
|
2054
2161
|
context: intermediateSnapshot.context,
|
|
2055
2162
|
event
|
|
2056
2163
|
}) : action.params : undefined;
|
|
2057
2164
|
if (!('resolve' in resolvedAction)) {
|
|
2058
|
-
if (actorScope
|
|
2165
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2059
2166
|
resolvedAction(actionArgs, actionParams);
|
|
2060
2167
|
} else {
|
|
2061
|
-
actorScope
|
|
2168
|
+
actorScope.defer(() => {
|
|
2062
2169
|
resolvedAction(actionArgs, actionParams);
|
|
2063
2170
|
});
|
|
2064
2171
|
}
|
|
@@ -2073,10 +2180,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2073
2180
|
retries?.push([builtinAction, params]);
|
|
2074
2181
|
}
|
|
2075
2182
|
if ('execute' in builtinAction) {
|
|
2076
|
-
if (actorScope
|
|
2183
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2077
2184
|
builtinAction.execute(actorScope, params);
|
|
2078
2185
|
} else {
|
|
2079
|
-
actorScope
|
|
2186
|
+
actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
|
|
2080
2187
|
}
|
|
2081
2188
|
}
|
|
2082
2189
|
if (actions) {
|
|
@@ -2133,7 +2240,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
|
|
|
2133
2240
|
microstates: states
|
|
2134
2241
|
};
|
|
2135
2242
|
}
|
|
2136
|
-
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2243
|
+
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2244
|
+
// isInitial
|
|
2245
|
+
internalQueue);
|
|
2137
2246
|
states.push(nextSnapshot);
|
|
2138
2247
|
}
|
|
2139
2248
|
let shouldSelectEventlessTransitions = true;
|
|
@@ -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
|
|
@@ -988,10 +989,8 @@ class Actor {
|
|
|
988
989
|
* @param logic - The actor logic to create an actor from. For a state machine actor logic creator, see {@link createMachine}. Other actor logic creators include {@link fromCallback}, {@link fromEventObservable}, {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
|
|
989
990
|
* @param options - Actor options
|
|
990
991
|
*/
|
|
991
|
-
|
|
992
992
|
function createActor(logic, options) {
|
|
993
|
-
|
|
994
|
-
return interpreter;
|
|
993
|
+
return new Actor(logic, options);
|
|
995
994
|
}
|
|
996
995
|
|
|
997
996
|
/**
|
|
@@ -1017,11 +1016,30 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1017
1016
|
});
|
|
1018
1017
|
}
|
|
1019
1018
|
/**
|
|
1020
|
-
* Cancels
|
|
1021
|
-
*
|
|
1022
|
-
* (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.
|
|
1023
1021
|
*
|
|
1024
|
-
* @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
|
+
```
|
|
1025
1043
|
*/
|
|
1026
1044
|
function cancel(sendId) {
|
|
1027
1045
|
function cancel(args, params) {
|
|
@@ -1050,13 +1068,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
|
|
|
1050
1068
|
actorRef = createActor(logic, {
|
|
1051
1069
|
id: resolvedId,
|
|
1052
1070
|
src,
|
|
1053
|
-
parent: actorScope
|
|
1071
|
+
parent: actorScope.self,
|
|
1054
1072
|
syncSnapshot,
|
|
1055
1073
|
systemId,
|
|
1056
1074
|
input: typeof input === 'function' ? input({
|
|
1057
1075
|
context: snapshot.context,
|
|
1058
1076
|
event: actionArgs.event,
|
|
1059
|
-
self: actorScope
|
|
1077
|
+
self: actorScope.self
|
|
1060
1078
|
}) : input
|
|
1061
1079
|
});
|
|
1062
1080
|
}
|
|
@@ -1201,6 +1219,33 @@ function checkNot(snapshot, {
|
|
|
1201
1219
|
}) {
|
|
1202
1220
|
return !evaluateGuard(guards[0], context, event, snapshot);
|
|
1203
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
|
+
*/
|
|
1204
1249
|
function not(guard) {
|
|
1205
1250
|
function not(args, params) {
|
|
1206
1251
|
{
|
|
@@ -1219,6 +1264,37 @@ function checkAnd(snapshot, {
|
|
|
1219
1264
|
}) {
|
|
1220
1265
|
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1221
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
|
+
*/
|
|
1222
1298
|
function and(guards) {
|
|
1223
1299
|
function and(args, params) {
|
|
1224
1300
|
{
|
|
@@ -1237,6 +1313,37 @@ function checkOr(snapshot, {
|
|
|
1237
1313
|
}) {
|
|
1238
1314
|
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
|
|
1239
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
|
+
*/
|
|
1240
1347
|
function or(guards) {
|
|
1241
1348
|
function or(args, params) {
|
|
1242
1349
|
{
|
|
@@ -2094,18 +2201,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2094
2201
|
const actionArgs = {
|
|
2095
2202
|
context: intermediateSnapshot.context,
|
|
2096
2203
|
event,
|
|
2097
|
-
self: actorScope
|
|
2098
|
-
system: actorScope
|
|
2204
|
+
self: actorScope.self,
|
|
2205
|
+
system: actorScope.system
|
|
2099
2206
|
};
|
|
2100
2207
|
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
|
|
2101
2208
|
context: intermediateSnapshot.context,
|
|
2102
2209
|
event
|
|
2103
2210
|
}) : action.params : undefined;
|
|
2104
2211
|
if (!('resolve' in resolvedAction)) {
|
|
2105
|
-
if (actorScope
|
|
2212
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2106
2213
|
resolvedAction(actionArgs, actionParams);
|
|
2107
2214
|
} else {
|
|
2108
|
-
actorScope
|
|
2215
|
+
actorScope.defer(() => {
|
|
2109
2216
|
resolvedAction(actionArgs, actionParams);
|
|
2110
2217
|
});
|
|
2111
2218
|
}
|
|
@@ -2120,10 +2227,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
|
|
|
2120
2227
|
retries?.push([builtinAction, params]);
|
|
2121
2228
|
}
|
|
2122
2229
|
if ('execute' in builtinAction) {
|
|
2123
|
-
if (actorScope
|
|
2230
|
+
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
|
|
2124
2231
|
builtinAction.execute(actorScope, params);
|
|
2125
2232
|
} else {
|
|
2126
|
-
actorScope
|
|
2233
|
+
actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
|
|
2127
2234
|
}
|
|
2128
2235
|
}
|
|
2129
2236
|
if (actions) {
|
|
@@ -2183,7 +2290,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
|
|
|
2183
2290
|
microstates: states
|
|
2184
2291
|
};
|
|
2185
2292
|
}
|
|
2186
|
-
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2293
|
+
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
|
|
2294
|
+
// isInitial
|
|
2295
|
+
internalQueue);
|
|
2187
2296
|
states.push(nextSnapshot);
|
|
2188
2297
|
}
|
|
2189
2298
|
let shouldSelectEventlessTransitions = true;
|