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-fb2829f1.cjs.js');
3
+ var interpreter = require('./interpreter-36d5556e.cjs.js');
4
4
 
5
5
  const cache = new WeakMap();
6
6
  function memo(object, key, fn) {
@@ -49,25 +49,20 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
49
49
  input,
50
50
  syncSnapshot
51
51
  }) {
52
- const referenced = typeof src === 'string' ? interpreter.resolveReferencedActor(state.machine, src) : {
53
- src,
54
- input: undefined
55
- };
52
+ const logic = typeof src === 'string' ? interpreter.resolveReferencedActor(state.machine, src) : src;
56
53
  const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
57
54
  let actorRef;
58
- if (referenced) {
59
- // TODO: inline `input: undefined` should win over the referenced one
60
- const configuredInput = input || referenced.input;
61
- actorRef = interpreter.createActor(referenced.src, {
55
+ if (logic) {
56
+ actorRef = interpreter.createActor(logic, {
62
57
  id: resolvedId,
63
58
  src,
64
59
  parent: actorScope?.self,
65
60
  systemId,
66
- input: typeof configuredInput === 'function' ? configuredInput({
61
+ input: typeof input === 'function' ? input({
67
62
  context: state.context,
68
63
  event: actionArgs.event,
69
64
  self: actorScope?.self
70
- }) : configuredInput
65
+ }) : input
71
66
  });
72
67
  if (syncSnapshot) {
73
68
  actorRef.subscribe({
@@ -83,7 +78,7 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
83
78
  });
84
79
  }
85
80
  }
86
- return [cloneState(state, {
81
+ return [cloneMachineSnapshot(state, {
87
82
  children: {
88
83
  ...state.children,
89
84
  [resolvedId]: actorRef
@@ -143,7 +138,7 @@ function resolveStop(_, state, args, actionParams, {
143
138
  };
144
139
  delete children[resolvedActorRef.id];
145
140
  }
146
- return [cloneState(state, {
141
+ return [cloneMachineSnapshot(state, {
147
142
  children
148
143
  }), resolvedActorRef];
149
144
  }
@@ -654,10 +649,9 @@ function getStateNodeByPath(stateNode, statePath) {
654
649
  /**
655
650
  * Returns the state nodes represented by the current state value.
656
651
  *
657
- * @param state The state value or State instance
652
+ * @param stateValue The state value or State instance
658
653
  */
659
- function getStateNodes(stateNode, state) {
660
- const stateValue = state instanceof State ? state.value : interpreter.toStateValue(state);
654
+ function getStateNodes(stateNode, stateValue) {
661
655
  if (typeof stateValue === 'string') {
662
656
  return [stateNode, stateNode.states[stateValue]];
663
657
  }
@@ -878,7 +872,7 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
878
872
  if (historyValue === currentState.historyValue && areConfigurationsEqual(currentState.configuration, mutConfiguration)) {
879
873
  return nextState;
880
874
  }
881
- return cloneState(nextState, {
875
+ return cloneMachineSnapshot(nextState, {
882
876
  configuration: nextConfiguration,
883
877
  historyValue
884
878
  });
@@ -942,9 +936,9 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutCo
942
936
  if (ancestorMarker) {
943
937
  continue;
944
938
  }
945
- nextState = cloneState(nextState, {
939
+ nextState = cloneMachineSnapshot(nextState, {
946
940
  status: 'done',
947
- output: getMachineOutput(nextState, event, actorScope, currentState.configuration[0].machine.root, rootCompletionNode)
941
+ output: getMachineOutput(nextState, event, actorScope, nextState.machine.root, rootCompletionNode)
948
942
  });
949
943
  }
950
944
  }
@@ -1145,7 +1139,7 @@ function macrostep(state, event, actorScope, internalQueue = []) {
1145
1139
 
1146
1140
  // Handle stop event
1147
1141
  if (event.type === interpreter.XSTATE_STOP) {
1148
- nextState = cloneState(stopChildren(nextState, event, actorScope), {
1142
+ nextState = cloneMachineSnapshot(stopChildren(nextState, event, actorScope), {
1149
1143
  status: 'stopped'
1150
1144
  });
1151
1145
  states.push(nextState);
@@ -1223,170 +1217,85 @@ function resolveStateValue(rootNode, stateValue) {
1223
1217
  const configuration = getConfiguration(getStateNodes(rootNode, stateValue));
1224
1218
  return getStateValue(rootNode, [...configuration]);
1225
1219
  }
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
1220
 
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;
1221
+ const machineSnapshotMatches = function matches(testValue) {
1222
+ return interpreter.matchesState(testValue, this.value);
1223
+ };
1224
+ const machineSnapshotHasTag = function hasTag(tag) {
1225
+ return this.tags.has(tag);
1226
+ };
1227
+ const machineSnapshotCan = function can(event) {
1228
+ const transitionData = this.machine.getTransitionData(this, event);
1229
+ return !!transitionData?.length &&
1230
+ // Check that at least one transition is not forbidden
1231
+ transitionData.some(t => t.target !== undefined || t.actions.length);
1232
+ };
1233
+ const machineSnapshotToJSON = function toJSON() {
1234
+ const {
1235
+ configuration,
1236
+ tags,
1237
+ machine,
1238
+ nextEvents,
1239
+ toJSON,
1240
+ can,
1241
+ hasTag,
1242
+ matches,
1243
+ ...jsonValues
1244
+ } = this;
1245
+ return {
1246
+ ...jsonValues,
1247
+ tags: Array.from(tags)
1248
+ };
1249
+ };
1250
+ const machineSnapshotNextEvents = function nextEvents() {
1251
+ return memo(this, 'nextEvents', () => {
1252
+ return [...new Set(interpreter.flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1253
+ });
1254
+ };
1255
+ const machineSnapshotMeta = function nextEvents() {
1256
+ return this.configuration.reduce((acc, stateNode) => {
1257
+ if (stateNode.meta !== undefined) {
1258
+ acc[stateNode.id] = stateNode.meta;
1274
1259
  }
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
-
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];
1260
+ return acc;
1261
+ }, {});
1262
+ };
1263
+ function createMachineSnapshot(config, machine) {
1264
+ const snapshot = {
1265
+ status: config.status,
1266
+ output: config.output,
1267
+ error: config.error,
1268
+ machine,
1269
+ context: config.context,
1270
+ configuration: config.configuration,
1271
+ value: getStateValue(machine.root, config.configuration),
1272
+ tags: new Set(interpreter.flatten(config.configuration.map(sn => sn.tags))),
1273
+ children: config.children,
1274
+ historyValue: config.historyValue || {},
1275
+ // this one is generic in the target and it's hard to create a matching non-generic source signature
1276
+ matches: machineSnapshotMatches,
1277
+ hasTag: machineSnapshotHasTag,
1278
+ can: machineSnapshotCan,
1279
+ toJSON: machineSnapshotToJSON
1280
+ };
1281
+ Object.defineProperties(snapshot, {
1282
+ nextEvents: {
1283
+ get: machineSnapshotNextEvents,
1284
+ configurable: true,
1285
+ enumerable: true
1286
+ },
1287
+ meta: {
1288
+ get: machineSnapshotMeta,
1289
+ configurable: true,
1290
+ enumerable: true
1322
1291
  }
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
- }
1292
+ });
1293
+ return snapshot;
1387
1294
  }
1388
- function cloneState(state, config = {}) {
1389
- return new State({
1295
+ function cloneMachineSnapshot(state, config = {}) {
1296
+ return createMachineSnapshot(
1297
+ // TODO: it's wasteful that this spread triggers getters
1298
+ {
1390
1299
  ...state,
1391
1300
  ...config
1392
1301
  }, state.machine);
@@ -1398,6 +1307,11 @@ function getPersistedState(state, options) {
1398
1307
  machine,
1399
1308
  children,
1400
1309
  context,
1310
+ can,
1311
+ hasTag,
1312
+ matches,
1313
+ toJSON,
1314
+ nextEvents,
1401
1315
  ...jsonValues
1402
1316
  } = state;
1403
1317
  const childrenJson = {};
@@ -1495,10 +1409,10 @@ function raise(eventOrExpr, options) {
1495
1409
  return raise;
1496
1410
  }
1497
1411
 
1498
- exports.State = State;
1499
1412
  exports.and = and;
1500
1413
  exports.cancel = cancel;
1501
- exports.cloneState = cloneState;
1414
+ exports.cloneMachineSnapshot = cloneMachineSnapshot;
1415
+ exports.createMachineSnapshot = createMachineSnapshot;
1502
1416
  exports.evaluateGuard = evaluateGuard;
1503
1417
  exports.formatInitialTransition = formatInitialTransition;
1504
1418
  exports.formatTransition = formatTransition;
@@ -1506,7 +1420,6 @@ exports.formatTransitions = formatTransitions;
1506
1420
  exports.getCandidates = getCandidates;
1507
1421
  exports.getConfiguration = getConfiguration;
1508
1422
  exports.getDelayedTransitions = getDelayedTransitions;
1509
- exports.getInitialConfiguration = getInitialConfiguration;
1510
1423
  exports.getInitialStateNodes = getInitialStateNodes;
1511
1424
  exports.getPersistedState = getPersistedState;
1512
1425
  exports.getStateNodeByPath = getStateNodeByPath;