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.
Files changed (48) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/declarations/src/State.d.ts +43 -17
  13. package/dist/declarations/src/StateMachine.d.ts +16 -15
  14. package/dist/declarations/src/StateNode.d.ts +0 -4
  15. package/dist/declarations/src/actions/assign.d.ts +28 -1
  16. package/dist/declarations/src/actions/cancel.d.ts +23 -4
  17. package/dist/declarations/src/actions/enqueueActions.d.ts +20 -0
  18. package/dist/declarations/src/actions/send.d.ts +2 -2
  19. package/dist/declarations/src/{interpreter.d.ts → createActor.d.ts} +2 -4
  20. package/dist/declarations/src/createMachine.d.ts +40 -0
  21. package/dist/declarations/src/getNextSnapshot.d.ts +32 -0
  22. package/dist/declarations/src/guards.d.ts +86 -0
  23. package/dist/declarations/src/index.d.ts +2 -1
  24. package/dist/declarations/src/spawn.d.ts +2 -2
  25. package/dist/declarations/src/typegenTypes.d.ts +26 -14
  26. package/dist/declarations/src/types.d.ts +17 -36
  27. package/dist/{log-e870aec8.esm.js → log-22b3587f.esm.js} +67 -10
  28. package/dist/{log-580765a2.development.esm.js → log-285f62db.development.esm.js} +67 -10
  29. package/dist/{log-a32b44b3.cjs.js → log-742895c6.cjs.js} +67 -10
  30. package/dist/{log-cd3d7c14.development.cjs.js → log-da322832.development.cjs.js} +67 -10
  31. package/dist/{raise-7df513e7.esm.js → raise-0e64ee6e.esm.js} +125 -16
  32. package/dist/{raise-e3ff3de1.development.cjs.js → raise-7af39710.development.cjs.js} +125 -16
  33. package/dist/{raise-0fc3a80c.development.esm.js → raise-8da27ebb.development.esm.js} +125 -16
  34. package/dist/{raise-c3bbdd3a.cjs.js → raise-ad8bb7c2.cjs.js} +125 -16
  35. package/dist/xstate.cjs.js +105 -4
  36. package/dist/xstate.cjs.mjs +1 -0
  37. package/dist/xstate.development.cjs.js +105 -4
  38. package/dist/xstate.development.cjs.mjs +1 -0
  39. package/dist/xstate.development.esm.js +107 -7
  40. package/dist/xstate.esm.js +107 -7
  41. package/dist/xstate.umd.min.js +1 -1
  42. package/dist/xstate.umd.min.js.map +1 -1
  43. package/guards/dist/xstate-guards.cjs.js +1 -1
  44. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  45. package/guards/dist/xstate-guards.development.esm.js +1 -1
  46. package/guards/dist/xstate-guards.esm.js +1 -1
  47. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  48. package/package.json +1 -1
@@ -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
@@ -986,10 +987,8 @@ class Actor {
986
987
  * @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}.
987
988
  * @param options - Actor options
988
989
  */
989
-
990
990
  function createActor(logic, options) {
991
- const interpreter = new Actor(logic, options);
992
- return interpreter;
991
+ return new Actor(logic, options);
993
992
  }
994
993
 
995
994
  /**
@@ -1015,11 +1014,30 @@ function executeCancel(actorScope, resolvedSendId) {
1015
1014
  });
1016
1015
  }
1017
1016
  /**
1018
- * Cancels an in-flight `send(...)` action. A canceled sent action will not
1019
- * be executed, nor will its event be sent, unless it has already been sent
1020
- * (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.
1021
1019
  *
1022
- * @param sendId The `id` of the `send(...)` action to cancel.
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
+ ```
1023
1041
  */
1024
1042
  function cancel(sendId) {
1025
1043
  function cancel(args, params) {
@@ -1048,13 +1066,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1048
1066
  actorRef = createActor(logic, {
1049
1067
  id: resolvedId,
1050
1068
  src,
1051
- parent: actorScope?.self,
1069
+ parent: actorScope.self,
1052
1070
  syncSnapshot,
1053
1071
  systemId,
1054
1072
  input: typeof input === 'function' ? input({
1055
1073
  context: snapshot.context,
1056
1074
  event: actionArgs.event,
1057
- self: actorScope?.self
1075
+ self: actorScope.self
1058
1076
  }) : input
1059
1077
  });
1060
1078
  }
@@ -1199,6 +1217,33 @@ function checkNot(snapshot, {
1199
1217
  }) {
1200
1218
  return !evaluateGuard(guards[0], context, event, snapshot);
1201
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
+ */
1202
1247
  function not(guard) {
1203
1248
  function not(args, params) {
1204
1249
  {
@@ -1217,6 +1262,37 @@ function checkAnd(snapshot, {
1217
1262
  }) {
1218
1263
  return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
1219
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
+ */
1220
1296
  function and(guards) {
1221
1297
  function and(args, params) {
1222
1298
  {
@@ -1235,6 +1311,37 @@ function checkOr(snapshot, {
1235
1311
  }) {
1236
1312
  return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
1237
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
+ */
1238
1345
  function or(guards) {
1239
1346
  function or(args, params) {
1240
1347
  {
@@ -2092,18 +2199,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
2092
2199
  const actionArgs = {
2093
2200
  context: intermediateSnapshot.context,
2094
2201
  event,
2095
- self: actorScope?.self,
2096
- system: actorScope?.system
2202
+ self: actorScope.self,
2203
+ system: actorScope.system
2097
2204
  };
2098
2205
  const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
2099
2206
  context: intermediateSnapshot.context,
2100
2207
  event
2101
2208
  }) : action.params : undefined;
2102
2209
  if (!('resolve' in resolvedAction)) {
2103
- if (actorScope?.self._processingStatus === ProcessingStatus.Running) {
2210
+ if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2104
2211
  resolvedAction(actionArgs, actionParams);
2105
2212
  } else {
2106
- actorScope?.defer(() => {
2213
+ actorScope.defer(() => {
2107
2214
  resolvedAction(actionArgs, actionParams);
2108
2215
  });
2109
2216
  }
@@ -2118,10 +2225,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
2118
2225
  retries?.push([builtinAction, params]);
2119
2226
  }
2120
2227
  if ('execute' in builtinAction) {
2121
- if (actorScope?.self._processingStatus === ProcessingStatus.Running) {
2228
+ if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2122
2229
  builtinAction.execute(actorScope, params);
2123
2230
  } else {
2124
- actorScope?.defer(builtinAction.execute.bind(null, actorScope, params));
2231
+ actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
2125
2232
  }
2126
2233
  }
2127
2234
  if (actions) {
@@ -2181,7 +2288,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
2181
2288
  microstates: states
2182
2289
  };
2183
2290
  }
2184
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false, internalQueue);
2291
+ nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2292
+ // isInitial
2293
+ internalQueue);
2185
2294
  states.push(nextSnapshot);
2186
2295
  }
2187
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
@@ -974,10 +975,8 @@ class Actor {
974
975
  * @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}.
975
976
  * @param options - Actor options
976
977
  */
977
-
978
978
  function createActor(logic, options) {
979
- const interpreter = new Actor(logic, options);
980
- return interpreter;
979
+ return new Actor(logic, options);
981
980
  }
982
981
 
983
982
  /**
@@ -1003,11 +1002,30 @@ function executeCancel(actorScope, resolvedSendId) {
1003
1002
  });
1004
1003
  }
1005
1004
  /**
1006
- * Cancels an in-flight `send(...)` action. A canceled sent action will not
1007
- * be executed, nor will its event be sent, unless it has already been sent
1008
- * (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.
1009
1007
  *
1010
- * @param sendId The `id` of the `send(...)` action to cancel.
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
+ ```
1011
1029
  */
1012
1030
  function cancel(sendId) {
1013
1031
  function cancel(args, params) {
@@ -1033,13 +1051,13 @@ function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
1033
1051
  actorRef = createActor(logic, {
1034
1052
  id: resolvedId,
1035
1053
  src,
1036
- parent: actorScope?.self,
1054
+ parent: actorScope.self,
1037
1055
  syncSnapshot,
1038
1056
  systemId,
1039
1057
  input: typeof input === 'function' ? input({
1040
1058
  context: snapshot.context,
1041
1059
  event: actionArgs.event,
1042
- self: actorScope?.self
1060
+ self: actorScope.self
1043
1061
  }) : input
1044
1062
  });
1045
1063
  }
@@ -1173,6 +1191,33 @@ function checkNot(snapshot, {
1173
1191
  }) {
1174
1192
  return !evaluateGuard(guards[0], context, event, snapshot);
1175
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
+ */
1176
1221
  function not(guard) {
1177
1222
  function not(args, params) {
1178
1223
  return false;
@@ -1189,6 +1234,37 @@ function checkAnd(snapshot, {
1189
1234
  }) {
1190
1235
  return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
1191
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
+ */
1192
1268
  function and(guards) {
1193
1269
  function and(args, params) {
1194
1270
  return false;
@@ -1205,6 +1281,37 @@ function checkOr(snapshot, {
1205
1281
  }) {
1206
1282
  return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
1207
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
+ */
1208
1315
  function or(guards) {
1209
1316
  function or(args, params) {
1210
1317
  return false;
@@ -2049,18 +2156,18 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
2049
2156
  const actionArgs = {
2050
2157
  context: intermediateSnapshot.context,
2051
2158
  event,
2052
- self: actorScope?.self,
2053
- system: actorScope?.system
2159
+ self: actorScope.self,
2160
+ system: actorScope.system
2054
2161
  };
2055
2162
  const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
2056
2163
  context: intermediateSnapshot.context,
2057
2164
  event
2058
2165
  }) : action.params : undefined;
2059
2166
  if (!('resolve' in resolvedAction)) {
2060
- if (actorScope?.self._processingStatus === ProcessingStatus.Running) {
2167
+ if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2061
2168
  resolvedAction(actionArgs, actionParams);
2062
2169
  } else {
2063
- actorScope?.defer(() => {
2170
+ actorScope.defer(() => {
2064
2171
  resolvedAction(actionArgs, actionParams);
2065
2172
  });
2066
2173
  }
@@ -2075,10 +2182,10 @@ function resolveActionsAndContextWorker(currentSnapshot, event, actorScope, acti
2075
2182
  retries?.push([builtinAction, params]);
2076
2183
  }
2077
2184
  if ('execute' in builtinAction) {
2078
- if (actorScope?.self._processingStatus === ProcessingStatus.Running) {
2185
+ if (actorScope.self._processingStatus === ProcessingStatus.Running) {
2079
2186
  builtinAction.execute(actorScope, params);
2080
2187
  } else {
2081
- actorScope?.defer(builtinAction.execute.bind(null, actorScope, params));
2188
+ actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
2082
2189
  }
2083
2190
  }
2084
2191
  if (actions) {
@@ -2135,7 +2242,9 @@ function macrostep(snapshot, event, actorScope, internalQueue = []) {
2135
2242
  microstates: states
2136
2243
  };
2137
2244
  }
2138
- nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false, internalQueue);
2245
+ nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
2246
+ // isInitial
2247
+ internalQueue);
2139
2248
  states.push(nextSnapshot);
2140
2249
  }
2141
2250
  let shouldSelectEventlessTransitions = true;
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var actors_dist_xstateActors = require('../actors/dist/xstate-actors.cjs.js');
6
- var guards_dist_xstateGuards = require('./raise-c3bbdd3a.cjs.js');
7
- var log = require('./log-a32b44b3.cjs.js');
6
+ var guards_dist_xstateGuards = require('./raise-ad8bb7c2.cjs.js');
7
+ var log = require('./log-742895c6.cjs.js');
8
8
  require('../dev/dist/xstate-dev.cjs.js');
9
9
 
10
10
  class SimulatedClock {
@@ -207,6 +207,8 @@ class StateNode {
207
207
  this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
208
208
  this.tags = guards_dist_xstateGuards.toArray(config.tags).slice();
209
209
  }
210
+
211
+ /** @internal */
210
212
  _initialize() {
211
213
  this.transitions = guards_dist_xstateGuards.formatTransitions(this);
212
214
  if (this.config.always) {
@@ -258,6 +260,8 @@ class StateNode {
258
260
  tags: this.tags
259
261
  };
260
262
  }
263
+
264
+ /** @internal */
261
265
  toJSON() {
262
266
  return this.definition;
263
267
  }
@@ -314,6 +318,8 @@ class StateNode {
314
318
  get initial() {
315
319
  return memo(this, 'initial', () => guards_dist_xstateGuards.formatInitialTransition(this, this.config.initial));
316
320
  }
321
+
322
+ /** @internal */
317
323
  next(snapshot, event) {
318
324
  const eventType = event.type;
319
325
  const actions = [];
@@ -389,13 +395,17 @@ class StateMachine {
389
395
  */
390
396
  this.version = void 0;
391
397
  this.implementations = void 0;
398
+ /** @internal */
392
399
  this.__xstatenode = true;
400
+ /** @internal */
393
401
  this.idMap = new Map();
394
402
  this.root = void 0;
395
403
  this.id = void 0;
396
404
  this.states = void 0;
397
405
  this.events = void 0;
398
- /** @deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
406
+ /**
407
+ * @deprecated an internal property that was acting as a "phantom" type, it's not used by anything right now but it's kept around for compatibility reasons
408
+ **/
399
409
  this.__TResolvedTypesMeta = void 0;
400
410
  this.id = config.id || '(machine)';
401
411
  this.implementations = {
@@ -581,7 +591,7 @@ class StateMachine {
581
591
  }
582
592
  const actorRef = guards_dist_xstateGuards.createActor(logic, {
583
593
  id: actorId,
584
- parent: _actorScope?.self,
594
+ parent: _actorScope.self,
585
595
  syncSnapshot: actorData.syncSnapshot,
586
596
  snapshot: childState,
587
597
  src,
@@ -690,10 +700,100 @@ function waitFor(actorRef, predicate, options) {
690
700
  // this is not 100% accurate since we can't make parallel regions required in the result
691
701
  // `TTestValue` doesn't encode this information anyhow for us to be able to do that
692
702
  // this is fine for most practical use cases anyway though
703
+ /**
704
+ * Creates a state machine (statechart) with the given configuration.
705
+ *
706
+ * The state machine represents the pure logic of a state machine actor.
707
+ *
708
+ * @param config The state machine configuration.
709
+ * @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })` to provide machine implementations instead.
710
+ *
711
+ * @example
712
+ ```ts
713
+ import { createMachine } from 'xstate';
714
+
715
+ const lightMachine = createMachine({
716
+ id: 'light',
717
+ initial: 'green',
718
+ states: {
719
+ green: {
720
+ on: {
721
+ TIMER: { target: 'yellow' }
722
+ }
723
+ },
724
+ yellow: {
725
+ on: {
726
+ TIMER: { target: 'red' }
727
+ }
728
+ },
729
+ red: {
730
+ on: {
731
+ TIMER: { target: 'green' }
732
+ }
733
+ }
734
+ }
735
+ });
736
+
737
+ const lightActor = createActor(lightMachine);
738
+ lightActor.start();
739
+
740
+ lightActor.send({ type: 'TIMER' });
741
+ ```
742
+ */
693
743
  function createMachine(config, implementations) {
694
744
  return new StateMachine(config, implementations);
695
745
  }
696
746
 
747
+ /** @internal */
748
+ function createInertActorScope(actorLogic) {
749
+ const self = guards_dist_xstateGuards.createActor(actorLogic);
750
+ const inertActorScope = {
751
+ self,
752
+ defer: () => {},
753
+ id: '',
754
+ logger: () => {},
755
+ sessionId: '',
756
+ stopChild: () => {},
757
+ system: self.system
758
+ };
759
+ return inertActorScope;
760
+ }
761
+
762
+ /**
763
+ * Determines the next snapshot for the given `actorLogic` based on
764
+ * the given `snapshot` and `event`.
765
+ *
766
+ * If the `snapshot` is `undefined`, the initial snapshot of the
767
+ * `actorLogic` is used.
768
+ *
769
+ * @example
770
+ ```ts
771
+ import { getNextSnapshot } from 'xstate';
772
+ import { trafficLightMachine } from './trafficLightMachine.ts';
773
+
774
+ const nextSnapshot = getNextSnapshot(
775
+ trafficLightMachine, // actor logic
776
+ undefined, // snapshot (or initial state if undefined)
777
+ { type: 'TIMER' }); // event object
778
+
779
+ console.log(nextSnapshot.value);
780
+ // => 'yellow'
781
+
782
+ const nextSnapshot2 = getNextSnapshot(
783
+ trafficLightMachine, // actor logic
784
+ nextSnapshot, // snapshot
785
+ { type: 'TIMER' }); // event object
786
+
787
+ console.log(nextSnapshot2.value);
788
+ // =>'red'
789
+ ```
790
+ */
791
+ function getNextSnapshot(actorLogic, snapshot, event) {
792
+ const inertActorScope = createInertActorScope(actorLogic);
793
+ inertActorScope.self._snapshot = snapshot;
794
+ return actorLogic.transition(snapshot, event, inertActorScope);
795
+ }
796
+
697
797
  // at the moment we allow extra actors - ones that are not specified by `children`
698
798
  // this could be reconsidered in the future
699
799
  function setup({
@@ -811,6 +911,7 @@ exports.StateMachine = StateMachine;
811
911
  exports.StateNode = StateNode;
812
912
  exports.assertEvent = assertEvent;
813
913
  exports.createMachine = createMachine;
914
+ exports.getNextSnapshot = getNextSnapshot;
814
915
  exports.setup = setup;
815
916
  exports.toPromise = toPromise;
816
917
  exports.waitFor = waitFor;
@@ -19,6 +19,7 @@ export {
19
19
  fromObservable,
20
20
  fromPromise,
21
21
  fromTransition,
22
+ getNextSnapshot,
22
23
  getStateNodes,
23
24
  interpret,
24
25
  isMachineSnapshot,