xstate 5.0.0-beta.40 → 5.0.0-beta.42

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 (46) hide show
  1. package/actions/dist/xstate-actions.cjs.js +3 -3
  2. package/actions/dist/xstate-actions.development.cjs.js +3 -3
  3. package/actions/dist/xstate-actions.development.esm.js +3 -3
  4. package/actions/dist/xstate-actions.esm.js +3 -3
  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 +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/declarations/src/State.d.ts +36 -43
  14. package/dist/declarations/src/StateMachine.d.ts +13 -30
  15. package/dist/declarations/src/StateNode.d.ts +2 -2
  16. package/dist/declarations/src/index.d.ts +4 -5
  17. package/dist/declarations/src/interpreter.d.ts +14 -10
  18. package/dist/declarations/src/stateUtils.d.ts +7 -8
  19. package/dist/declarations/src/types.d.ts +114 -22
  20. package/dist/{interpreter-bae5c279.development.cjs.js → interpreter-23e4041c.development.cjs.js} +49 -42
  21. package/dist/{interpreter-ed0fac7e.esm.js → interpreter-3d0c0ff2.esm.js} +49 -40
  22. package/dist/{interpreter-586abde4.cjs.js → interpreter-b6bdd134.cjs.js} +49 -42
  23. package/dist/{interpreter-410d7ca9.development.esm.js → interpreter-f2620ea7.development.esm.js} +49 -40
  24. package/dist/{raise-37f9f3b8.development.esm.js → raise-51ae36e5.development.esm.js} +84 -183
  25. package/dist/{raise-27909189.cjs.js → raise-6b64c553.cjs.js} +81 -181
  26. package/dist/{raise-8325e2df.development.cjs.js → raise-8f482ce9.development.cjs.js} +85 -185
  27. package/dist/{raise-2b2fdec3.esm.js → raise-d2084327.esm.js} +80 -179
  28. package/dist/{send-59f66c58.esm.js → send-4e732fa5.esm.js} +7 -6
  29. package/dist/{send-f6b49072.development.esm.js → send-7a350091.development.esm.js} +7 -6
  30. package/dist/{send-4fdf275e.cjs.js → send-85b562d8.cjs.js} +7 -6
  31. package/dist/{send-c45d0d2c.development.cjs.js → send-cc8f864e.development.cjs.js} +7 -6
  32. package/dist/xstate.cjs.js +27 -56
  33. package/dist/xstate.cjs.mjs +0 -4
  34. package/dist/xstate.development.cjs.js +27 -56
  35. package/dist/xstate.development.cjs.mjs +0 -4
  36. package/dist/xstate.development.esm.js +31 -56
  37. package/dist/xstate.esm.js +31 -56
  38. package/dist/xstate.umd.min.js +1 -1
  39. package/dist/xstate.umd.min.js.map +1 -1
  40. package/guards/dist/xstate-guards.cjs.js +2 -2
  41. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  42. package/guards/dist/xstate-guards.development.esm.js +2 -2
  43. package/guards/dist/xstate-guards.esm.js +2 -2
  44. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  45. package/package.json +1 -1
  46. package/dist/declarations/src/mapState.d.ts +0 -3
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var interpreter = require('./interpreter-586abde4.cjs.js');
3
+ var interpreter = require('./interpreter-b6bdd134.cjs.js');
4
4
 
5
5
  const cache = new WeakMap();
6
6
  function memo(object, key, fn) {
@@ -60,7 +60,7 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
60
60
  const configuredInput = input || referenced.input;
61
61
  actorRef = interpreter.createActor(referenced.src, {
62
62
  id: resolvedId,
63
- src: typeof src === 'string' ? src : undefined,
63
+ src,
64
64
  parent: actorScope?.self,
65
65
  systemId,
66
66
  input: typeof configuredInput === 'function' ? configuredInput({
@@ -83,7 +83,7 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
83
83
  });
84
84
  }
85
85
  }
86
- return [cloneState(state, {
86
+ return [cloneMachineSnapshot(state, {
87
87
  children: {
88
88
  ...state.children,
89
89
  [resolvedId]: actorRef
@@ -101,7 +101,7 @@ function executeSpawn(actorScope, {
101
101
  return;
102
102
  }
103
103
  actorScope.defer(() => {
104
- if (actorRef.status === interpreter.ActorStatus.Stopped) {
104
+ if (actorRef._processingStatus === interpreter.ProcessingStatus.Stopped) {
105
105
  return;
106
106
  }
107
107
  try {
@@ -143,7 +143,7 @@ function resolveStop(_, state, args, actionParams, {
143
143
  };
144
144
  delete children[resolvedActorRef.id];
145
145
  }
146
- return [cloneState(state, {
146
+ return [cloneMachineSnapshot(state, {
147
147
  children
148
148
  }), resolvedActorRef];
149
149
  }
@@ -159,7 +159,7 @@ function executeStop(actorScope, actorRef) {
159
159
 
160
160
  // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
161
161
  // this can happen, for example, when the invoking state is being exited immediately by an always transition
162
- if (actorRef.status !== interpreter.ActorStatus.Running) {
162
+ if (actorRef._processingStatus !== interpreter.ProcessingStatus.Running) {
163
163
  actorScope.stopChild(actorRef);
164
164
  return;
165
165
  }
@@ -654,10 +654,9 @@ function getStateNodeByPath(stateNode, statePath) {
654
654
  /**
655
655
  * Returns the state nodes represented by the current state value.
656
656
  *
657
- * @param state The state value or State instance
657
+ * @param stateValue The state value or State instance
658
658
  */
659
- function getStateNodes(stateNode, state) {
660
- const stateValue = state instanceof State ? state.value : interpreter.toStateValue(state);
659
+ function getStateNodes(stateNode, stateValue) {
661
660
  if (typeof stateValue === 'string') {
662
661
  return [stateNode, stateNode.states[stateValue]];
663
662
  }
@@ -878,7 +877,7 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
878
877
  if (historyValue === currentState.historyValue && areConfigurationsEqual(currentState.configuration, mutConfiguration)) {
879
878
  return nextState;
880
879
  }
881
- return cloneState(nextState, {
880
+ return cloneMachineSnapshot(nextState, {
882
881
  configuration: nextConfiguration,
883
882
  historyValue
884
883
  });
@@ -942,9 +941,9 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutCo
942
941
  if (ancestorMarker) {
943
942
  continue;
944
943
  }
945
- nextState = cloneState(nextState, {
944
+ nextState = cloneMachineSnapshot(nextState, {
946
945
  status: 'done',
947
- output: getMachineOutput(nextState, event, actorScope, currentState.configuration[0].machine.root, rootCompletionNode)
946
+ output: getMachineOutput(nextState, event, actorScope, nextState.machine.root, rootCompletionNode)
948
947
  });
949
948
  }
950
949
  }
@@ -1098,7 +1097,7 @@ function resolveActionsAndContextWorker(currentState, event, actorScope, actions
1098
1097
  event
1099
1098
  }) : action.params : undefined;
1100
1099
  if (!('resolve' in resolvedAction)) {
1101
- if (actorScope?.self.status === interpreter.ActorStatus.Running) {
1100
+ if (actorScope?.self._processingStatus === interpreter.ProcessingStatus.Running) {
1102
1101
  resolvedAction(actionArgs, actionParams);
1103
1102
  } else {
1104
1103
  actorScope?.defer(() => {
@@ -1116,7 +1115,7 @@ function resolveActionsAndContextWorker(currentState, event, actorScope, actions
1116
1115
  retries?.push([builtinAction, params]);
1117
1116
  }
1118
1117
  if ('execute' in builtinAction) {
1119
- if (actorScope?.self.status === interpreter.ActorStatus.Running) {
1118
+ if (actorScope?.self._processingStatus === interpreter.ProcessingStatus.Running) {
1120
1119
  builtinAction.execute(actorScope, params);
1121
1120
  } else {
1122
1121
  actorScope?.defer(builtinAction.execute.bind(null, actorScope, params));
@@ -1145,7 +1144,7 @@ function macrostep(state, event, actorScope, internalQueue = []) {
1145
1144
 
1146
1145
  // Handle stop event
1147
1146
  if (event.type === interpreter.XSTATE_STOP) {
1148
- nextState = cloneState(stopChildren(nextState, event, actorScope), {
1147
+ nextState = cloneMachineSnapshot(stopChildren(nextState, event, actorScope), {
1149
1148
  status: 'stopped'
1150
1149
  });
1151
1150
  states.push(nextState);
@@ -1223,188 +1222,90 @@ function resolveStateValue(rootNode, stateValue) {
1223
1222
  const configuration = getConfiguration(getStateNodes(rootNode, stateValue));
1224
1223
  return getStateValue(rootNode, [...configuration]);
1225
1224
  }
1226
- function getInitialConfiguration(rootNode) {
1227
- const configuration = [];
1228
- const initialTransition = rootNode.initial;
1229
- const statesToEnter = new Set();
1230
- const statesForDefaultEntry = new Set([rootNode]);
1231
- computeEntrySet([initialTransition], {}, statesForDefaultEntry, statesToEnter);
1232
- for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
1233
- configuration.push(stateNodeToEnter);
1234
- }
1235
- return configuration;
1236
- }
1237
-
1238
- class State {
1239
- /**
1240
- * Indicates whether the state is a final state.
1241
- */
1242
-
1243
- /**
1244
- * The output data of the top-level finite state.
1245
- */
1246
-
1247
- /**
1248
- * The enabled state nodes representative of the state value.
1249
- */
1250
-
1251
- /**
1252
- * An object mapping actor names to spawned/invoked actors.
1253
- */
1254
-
1255
- /**
1256
- * Creates a new State instance for the given `stateValue` and `context`.
1257
- * @param stateValue
1258
- * @param context
1259
- */
1260
- static from(stateValue, context = {}, machine) {
1261
- if (stateValue instanceof State) {
1262
- if (stateValue.context !== context) {
1263
- return new State({
1264
- value: stateValue.value,
1265
- context,
1266
- meta: {},
1267
- configuration: [],
1268
- // TODO: fix,
1269
- children: {},
1270
- status: 'active'
1271
- }, machine);
1272
- }
1273
- return stateValue;
1274
- }
1275
- const configuration = getConfiguration(getStateNodes(machine.root, stateValue));
1276
- return new State({
1277
- value: stateValue,
1278
- context,
1279
- meta: undefined,
1280
- configuration: Array.from(configuration),
1281
- children: {},
1282
- status: 'active'
1283
- }, machine);
1284
- }
1285
-
1286
- /**
1287
- * Creates a new `State` instance that represents the current state of a running machine.
1288
- *
1289
- * @param config
1290
- */
1291
- constructor(config, machine) {
1292
- this.machine = machine;
1293
- this.tags = void 0;
1294
- this.value = void 0;
1295
- this.status = void 0;
1296
- this.error = void 0;
1297
- this.context = void 0;
1298
- this.historyValue = {};
1299
- this.configuration = void 0;
1300
- this.children = void 0;
1301
- this.context = config.context;
1302
- this.historyValue = config.historyValue || {};
1303
- this.matches = this.matches.bind(this);
1304
- this.toStrings = this.toStrings.bind(this);
1305
- this.configuration = config.configuration ?? Array.from(getConfiguration(getStateNodes(machine.root, config.value)));
1306
- this.children = config.children;
1307
- this.value = getStateValue(machine.root, this.configuration);
1308
- this.tags = new Set(interpreter.flatten(this.configuration.map(sn => sn.tags)));
1309
- this.status = config.status;
1310
- this.output = config.output;
1311
- this.error = config.error;
1312
- }
1313
1225
 
1314
- /**
1315
- * Returns an array of all the string leaf state node paths.
1316
- * @param stateValue
1317
- * @param delimiter The character(s) that separate each subpath in the string state node path.
1318
- */
1319
- toStrings(stateValue = this.value) {
1320
- if (typeof stateValue === 'string') {
1321
- return [stateValue];
1226
+ function createMachineSnapshot(config, machine) {
1227
+ return {
1228
+ status: config.status,
1229
+ output: config.output,
1230
+ error: config.error,
1231
+ machine,
1232
+ context: config.context,
1233
+ configuration: config.configuration,
1234
+ value: getStateValue(machine.root, config.configuration),
1235
+ tags: new Set(interpreter.flatten(config.configuration.map(sn => sn.tags))),
1236
+ children: config.children,
1237
+ historyValue: config.historyValue || {},
1238
+ matches(parentStateValue) {
1239
+ return interpreter.matchesState(parentStateValue, this.value);
1240
+ },
1241
+ hasTag(tag) {
1242
+ return this.tags.has(tag);
1243
+ },
1244
+ can(event) {
1245
+ const transitionData = this.machine.getTransitionData(this, event);
1246
+ return !!transitionData?.length &&
1247
+ // Check that at least one transition is not forbidden
1248
+ transitionData.some(t => t.target !== undefined || t.actions.length);
1249
+ },
1250
+ get nextEvents() {
1251
+ return memo(this, 'nextEvents', () => {
1252
+ return [...new Set(interpreter.flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1253
+ });
1254
+ },
1255
+ get meta() {
1256
+ return this.configuration.reduce((acc, stateNode) => {
1257
+ if (stateNode.meta !== undefined) {
1258
+ acc[stateNode.id] = stateNode.meta;
1259
+ }
1260
+ return acc;
1261
+ }, {});
1262
+ },
1263
+ toJSON() {
1264
+ const {
1265
+ configuration,
1266
+ tags,
1267
+ machine,
1268
+ nextEvents,
1269
+ toJSON,
1270
+ can,
1271
+ hasTag,
1272
+ matches,
1273
+ ...jsonValues
1274
+ } = this;
1275
+ return {
1276
+ ...jsonValues,
1277
+ tags: Array.from(tags)
1278
+ };
1322
1279
  }
1323
- const valueKeys = Object.keys(stateValue);
1324
- return valueKeys.concat(...valueKeys.map(key => this.toStrings(stateValue[key]).map(s => key + interpreter.STATE_DELIMITER + s)));
1325
- }
1326
- toJSON() {
1327
- const {
1328
- configuration,
1329
- tags,
1330
- machine,
1331
- ...jsonValues
1332
- } = this;
1333
- return {
1334
- ...jsonValues,
1335
- tags: Array.from(tags),
1336
- meta: this.meta
1337
- };
1338
- }
1339
-
1340
- /**
1341
- * Whether the current state value is a subset of the given parent state value.
1342
- * @param parentStateValue
1343
- */
1344
- matches(parentStateValue) {
1345
- return interpreter.matchesState(parentStateValue, this.value);
1346
- }
1347
-
1348
- /**
1349
- * Whether the current state configuration has a state node with the specified `tag`.
1350
- * @param tag
1351
- */
1352
- hasTag(tag) {
1353
- return this.tags.has(tag);
1354
- }
1355
-
1356
- /**
1357
- * Determines whether sending the `event` will cause a non-forbidden transition
1358
- * to be selected, even if the transitions have no actions nor
1359
- * change the state value.
1360
- *
1361
- * @param event The event to test
1362
- * @returns Whether the event will cause a transition
1363
- */
1364
- can(event) {
1365
- const transitionData = this.machine.getTransitionData(this, event);
1366
- return !!transitionData?.length &&
1367
- // Check that at least one transition is not forbidden
1368
- transitionData.some(t => t.target !== undefined || t.actions.length);
1369
- }
1370
-
1371
- /**
1372
- * The next events that will cause a transition from the current state.
1373
- */
1374
- get nextEvents() {
1375
- return memo(this, 'nextEvents', () => {
1376
- return [...new Set(interpreter.flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1377
- });
1378
- }
1379
- get meta() {
1380
- return this.configuration.reduce((acc, stateNode) => {
1381
- if (stateNode.meta !== undefined) {
1382
- acc[stateNode.id] = stateNode.meta;
1383
- }
1384
- return acc;
1385
- }, {});
1386
- }
1280
+ };
1387
1281
  }
1388
- function cloneState(state, config = {}) {
1389
- return new State({
1282
+ function cloneMachineSnapshot(state, config = {}) {
1283
+ return createMachineSnapshot(
1284
+ // TODO: it's wasteful that this spread triggers getters
1285
+ {
1390
1286
  ...state,
1391
1287
  ...config
1392
1288
  }, state.machine);
1393
1289
  }
1394
- function getPersistedState(state) {
1290
+ function getPersistedState(state, options) {
1395
1291
  const {
1396
1292
  configuration,
1397
1293
  tags,
1398
1294
  machine,
1399
1295
  children,
1400
1296
  context,
1297
+ can,
1298
+ hasTag,
1299
+ matches,
1300
+ toJSON,
1301
+ nextEvents,
1401
1302
  ...jsonValues
1402
1303
  } = state;
1403
1304
  const childrenJson = {};
1404
1305
  for (const id in children) {
1405
1306
  const child = children[id];
1406
1307
  childrenJson[id] = {
1407
- state: child.getPersistedState(),
1308
+ state: child.getPersistedState(options),
1408
1309
  src: child.src,
1409
1310
  systemId: child._systemId
1410
1311
  };
@@ -1495,10 +1396,10 @@ function raise(eventOrExpr, options) {
1495
1396
  return raise;
1496
1397
  }
1497
1398
 
1498
- exports.State = State;
1499
1399
  exports.and = and;
1500
1400
  exports.cancel = cancel;
1501
- exports.cloneState = cloneState;
1401
+ exports.cloneMachineSnapshot = cloneMachineSnapshot;
1402
+ exports.createMachineSnapshot = createMachineSnapshot;
1502
1403
  exports.evaluateGuard = evaluateGuard;
1503
1404
  exports.formatInitialTransition = formatInitialTransition;
1504
1405
  exports.formatTransition = formatTransition;
@@ -1506,7 +1407,6 @@ exports.formatTransitions = formatTransitions;
1506
1407
  exports.getCandidates = getCandidates;
1507
1408
  exports.getConfiguration = getConfiguration;
1508
1409
  exports.getDelayedTransitions = getDelayedTransitions;
1509
- exports.getInitialConfiguration = getInitialConfiguration;
1510
1410
  exports.getInitialStateNodes = getInitialStateNodes;
1511
1411
  exports.getPersistedState = getPersistedState;
1512
1412
  exports.getStateNodeByPath = getStateNodeByPath;