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,4 +1,4 @@
1
- import { r as resolveReferencedActor, d as createActor, P as ProcessingStatus, h as createErrorActorEvent, j as toStateValue, k as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, l as toStatePath, o as createDoneStateEvent, q as resolveOutput, X as XSTATE_STOP, s as XSTATE_INIT, W as WILDCARD, u as createAfterEvent, v as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-480db258.esm.js';
1
+ import { r as resolveReferencedActor, d as createActor, P as ProcessingStatus, h as createErrorActorEvent, j as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, k as toStatePath, l as createDoneStateEvent, o as resolveOutput, X as XSTATE_STOP, q as XSTATE_INIT, W as WILDCARD, s as createAfterEvent, u as flatten, $ as $$ACTOR_TYPE, f as matchesState } from './interpreter-63c80754.esm.js';
2
2
 
3
3
  const cache = new WeakMap();
4
4
  function memo(object, key, fn) {
@@ -47,25 +47,20 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
47
47
  input,
48
48
  syncSnapshot
49
49
  }) {
50
- const referenced = typeof src === 'string' ? resolveReferencedActor(state.machine, src) : {
51
- src,
52
- input: undefined
53
- };
50
+ const logic = typeof src === 'string' ? resolveReferencedActor(state.machine, src) : src;
54
51
  const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
55
52
  let actorRef;
56
- if (referenced) {
57
- // TODO: inline `input: undefined` should win over the referenced one
58
- const configuredInput = input || referenced.input;
59
- actorRef = createActor(referenced.src, {
53
+ if (logic) {
54
+ actorRef = createActor(logic, {
60
55
  id: resolvedId,
61
56
  src,
62
57
  parent: actorScope?.self,
63
58
  systemId,
64
- input: typeof configuredInput === 'function' ? configuredInput({
59
+ input: typeof input === 'function' ? input({
65
60
  context: state.context,
66
61
  event: actionArgs.event,
67
62
  self: actorScope?.self
68
- }) : configuredInput
63
+ }) : input
69
64
  });
70
65
  if (syncSnapshot) {
71
66
  actorRef.subscribe({
@@ -81,7 +76,7 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
81
76
  });
82
77
  }
83
78
  }
84
- return [cloneState(state, {
79
+ return [cloneMachineSnapshot(state, {
85
80
  children: {
86
81
  ...state.children,
87
82
  [resolvedId]: actorRef
@@ -141,7 +136,7 @@ function resolveStop(_, state, args, actionParams, {
141
136
  };
142
137
  delete children[resolvedActorRef.id];
143
138
  }
144
- return [cloneState(state, {
139
+ return [cloneMachineSnapshot(state, {
145
140
  children
146
141
  }), resolvedActorRef];
147
142
  }
@@ -652,10 +647,9 @@ function getStateNodeByPath(stateNode, statePath) {
652
647
  /**
653
648
  * Returns the state nodes represented by the current state value.
654
649
  *
655
- * @param state The state value or State instance
650
+ * @param stateValue The state value or State instance
656
651
  */
657
- function getStateNodes(stateNode, state) {
658
- const stateValue = state instanceof State ? state.value : toStateValue(state);
652
+ function getStateNodes(stateNode, stateValue) {
659
653
  if (typeof stateValue === 'string') {
660
654
  return [stateNode, stateNode.states[stateValue]];
661
655
  }
@@ -876,7 +870,7 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
876
870
  if (historyValue === currentState.historyValue && areConfigurationsEqual(currentState.configuration, mutConfiguration)) {
877
871
  return nextState;
878
872
  }
879
- return cloneState(nextState, {
873
+ return cloneMachineSnapshot(nextState, {
880
874
  configuration: nextConfiguration,
881
875
  historyValue
882
876
  });
@@ -940,9 +934,9 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutCo
940
934
  if (ancestorMarker) {
941
935
  continue;
942
936
  }
943
- nextState = cloneState(nextState, {
937
+ nextState = cloneMachineSnapshot(nextState, {
944
938
  status: 'done',
945
- output: getMachineOutput(nextState, event, actorScope, currentState.configuration[0].machine.root, rootCompletionNode)
939
+ output: getMachineOutput(nextState, event, actorScope, nextState.machine.root, rootCompletionNode)
946
940
  });
947
941
  }
948
942
  }
@@ -1143,7 +1137,7 @@ function macrostep(state, event, actorScope, internalQueue = []) {
1143
1137
 
1144
1138
  // Handle stop event
1145
1139
  if (event.type === XSTATE_STOP) {
1146
- nextState = cloneState(stopChildren(nextState, event, actorScope), {
1140
+ nextState = cloneMachineSnapshot(stopChildren(nextState, event, actorScope), {
1147
1141
  status: 'stopped'
1148
1142
  });
1149
1143
  states.push(nextState);
@@ -1221,170 +1215,85 @@ function resolveStateValue(rootNode, stateValue) {
1221
1215
  const configuration = getConfiguration(getStateNodes(rootNode, stateValue));
1222
1216
  return getStateValue(rootNode, [...configuration]);
1223
1217
  }
1224
- function getInitialConfiguration(rootNode) {
1225
- const configuration = [];
1226
- const initialTransition = rootNode.initial;
1227
- const statesToEnter = new Set();
1228
- const statesForDefaultEntry = new Set([rootNode]);
1229
- computeEntrySet([initialTransition], {}, statesForDefaultEntry, statesToEnter);
1230
- for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
1231
- configuration.push(stateNodeToEnter);
1232
- }
1233
- return configuration;
1234
- }
1235
-
1236
- class State {
1237
- /**
1238
- * Indicates whether the state is a final state.
1239
- */
1240
1218
 
1241
- /**
1242
- * The output data of the top-level finite state.
1243
- */
1244
-
1245
- /**
1246
- * The enabled state nodes representative of the state value.
1247
- */
1248
-
1249
- /**
1250
- * An object mapping actor names to spawned/invoked actors.
1251
- */
1252
-
1253
- /**
1254
- * Creates a new State instance for the given `stateValue` and `context`.
1255
- * @param stateValue
1256
- * @param context
1257
- */
1258
- static from(stateValue, context = {}, machine) {
1259
- if (stateValue instanceof State) {
1260
- if (stateValue.context !== context) {
1261
- return new State({
1262
- value: stateValue.value,
1263
- context,
1264
- meta: {},
1265
- configuration: [],
1266
- // TODO: fix,
1267
- children: {},
1268
- status: 'active'
1269
- }, machine);
1270
- }
1271
- return stateValue;
1219
+ const machineSnapshotMatches = function matches(testValue) {
1220
+ return matchesState(testValue, this.value);
1221
+ };
1222
+ const machineSnapshotHasTag = function hasTag(tag) {
1223
+ return this.tags.has(tag);
1224
+ };
1225
+ const machineSnapshotCan = function can(event) {
1226
+ const transitionData = this.machine.getTransitionData(this, event);
1227
+ return !!transitionData?.length &&
1228
+ // Check that at least one transition is not forbidden
1229
+ transitionData.some(t => t.target !== undefined || t.actions.length);
1230
+ };
1231
+ const machineSnapshotToJSON = function toJSON() {
1232
+ const {
1233
+ configuration,
1234
+ tags,
1235
+ machine,
1236
+ nextEvents,
1237
+ toJSON,
1238
+ can,
1239
+ hasTag,
1240
+ matches,
1241
+ ...jsonValues
1242
+ } = this;
1243
+ return {
1244
+ ...jsonValues,
1245
+ tags: Array.from(tags)
1246
+ };
1247
+ };
1248
+ const machineSnapshotNextEvents = function nextEvents() {
1249
+ return memo(this, 'nextEvents', () => {
1250
+ return [...new Set(flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1251
+ });
1252
+ };
1253
+ const machineSnapshotMeta = function nextEvents() {
1254
+ return this.configuration.reduce((acc, stateNode) => {
1255
+ if (stateNode.meta !== undefined) {
1256
+ acc[stateNode.id] = stateNode.meta;
1272
1257
  }
1273
- const configuration = getConfiguration(getStateNodes(machine.root, stateValue));
1274
- return new State({
1275
- value: stateValue,
1276
- context,
1277
- meta: undefined,
1278
- configuration: Array.from(configuration),
1279
- children: {},
1280
- status: 'active'
1281
- }, machine);
1282
- }
1283
-
1284
- /**
1285
- * Creates a new `State` instance that represents the current state of a running machine.
1286
- *
1287
- * @param config
1288
- */
1289
- constructor(config, machine) {
1290
- this.machine = machine;
1291
- this.tags = void 0;
1292
- this.value = void 0;
1293
- this.status = void 0;
1294
- this.error = void 0;
1295
- this.context = void 0;
1296
- this.historyValue = {};
1297
- this.configuration = void 0;
1298
- this.children = void 0;
1299
- this.context = config.context;
1300
- this.historyValue = config.historyValue || {};
1301
- this.matches = this.matches.bind(this);
1302
- this.toStrings = this.toStrings.bind(this);
1303
- this.configuration = config.configuration ?? Array.from(getConfiguration(getStateNodes(machine.root, config.value)));
1304
- this.children = config.children;
1305
- this.value = getStateValue(machine.root, this.configuration);
1306
- this.tags = new Set(flatten(this.configuration.map(sn => sn.tags)));
1307
- this.status = config.status;
1308
- this.output = config.output;
1309
- this.error = config.error;
1310
- }
1311
-
1312
- /**
1313
- * Returns an array of all the string leaf state node paths.
1314
- * @param stateValue
1315
- * @param delimiter The character(s) that separate each subpath in the string state node path.
1316
- */
1317
- toStrings(stateValue = this.value) {
1318
- if (typeof stateValue === 'string') {
1319
- return [stateValue];
1258
+ return acc;
1259
+ }, {});
1260
+ };
1261
+ function createMachineSnapshot(config, machine) {
1262
+ const snapshot = {
1263
+ status: config.status,
1264
+ output: config.output,
1265
+ error: config.error,
1266
+ machine,
1267
+ context: config.context,
1268
+ configuration: config.configuration,
1269
+ value: getStateValue(machine.root, config.configuration),
1270
+ tags: new Set(flatten(config.configuration.map(sn => sn.tags))),
1271
+ children: config.children,
1272
+ historyValue: config.historyValue || {},
1273
+ // this one is generic in the target and it's hard to create a matching non-generic source signature
1274
+ matches: machineSnapshotMatches,
1275
+ hasTag: machineSnapshotHasTag,
1276
+ can: machineSnapshotCan,
1277
+ toJSON: machineSnapshotToJSON
1278
+ };
1279
+ Object.defineProperties(snapshot, {
1280
+ nextEvents: {
1281
+ get: machineSnapshotNextEvents,
1282
+ configurable: true,
1283
+ enumerable: true
1284
+ },
1285
+ meta: {
1286
+ get: machineSnapshotMeta,
1287
+ configurable: true,
1288
+ enumerable: true
1320
1289
  }
1321
- const valueKeys = Object.keys(stateValue);
1322
- return valueKeys.concat(...valueKeys.map(key => this.toStrings(stateValue[key]).map(s => key + STATE_DELIMITER + s)));
1323
- }
1324
- toJSON() {
1325
- const {
1326
- configuration,
1327
- tags,
1328
- machine,
1329
- ...jsonValues
1330
- } = this;
1331
- return {
1332
- ...jsonValues,
1333
- tags: Array.from(tags),
1334
- meta: this.meta
1335
- };
1336
- }
1337
-
1338
- /**
1339
- * Whether the current state value is a subset of the given parent state value.
1340
- * @param parentStateValue
1341
- */
1342
- matches(parentStateValue) {
1343
- return matchesState(parentStateValue, this.value);
1344
- }
1345
-
1346
- /**
1347
- * Whether the current state configuration has a state node with the specified `tag`.
1348
- * @param tag
1349
- */
1350
- hasTag(tag) {
1351
- return this.tags.has(tag);
1352
- }
1353
-
1354
- /**
1355
- * Determines whether sending the `event` will cause a non-forbidden transition
1356
- * to be selected, even if the transitions have no actions nor
1357
- * change the state value.
1358
- *
1359
- * @param event The event to test
1360
- * @returns Whether the event will cause a transition
1361
- */
1362
- can(event) {
1363
- const transitionData = this.machine.getTransitionData(this, event);
1364
- return !!transitionData?.length &&
1365
- // Check that at least one transition is not forbidden
1366
- transitionData.some(t => t.target !== undefined || t.actions.length);
1367
- }
1368
-
1369
- /**
1370
- * The next events that will cause a transition from the current state.
1371
- */
1372
- get nextEvents() {
1373
- return memo(this, 'nextEvents', () => {
1374
- return [...new Set(flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1375
- });
1376
- }
1377
- get meta() {
1378
- return this.configuration.reduce((acc, stateNode) => {
1379
- if (stateNode.meta !== undefined) {
1380
- acc[stateNode.id] = stateNode.meta;
1381
- }
1382
- return acc;
1383
- }, {});
1384
- }
1290
+ });
1291
+ return snapshot;
1385
1292
  }
1386
- function cloneState(state, config = {}) {
1387
- return new State({
1293
+ function cloneMachineSnapshot(state, config = {}) {
1294
+ return createMachineSnapshot(
1295
+ // TODO: it's wasteful that this spread triggers getters
1296
+ {
1388
1297
  ...state,
1389
1298
  ...config
1390
1299
  }, state.machine);
@@ -1396,6 +1305,11 @@ function getPersistedState(state, options) {
1396
1305
  machine,
1397
1306
  children,
1398
1307
  context,
1308
+ can,
1309
+ hasTag,
1310
+ matches,
1311
+ toJSON,
1312
+ nextEvents,
1399
1313
  ...jsonValues
1400
1314
  } = state;
1401
1315
  const childrenJson = {};
@@ -1493,4 +1407,4 @@ function raise(eventOrExpr, options) {
1493
1407
  return raise;
1494
1408
  }
1495
1409
 
1496
- export { raise as A, stop as B, spawn as C, State as S, formatTransition as a, formatInitialTransition as b, getCandidates as c, getConfiguration as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getStateNodes as h, isInFinalState as i, cloneState as j, macrostep as k, getInitialConfiguration as l, memo as m, resolveActionsAndContext as n, microstep as o, getInitialStateNodes as p, isStateId as q, resolveStateValue as r, getStateNodeByPath as s, transitionNode as t, getPersistedState as u, and as v, not as w, or as x, stateIn as y, cancel as z };
1410
+ export { raise as A, stop as B, spawn as C, formatTransition as a, formatInitialTransition as b, getCandidates as c, getConfiguration as d, evaluateGuard as e, formatTransitions as f, getDelayedTransitions as g, getStateNodes as h, createMachineSnapshot as i, isInFinalState as j, cloneMachineSnapshot as k, macrostep as l, memo as m, resolveActionsAndContext as n, microstep as o, getInitialStateNodes as p, isStateId as q, resolveStateValue as r, getStateNodeByPath as s, transitionNode as t, getPersistedState as u, and as v, not as w, or as x, stateIn as y, cancel as z };