xstate 5.0.0-beta.41 → 5.0.0-beta.43

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 +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 +45 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +45 -1
  9. package/actors/dist/xstate-actors.development.esm.js +45 -1
  10. package/actors/dist/xstate-actors.esm.js +45 -1
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/declarations/src/State.d.ts +36 -43
  13. package/dist/declarations/src/StateMachine.d.ts +12 -29
  14. package/dist/declarations/src/StateNode.d.ts +2 -2
  15. package/dist/declarations/src/actions/spawn.d.ts +3 -3
  16. package/dist/declarations/src/actors/promise.d.ts +53 -0
  17. package/dist/declarations/src/index.d.ts +3 -4
  18. package/dist/declarations/src/interpreter.d.ts +12 -0
  19. package/dist/declarations/src/stateUtils.d.ts +7 -8
  20. package/dist/declarations/src/types.d.ts +153 -21
  21. package/dist/declarations/src/utils.d.ts +2 -8
  22. package/dist/{interpreter-fb2829f1.cjs.js → interpreter-36d5556e.cjs.js} +25 -19
  23. package/dist/{interpreter-70ed981b.development.cjs.js → interpreter-4e8e2a0d.development.cjs.js} +25 -19
  24. package/dist/{interpreter-480db258.esm.js → interpreter-63c80754.esm.js} +26 -19
  25. package/dist/{interpreter-936da690.development.esm.js → interpreter-80eb3bec.development.esm.js} +26 -19
  26. package/dist/{raise-5ab465ed.development.cjs.js → raise-23dea0d7.development.cjs.js} +101 -188
  27. package/dist/{raise-9d6921da.esm.js → raise-8dc8e1aa.esm.js} +95 -181
  28. package/dist/{raise-beae3fd3.cjs.js → raise-e0fe5c2d.cjs.js} +96 -183
  29. package/dist/{raise-f757be00.development.esm.js → raise-f4ad5a87.development.esm.js} +100 -186
  30. package/dist/{send-fb87a01a.development.cjs.js → send-0174c155.development.cjs.js} +10 -10
  31. package/dist/{send-b26e3812.development.esm.js → send-5d129d95.development.esm.js} +10 -10
  32. package/dist/{send-a931d1b8.esm.js → send-84e2e742.esm.js} +10 -10
  33. package/dist/{send-ca5f706c.cjs.js → send-87bbaaab.cjs.js} +10 -10
  34. package/dist/xstate.cjs.js +24 -50
  35. package/dist/xstate.cjs.mjs +0 -2
  36. package/dist/xstate.development.cjs.js +24 -50
  37. package/dist/xstate.development.cjs.mjs +0 -2
  38. package/dist/xstate.development.esm.js +28 -52
  39. package/dist/xstate.esm.js +28 -52
  40. package/dist/xstate.umd.min.js +1 -1
  41. package/dist/xstate.umd.min.js.map +1 -1
  42. package/guards/dist/xstate-guards.cjs.js +2 -2
  43. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  44. package/guards/dist/xstate-guards.development.esm.js +2 -2
  45. package/guards/dist/xstate-guards.esm.js +2 -2
  46. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  47. package/package.json +1 -1
  48. package/dist/declarations/src/mapState.d.ts +0 -3
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var interpreter = require('./interpreter-70ed981b.development.cjs.js');
3
+ var interpreter = require('./interpreter-4e8e2a0d.development.cjs.js');
4
4
 
5
5
  const cache = new WeakMap();
6
6
  function memo(object, key, fn) {
@@ -52,25 +52,20 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
52
52
  input,
53
53
  syncSnapshot
54
54
  }) {
55
- const referenced = typeof src === 'string' ? interpreter.resolveReferencedActor(state.machine, src) : {
56
- src,
57
- input: undefined
58
- };
55
+ const logic = typeof src === 'string' ? interpreter.resolveReferencedActor(state.machine, src) : src;
59
56
  const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
60
57
  let actorRef;
61
- if (referenced) {
62
- // TODO: inline `input: undefined` should win over the referenced one
63
- const configuredInput = input || referenced.input;
64
- actorRef = interpreter.createActor(referenced.src, {
58
+ if (logic) {
59
+ actorRef = interpreter.createActor(logic, {
65
60
  id: resolvedId,
66
61
  src,
67
62
  parent: actorScope?.self,
68
63
  systemId,
69
- input: typeof configuredInput === 'function' ? configuredInput({
64
+ input: typeof input === 'function' ? input({
70
65
  context: state.context,
71
66
  event: actionArgs.event,
72
67
  self: actorScope?.self
73
- }) : configuredInput
68
+ }) : input
74
69
  });
75
70
  if (syncSnapshot) {
76
71
  actorRef.subscribe({
@@ -89,7 +84,7 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
89
84
  if (!actorRef) {
90
85
  console.warn(`Actor type '${src}' not found in machine '${actorScope.id}'.`);
91
86
  }
92
- return [cloneState(state, {
87
+ return [cloneMachineSnapshot(state, {
93
88
  children: {
94
89
  ...state.children,
95
90
  [resolvedId]: actorRef
@@ -152,7 +147,7 @@ function resolveStop(_, state, args, actionParams, {
152
147
  };
153
148
  delete children[resolvedActorRef.id];
154
149
  }
155
- return [cloneState(state, {
150
+ return [cloneMachineSnapshot(state, {
156
151
  children
157
152
  }), resolvedActorRef];
158
153
  }
@@ -685,10 +680,9 @@ function getStateNodeByPath(stateNode, statePath) {
685
680
  /**
686
681
  * Returns the state nodes represented by the current state value.
687
682
  *
688
- * @param state The state value or State instance
683
+ * @param stateValue The state value or State instance
689
684
  */
690
- function getStateNodes(stateNode, state) {
691
- const stateValue = state instanceof State ? state.value : interpreter.toStateValue(state);
685
+ function getStateNodes(stateNode, stateValue) {
692
686
  if (typeof stateValue === 'string') {
693
687
  return [stateNode, stateNode.states[stateValue]];
694
688
  }
@@ -909,7 +903,7 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
909
903
  if (historyValue === currentState.historyValue && areConfigurationsEqual(currentState.configuration, mutConfiguration)) {
910
904
  return nextState;
911
905
  }
912
- return cloneState(nextState, {
906
+ return cloneMachineSnapshot(nextState, {
913
907
  configuration: nextConfiguration,
914
908
  historyValue
915
909
  });
@@ -973,9 +967,9 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutCo
973
967
  if (ancestorMarker) {
974
968
  continue;
975
969
  }
976
- nextState = cloneState(nextState, {
970
+ nextState = cloneMachineSnapshot(nextState, {
977
971
  status: 'done',
978
- output: getMachineOutput(nextState, event, actorScope, currentState.configuration[0].machine.root, rootCompletionNode)
972
+ output: getMachineOutput(nextState, event, actorScope, nextState.machine.root, rootCompletionNode)
979
973
  });
980
974
  }
981
975
  }
@@ -1179,7 +1173,7 @@ function macrostep(state, event, actorScope, internalQueue = []) {
1179
1173
 
1180
1174
  // Handle stop event
1181
1175
  if (event.type === interpreter.XSTATE_STOP) {
1182
- nextState = cloneState(stopChildren(nextState, event, actorScope), {
1176
+ nextState = cloneMachineSnapshot(stopChildren(nextState, event, actorScope), {
1183
1177
  status: 'stopped'
1184
1178
  });
1185
1179
  states.push(nextState);
@@ -1257,173 +1251,88 @@ function resolveStateValue(rootNode, stateValue) {
1257
1251
  const configuration = getConfiguration(getStateNodes(rootNode, stateValue));
1258
1252
  return getStateValue(rootNode, [...configuration]);
1259
1253
  }
1260
- function getInitialConfiguration(rootNode) {
1261
- const configuration = [];
1262
- const initialTransition = rootNode.initial;
1263
- const statesToEnter = new Set();
1264
- const statesForDefaultEntry = new Set([rootNode]);
1265
- computeEntrySet([initialTransition], {}, statesForDefaultEntry, statesToEnter);
1266
- for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
1267
- configuration.push(stateNodeToEnter);
1268
- }
1269
- return configuration;
1270
- }
1271
-
1272
- class State {
1273
- /**
1274
- * Indicates whether the state is a final state.
1275
- */
1276
-
1277
- /**
1278
- * The output data of the top-level finite state.
1279
- */
1280
-
1281
- /**
1282
- * The enabled state nodes representative of the state value.
1283
- */
1284
-
1285
- /**
1286
- * An object mapping actor names to spawned/invoked actors.
1287
- */
1288
-
1289
- /**
1290
- * Creates a new State instance for the given `stateValue` and `context`.
1291
- * @param stateValue
1292
- * @param context
1293
- */
1294
- static from(stateValue, context = {}, machine) {
1295
- if (stateValue instanceof State) {
1296
- if (stateValue.context !== context) {
1297
- return new State({
1298
- value: stateValue.value,
1299
- context,
1300
- meta: {},
1301
- configuration: [],
1302
- // TODO: fix,
1303
- children: {},
1304
- status: 'active'
1305
- }, machine);
1306
- }
1307
- return stateValue;
1308
- }
1309
- const configuration = getConfiguration(getStateNodes(machine.root, stateValue));
1310
- return new State({
1311
- value: stateValue,
1312
- context,
1313
- meta: undefined,
1314
- configuration: Array.from(configuration),
1315
- children: {},
1316
- status: 'active'
1317
- }, machine);
1318
- }
1319
-
1320
- /**
1321
- * Creates a new `State` instance that represents the current state of a running machine.
1322
- *
1323
- * @param config
1324
- */
1325
- constructor(config, machine) {
1326
- this.machine = machine;
1327
- this.tags = void 0;
1328
- this.value = void 0;
1329
- this.status = void 0;
1330
- this.error = void 0;
1331
- this.context = void 0;
1332
- this.historyValue = {};
1333
- this.configuration = void 0;
1334
- this.children = void 0;
1335
- this.context = config.context;
1336
- this.historyValue = config.historyValue || {};
1337
- this.matches = this.matches.bind(this);
1338
- this.toStrings = this.toStrings.bind(this);
1339
- this.configuration = config.configuration ?? Array.from(getConfiguration(getStateNodes(machine.root, config.value)));
1340
- this.children = config.children;
1341
- this.value = getStateValue(machine.root, this.configuration);
1342
- this.tags = new Set(interpreter.flatten(this.configuration.map(sn => sn.tags)));
1343
- this.status = config.status;
1344
- this.output = config.output;
1345
- this.error = config.error;
1346
- }
1347
-
1348
- /**
1349
- * Returns an array of all the string leaf state node paths.
1350
- * @param stateValue
1351
- * @param delimiter The character(s) that separate each subpath in the string state node path.
1352
- */
1353
- toStrings(stateValue = this.value) {
1354
- if (typeof stateValue === 'string') {
1355
- return [stateValue];
1356
- }
1357
- const valueKeys = Object.keys(stateValue);
1358
- return valueKeys.concat(...valueKeys.map(key => this.toStrings(stateValue[key]).map(s => key + interpreter.STATE_DELIMITER + s)));
1359
- }
1360
- toJSON() {
1361
- const {
1362
- configuration,
1363
- tags,
1364
- machine,
1365
- ...jsonValues
1366
- } = this;
1367
- return {
1368
- ...jsonValues,
1369
- tags: Array.from(tags),
1370
- meta: this.meta
1371
- };
1372
- }
1373
-
1374
- /**
1375
- * Whether the current state value is a subset of the given parent state value.
1376
- * @param parentStateValue
1377
- */
1378
- matches(parentStateValue) {
1379
- return interpreter.matchesState(parentStateValue, this.value);
1380
- }
1381
1254
 
1382
- /**
1383
- * Whether the current state configuration has a state node with the specified `tag`.
1384
- * @param tag
1385
- */
1386
- hasTag(tag) {
1387
- return this.tags.has(tag);
1388
- }
1389
-
1390
- /**
1391
- * Determines whether sending the `event` will cause a non-forbidden transition
1392
- * to be selected, even if the transitions have no actions nor
1393
- * change the state value.
1394
- *
1395
- * @param event The event to test
1396
- * @returns Whether the event will cause a transition
1397
- */
1398
- can(event) {
1399
- if (!this.machine) {
1400
- console.warn(`state.can(...) used outside of a machine-created State object; this will always return false.`);
1401
- }
1402
- const transitionData = this.machine.getTransitionData(this, event);
1403
- return !!transitionData?.length &&
1404
- // Check that at least one transition is not forbidden
1405
- transitionData.some(t => t.target !== undefined || t.actions.length);
1406
- }
1407
-
1408
- /**
1409
- * The next events that will cause a transition from the current state.
1410
- */
1411
- get nextEvents() {
1412
- return memo(this, 'nextEvents', () => {
1413
- return [...new Set(interpreter.flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1414
- });
1415
- }
1416
- get meta() {
1417
- return this.configuration.reduce((acc, stateNode) => {
1418
- if (stateNode.meta !== undefined) {
1419
- acc[stateNode.id] = stateNode.meta;
1420
- }
1421
- return acc;
1422
- }, {});
1423
- }
1255
+ const machineSnapshotMatches = function matches(testValue) {
1256
+ return interpreter.matchesState(testValue, this.value);
1257
+ };
1258
+ const machineSnapshotHasTag = function hasTag(tag) {
1259
+ return this.tags.has(tag);
1260
+ };
1261
+ const machineSnapshotCan = function can(event) {
1262
+ if (!this.machine) {
1263
+ console.warn(`state.can(...) used outside of a machine-created State object; this will always return false.`);
1264
+ }
1265
+ const transitionData = this.machine.getTransitionData(this, event);
1266
+ return !!transitionData?.length &&
1267
+ // Check that at least one transition is not forbidden
1268
+ transitionData.some(t => t.target !== undefined || t.actions.length);
1269
+ };
1270
+ const machineSnapshotToJSON = function toJSON() {
1271
+ const {
1272
+ configuration,
1273
+ tags,
1274
+ machine,
1275
+ nextEvents,
1276
+ toJSON,
1277
+ can,
1278
+ hasTag,
1279
+ matches,
1280
+ ...jsonValues
1281
+ } = this;
1282
+ return {
1283
+ ...jsonValues,
1284
+ tags: Array.from(tags)
1285
+ };
1286
+ };
1287
+ const machineSnapshotNextEvents = function nextEvents() {
1288
+ return memo(this, 'nextEvents', () => {
1289
+ return [...new Set(interpreter.flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1290
+ });
1291
+ };
1292
+ const machineSnapshotMeta = function nextEvents() {
1293
+ return this.configuration.reduce((acc, stateNode) => {
1294
+ if (stateNode.meta !== undefined) {
1295
+ acc[stateNode.id] = stateNode.meta;
1296
+ }
1297
+ return acc;
1298
+ }, {});
1299
+ };
1300
+ function createMachineSnapshot(config, machine) {
1301
+ const snapshot = {
1302
+ status: config.status,
1303
+ output: config.output,
1304
+ error: config.error,
1305
+ machine,
1306
+ context: config.context,
1307
+ configuration: config.configuration,
1308
+ value: getStateValue(machine.root, config.configuration),
1309
+ tags: new Set(interpreter.flatten(config.configuration.map(sn => sn.tags))),
1310
+ children: config.children,
1311
+ historyValue: config.historyValue || {},
1312
+ // this one is generic in the target and it's hard to create a matching non-generic source signature
1313
+ matches: machineSnapshotMatches,
1314
+ hasTag: machineSnapshotHasTag,
1315
+ can: machineSnapshotCan,
1316
+ toJSON: machineSnapshotToJSON
1317
+ };
1318
+ Object.defineProperties(snapshot, {
1319
+ nextEvents: {
1320
+ get: machineSnapshotNextEvents,
1321
+ configurable: true,
1322
+ enumerable: true
1323
+ },
1324
+ meta: {
1325
+ get: machineSnapshotMeta,
1326
+ configurable: true,
1327
+ enumerable: true
1328
+ }
1329
+ });
1330
+ return snapshot;
1424
1331
  }
1425
- function cloneState(state, config = {}) {
1426
- return new State({
1332
+ function cloneMachineSnapshot(state, config = {}) {
1333
+ return createMachineSnapshot(
1334
+ // TODO: it's wasteful that this spread triggers getters
1335
+ {
1427
1336
  ...state,
1428
1337
  ...config
1429
1338
  }, state.machine);
@@ -1435,6 +1344,11 @@ function getPersistedState(state, options) {
1435
1344
  machine,
1436
1345
  children,
1437
1346
  context,
1347
+ can,
1348
+ hasTag,
1349
+ matches,
1350
+ toJSON,
1351
+ nextEvents,
1438
1352
  ...jsonValues
1439
1353
  } = state;
1440
1354
  const childrenJson = {};
@@ -1538,10 +1452,10 @@ function raise(eventOrExpr, options) {
1538
1452
  return raise;
1539
1453
  }
1540
1454
 
1541
- exports.State = State;
1542
1455
  exports.and = and;
1543
1456
  exports.cancel = cancel;
1544
- exports.cloneState = cloneState;
1457
+ exports.cloneMachineSnapshot = cloneMachineSnapshot;
1458
+ exports.createMachineSnapshot = createMachineSnapshot;
1545
1459
  exports.evaluateGuard = evaluateGuard;
1546
1460
  exports.formatInitialTransition = formatInitialTransition;
1547
1461
  exports.formatTransition = formatTransition;
@@ -1549,7 +1463,6 @@ exports.formatTransitions = formatTransitions;
1549
1463
  exports.getCandidates = getCandidates;
1550
1464
  exports.getConfiguration = getConfiguration;
1551
1465
  exports.getDelayedTransitions = getDelayedTransitions;
1552
- exports.getInitialConfiguration = getInitialConfiguration;
1553
1466
  exports.getInitialStateNodes = getInitialStateNodes;
1554
1467
  exports.getPersistedState = getPersistedState;
1555
1468
  exports.getStateNodeByPath = getStateNodeByPath;