xstate 5.0.0-beta.41 → 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 (45) 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.map +1 -1
  12. package/dist/declarations/src/State.d.ts +35 -42
  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/index.d.ts +3 -4
  16. package/dist/declarations/src/interpreter.d.ts +12 -0
  17. package/dist/declarations/src/stateUtils.d.ts +7 -8
  18. package/dist/declarations/src/types.d.ts +112 -15
  19. package/dist/{interpreter-70ed981b.development.cjs.js → interpreter-23e4041c.development.cjs.js} +23 -10
  20. package/dist/{interpreter-480db258.esm.js → interpreter-3d0c0ff2.esm.js} +24 -10
  21. package/dist/{interpreter-fb2829f1.cjs.js → interpreter-b6bdd134.cjs.js} +23 -10
  22. package/dist/{interpreter-936da690.development.esm.js → interpreter-f2620ea7.development.esm.js} +24 -10
  23. package/dist/{raise-f757be00.development.esm.js → raise-51ae36e5.development.esm.js} +76 -175
  24. package/dist/{raise-beae3fd3.cjs.js → raise-6b64c553.cjs.js} +74 -174
  25. package/dist/{raise-5ab465ed.development.cjs.js → raise-8f482ce9.development.cjs.js} +77 -177
  26. package/dist/{raise-9d6921da.esm.js → raise-d2084327.esm.js} +73 -172
  27. package/dist/{send-a931d1b8.esm.js → send-4e732fa5.esm.js} +5 -3
  28. package/dist/{send-b26e3812.development.esm.js → send-7a350091.development.esm.js} +5 -3
  29. package/dist/{send-ca5f706c.cjs.js → send-85b562d8.cjs.js} +5 -3
  30. package/dist/{send-fb87a01a.development.cjs.js → send-cc8f864e.development.cjs.js} +5 -3
  31. package/dist/xstate.cjs.js +23 -49
  32. package/dist/xstate.cjs.mjs +0 -2
  33. package/dist/xstate.development.cjs.js +23 -49
  34. package/dist/xstate.development.cjs.mjs +0 -2
  35. package/dist/xstate.development.esm.js +27 -51
  36. package/dist/xstate.esm.js +27 -51
  37. package/dist/xstate.umd.min.js +1 -1
  38. package/dist/xstate.umd.min.js.map +1 -1
  39. package/guards/dist/xstate-guards.cjs.js +2 -2
  40. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  41. package/guards/dist/xstate-guards.development.esm.js +2 -2
  42. package/guards/dist/xstate-guards.esm.js +2 -2
  43. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  44. package/package.json +1 -1
  45. 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, W as WILDCARD, X as XSTATE_STOP, s as XSTATE_INIT, u as createAfterEvent, v as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-936da690.development.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, W as WILDCARD, X as XSTATE_STOP, q as XSTATE_INIT, s as createAfterEvent, u as flatten, f as matchesState, $ as $$ACTOR_TYPE } from './interpreter-f2620ea7.development.esm.js';
2
2
 
3
3
  const cache = new WeakMap();
4
4
  function memo(object, key, fn) {
@@ -87,7 +87,7 @@ function resolveSpawn(actorScope, state, actionArgs, _actionParams, {
87
87
  if (!actorRef) {
88
88
  console.warn(`Actor type '${src}' not found in machine '${actorScope.id}'.`);
89
89
  }
90
- return [cloneState(state, {
90
+ return [cloneMachineSnapshot(state, {
91
91
  children: {
92
92
  ...state.children,
93
93
  [resolvedId]: actorRef
@@ -150,7 +150,7 @@ function resolveStop(_, state, args, actionParams, {
150
150
  };
151
151
  delete children[resolvedActorRef.id];
152
152
  }
153
- return [cloneState(state, {
153
+ return [cloneMachineSnapshot(state, {
154
154
  children
155
155
  }), resolvedActorRef];
156
156
  }
@@ -683,10 +683,9 @@ function getStateNodeByPath(stateNode, statePath) {
683
683
  /**
684
684
  * Returns the state nodes represented by the current state value.
685
685
  *
686
- * @param state The state value or State instance
686
+ * @param stateValue The state value or State instance
687
687
  */
688
- function getStateNodes(stateNode, state) {
689
- const stateValue = state instanceof State ? state.value : toStateValue(state);
688
+ function getStateNodes(stateNode, stateValue) {
690
689
  if (typeof stateValue === 'string') {
691
690
  return [stateNode, stateNode.states[stateValue]];
692
691
  }
@@ -907,7 +906,7 @@ function microstep(transitions, currentState, actorScope, event, isInitial, inte
907
906
  if (historyValue === currentState.historyValue && areConfigurationsEqual(currentState.configuration, mutConfiguration)) {
908
907
  return nextState;
909
908
  }
910
- return cloneState(nextState, {
909
+ return cloneMachineSnapshot(nextState, {
911
910
  configuration: nextConfiguration,
912
911
  historyValue
913
912
  });
@@ -971,9 +970,9 @@ function enterStates(currentState, event, actorScope, filteredTransitions, mutCo
971
970
  if (ancestorMarker) {
972
971
  continue;
973
972
  }
974
- nextState = cloneState(nextState, {
973
+ nextState = cloneMachineSnapshot(nextState, {
975
974
  status: 'done',
976
- output: getMachineOutput(nextState, event, actorScope, currentState.configuration[0].machine.root, rootCompletionNode)
975
+ output: getMachineOutput(nextState, event, actorScope, nextState.machine.root, rootCompletionNode)
977
976
  });
978
977
  }
979
978
  }
@@ -1177,7 +1176,7 @@ function macrostep(state, event, actorScope, internalQueue = []) {
1177
1176
 
1178
1177
  // Handle stop event
1179
1178
  if (event.type === XSTATE_STOP) {
1180
- nextState = cloneState(stopChildren(nextState, event, actorScope), {
1179
+ nextState = cloneMachineSnapshot(stopChildren(nextState, event, actorScope), {
1181
1180
  status: 'stopped'
1182
1181
  });
1183
1182
  states.push(nextState);
@@ -1255,173 +1254,70 @@ function resolveStateValue(rootNode, stateValue) {
1255
1254
  const configuration = getConfiguration(getStateNodes(rootNode, stateValue));
1256
1255
  return getStateValue(rootNode, [...configuration]);
1257
1256
  }
1258
- function getInitialConfiguration(rootNode) {
1259
- const configuration = [];
1260
- const initialTransition = rootNode.initial;
1261
- const statesToEnter = new Set();
1262
- const statesForDefaultEntry = new Set([rootNode]);
1263
- computeEntrySet([initialTransition], {}, statesForDefaultEntry, statesToEnter);
1264
- for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
1265
- configuration.push(stateNodeToEnter);
1266
- }
1267
- return configuration;
1268
- }
1269
-
1270
- class State {
1271
- /**
1272
- * Indicates whether the state is a final state.
1273
- */
1274
-
1275
- /**
1276
- * The output data of the top-level finite state.
1277
- */
1278
-
1279
- /**
1280
- * The enabled state nodes representative of the state value.
1281
- */
1282
-
1283
- /**
1284
- * An object mapping actor names to spawned/invoked actors.
1285
- */
1286
-
1287
- /**
1288
- * Creates a new State instance for the given `stateValue` and `context`.
1289
- * @param stateValue
1290
- * @param context
1291
- */
1292
- static from(stateValue, context = {}, machine) {
1293
- if (stateValue instanceof State) {
1294
- if (stateValue.context !== context) {
1295
- return new State({
1296
- value: stateValue.value,
1297
- context,
1298
- meta: {},
1299
- configuration: [],
1300
- // TODO: fix,
1301
- children: {},
1302
- status: 'active'
1303
- }, machine);
1304
- }
1305
- return stateValue;
1306
- }
1307
- const configuration = getConfiguration(getStateNodes(machine.root, stateValue));
1308
- return new State({
1309
- value: stateValue,
1310
- context,
1311
- meta: undefined,
1312
- configuration: Array.from(configuration),
1313
- children: {},
1314
- status: 'active'
1315
- }, machine);
1316
- }
1317
-
1318
- /**
1319
- * Creates a new `State` instance that represents the current state of a running machine.
1320
- *
1321
- * @param config
1322
- */
1323
- constructor(config, machine) {
1324
- this.machine = machine;
1325
- this.tags = void 0;
1326
- this.value = void 0;
1327
- this.status = void 0;
1328
- this.error = void 0;
1329
- this.context = void 0;
1330
- this.historyValue = {};
1331
- this.configuration = void 0;
1332
- this.children = void 0;
1333
- this.context = config.context;
1334
- this.historyValue = config.historyValue || {};
1335
- this.matches = this.matches.bind(this);
1336
- this.toStrings = this.toStrings.bind(this);
1337
- this.configuration = config.configuration ?? Array.from(getConfiguration(getStateNodes(machine.root, config.value)));
1338
- this.children = config.children;
1339
- this.value = getStateValue(machine.root, this.configuration);
1340
- this.tags = new Set(flatten(this.configuration.map(sn => sn.tags)));
1341
- this.status = config.status;
1342
- this.output = config.output;
1343
- this.error = config.error;
1344
- }
1345
-
1346
- /**
1347
- * Returns an array of all the string leaf state node paths.
1348
- * @param stateValue
1349
- * @param delimiter The character(s) that separate each subpath in the string state node path.
1350
- */
1351
- toStrings(stateValue = this.value) {
1352
- if (typeof stateValue === 'string') {
1353
- return [stateValue];
1354
- }
1355
- const valueKeys = Object.keys(stateValue);
1356
- return valueKeys.concat(...valueKeys.map(key => this.toStrings(stateValue[key]).map(s => key + STATE_DELIMITER + s)));
1357
- }
1358
- toJSON() {
1359
- const {
1360
- configuration,
1361
- tags,
1362
- machine,
1363
- ...jsonValues
1364
- } = this;
1365
- return {
1366
- ...jsonValues,
1367
- tags: Array.from(tags),
1368
- meta: this.meta
1369
- };
1370
- }
1371
-
1372
- /**
1373
- * Whether the current state value is a subset of the given parent state value.
1374
- * @param parentStateValue
1375
- */
1376
- matches(parentStateValue) {
1377
- return matchesState(parentStateValue, this.value);
1378
- }
1379
-
1380
- /**
1381
- * Whether the current state configuration has a state node with the specified `tag`.
1382
- * @param tag
1383
- */
1384
- hasTag(tag) {
1385
- return this.tags.has(tag);
1386
- }
1387
1257
 
1388
- /**
1389
- * Determines whether sending the `event` will cause a non-forbidden transition
1390
- * to be selected, even if the transitions have no actions nor
1391
- * change the state value.
1392
- *
1393
- * @param event The event to test
1394
- * @returns Whether the event will cause a transition
1395
- */
1396
- can(event) {
1397
- if (!this.machine) {
1398
- console.warn(`state.can(...) used outside of a machine-created State object; this will always return false.`);
1399
- }
1400
- const transitionData = this.machine.getTransitionData(this, event);
1401
- return !!transitionData?.length &&
1402
- // Check that at least one transition is not forbidden
1403
- transitionData.some(t => t.target !== undefined || t.actions.length);
1404
- }
1405
-
1406
- /**
1407
- * The next events that will cause a transition from the current state.
1408
- */
1409
- get nextEvents() {
1410
- return memo(this, 'nextEvents', () => {
1411
- return [...new Set(flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1412
- });
1413
- }
1414
- get meta() {
1415
- return this.configuration.reduce((acc, stateNode) => {
1416
- if (stateNode.meta !== undefined) {
1417
- acc[stateNode.id] = stateNode.meta;
1258
+ function createMachineSnapshot(config, machine) {
1259
+ return {
1260
+ status: config.status,
1261
+ output: config.output,
1262
+ error: config.error,
1263
+ machine,
1264
+ context: config.context,
1265
+ configuration: config.configuration,
1266
+ value: getStateValue(machine.root, config.configuration),
1267
+ tags: new Set(flatten(config.configuration.map(sn => sn.tags))),
1268
+ children: config.children,
1269
+ historyValue: config.historyValue || {},
1270
+ matches(parentStateValue) {
1271
+ return matchesState(parentStateValue, this.value);
1272
+ },
1273
+ hasTag(tag) {
1274
+ return this.tags.has(tag);
1275
+ },
1276
+ can(event) {
1277
+ if (!this.machine) {
1278
+ console.warn(`state.can(...) used outside of a machine-created State object; this will always return false.`);
1418
1279
  }
1419
- return acc;
1420
- }, {});
1421
- }
1280
+ const transitionData = this.machine.getTransitionData(this, event);
1281
+ return !!transitionData?.length &&
1282
+ // Check that at least one transition is not forbidden
1283
+ transitionData.some(t => t.target !== undefined || t.actions.length);
1284
+ },
1285
+ get nextEvents() {
1286
+ return memo(this, 'nextEvents', () => {
1287
+ return [...new Set(flatten([...this.configuration.map(sn => sn.ownEvents)]))];
1288
+ });
1289
+ },
1290
+ get meta() {
1291
+ return this.configuration.reduce((acc, stateNode) => {
1292
+ if (stateNode.meta !== undefined) {
1293
+ acc[stateNode.id] = stateNode.meta;
1294
+ }
1295
+ return acc;
1296
+ }, {});
1297
+ },
1298
+ toJSON() {
1299
+ const {
1300
+ configuration,
1301
+ tags,
1302
+ machine,
1303
+ nextEvents,
1304
+ toJSON,
1305
+ can,
1306
+ hasTag,
1307
+ matches,
1308
+ ...jsonValues
1309
+ } = this;
1310
+ return {
1311
+ ...jsonValues,
1312
+ tags: Array.from(tags)
1313
+ };
1314
+ }
1315
+ };
1422
1316
  }
1423
- function cloneState(state, config = {}) {
1424
- return new State({
1317
+ function cloneMachineSnapshot(state, config = {}) {
1318
+ return createMachineSnapshot(
1319
+ // TODO: it's wasteful that this spread triggers getters
1320
+ {
1425
1321
  ...state,
1426
1322
  ...config
1427
1323
  }, state.machine);
@@ -1433,6 +1329,11 @@ function getPersistedState(state, options) {
1433
1329
  machine,
1434
1330
  children,
1435
1331
  context,
1332
+ can,
1333
+ hasTag,
1334
+ matches,
1335
+ toJSON,
1336
+ nextEvents,
1436
1337
  ...jsonValues
1437
1338
  } = state;
1438
1339
  const childrenJson = {};
@@ -1536,4 +1437,4 @@ function raise(eventOrExpr, options) {
1536
1437
  return raise;
1537
1438
  }
1538
1439
 
1539
- 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 };
1440
+ 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 };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var interpreter = require('./interpreter-fb2829f1.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) {
@@ -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
@@ -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
  }
@@ -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
  }
@@ -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,170 +1222,67 @@ 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);
@@ -1398,6 +1294,11 @@ function getPersistedState(state, options) {
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 = {};
@@ -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;