xstate 5.0.0-beta.37 → 5.0.0-beta.38

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 (62) hide show
  1. package/actions/dist/xstate-actions.cjs.js +4 -3
  2. package/actions/dist/xstate-actions.cjs.mjs +1 -0
  3. package/actions/dist/xstate-actions.development.cjs.js +4 -3
  4. package/actions/dist/xstate-actions.development.cjs.mjs +1 -0
  5. package/actions/dist/xstate-actions.development.esm.js +3 -3
  6. package/actions/dist/xstate-actions.esm.js +3 -3
  7. package/actions/dist/xstate-actions.umd.min.js +1 -1
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  9. package/actors/dist/xstate-actors.cjs.js +1 -1
  10. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  11. package/actors/dist/xstate-actors.development.esm.js +1 -1
  12. package/actors/dist/xstate-actors.esm.js +1 -1
  13. package/actors/dist/xstate-actors.umd.min.js +1 -1
  14. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  15. package/dist/declarations/src/State.d.ts +2 -2
  16. package/dist/declarations/src/StateMachine.d.ts +4 -4
  17. package/dist/declarations/src/actions/assign.d.ts +4 -4
  18. package/dist/declarations/src/actions/cancel.d.ts +4 -4
  19. package/dist/declarations/src/actions/choose.d.ts +3 -3
  20. package/dist/declarations/src/actions/log.d.ts +4 -4
  21. package/dist/declarations/src/actions/pure.d.ts +4 -4
  22. package/dist/declarations/src/actions/raise.d.ts +3 -3
  23. package/dist/declarations/src/actions/send.d.ts +7 -7
  24. package/dist/declarations/src/actions/spawn.d.ts +34 -0
  25. package/dist/declarations/src/actions/stop.d.ts +4 -4
  26. package/dist/declarations/src/actions.d.ts +1 -0
  27. package/dist/declarations/src/actors/callback.d.ts +2 -2
  28. package/dist/declarations/src/actors/observable.d.ts +2 -3
  29. package/dist/declarations/src/actors/promise.d.ts +0 -1
  30. package/dist/declarations/src/actors/transition.d.ts +1 -1
  31. package/dist/declarations/src/guards.d.ts +9 -10
  32. package/dist/declarations/src/interpreter.d.ts +2 -2
  33. package/dist/declarations/src/spawn.d.ts +1 -2
  34. package/dist/declarations/src/stateUtils.d.ts +0 -5
  35. package/dist/declarations/src/types.d.ts +46 -57
  36. package/dist/{interpreter-1c52b23c.development.esm.js → interpreter-4005eb36.development.esm.js} +1 -1
  37. package/dist/{interpreter-97aff8d2.cjs.js → interpreter-b6f22ee2.cjs.js} +1 -1
  38. package/dist/{interpreter-8def682e.esm.js → interpreter-c80ce92e.esm.js} +1 -1
  39. package/dist/{interpreter-e58ca48d.development.cjs.js → interpreter-ed3f81f7.development.cjs.js} +1 -1
  40. package/dist/{raise-e342a840.development.esm.js → raise-42073973.development.esm.js} +68 -79
  41. package/dist/{raise-800296d7.cjs.js → raise-7faa9b3b.cjs.js} +69 -79
  42. package/dist/{raise-1fd59c65.development.cjs.js → raise-b69a3d16.development.cjs.js} +68 -78
  43. package/dist/{raise-21c417c1.esm.js → raise-c989c7fb.esm.js} +69 -80
  44. package/dist/{send-4cc29786.cjs.js → send-34160163.cjs.js} +27 -26
  45. package/dist/{send-92854675.esm.js → send-4b616da9.esm.js} +27 -26
  46. package/dist/{send-b309ef4e.development.cjs.js → send-58725522.development.cjs.js} +27 -26
  47. package/dist/{send-83ccc98b.development.esm.js → send-bff8c910.development.esm.js} +27 -26
  48. package/dist/xstate.cjs.js +9 -6
  49. package/dist/xstate.cjs.mjs +1 -0
  50. package/dist/xstate.development.cjs.js +9 -6
  51. package/dist/xstate.development.cjs.mjs +1 -0
  52. package/dist/xstate.development.esm.js +11 -9
  53. package/dist/xstate.esm.js +11 -9
  54. package/dist/xstate.umd.min.js +1 -1
  55. package/dist/xstate.umd.min.js.map +1 -1
  56. package/guards/dist/xstate-guards.cjs.js +2 -2
  57. package/guards/dist/xstate-guards.development.cjs.js +2 -2
  58. package/guards/dist/xstate-guards.development.esm.js +2 -2
  59. package/guards/dist/xstate-guards.esm.js +2 -2
  60. package/guards/dist/xstate-guards.umd.min.js +1 -1
  61. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  62. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var interpreter = require('./interpreter-e58ca48d.development.cjs.js');
3
+ var interpreter = require('./interpreter-ed3f81f7.development.cjs.js');
4
4
 
5
5
  const cache = new WeakMap();
6
6
  function memo(object, key, fn) {
@@ -16,10 +16,10 @@ function memo(object, key, fn) {
16
16
  return memoizedData[key];
17
17
  }
18
18
 
19
- function resolveCancel(_, state, actionArgs, {
19
+ function resolveCancel(_, state, actionArgs, actionParams, {
20
20
  sendId
21
21
  }) {
22
- const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs) : sendId;
22
+ const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
23
23
  return [state, resolvedSendId];
24
24
  }
25
25
  function executeCancel(actorContext, resolvedSendId) {
@@ -33,7 +33,7 @@ function executeCancel(actorContext, resolvedSendId) {
33
33
  * @param sendId The `id` of the `send(...)` action to cancel.
34
34
  */
35
35
  function cancel(sendId) {
36
- function cancel(_) {
36
+ function cancel(args, params) {
37
37
  {
38
38
  throw new Error(`This isn't supposed to be called`);
39
39
  }
@@ -45,7 +45,7 @@ function cancel(sendId) {
45
45
  return cancel;
46
46
  }
47
47
 
48
- function resolveInvoke(actorContext, state, actionArgs, {
48
+ function resolveSpawn(actorContext, state, actionArgs, _actionParams, {
49
49
  id,
50
50
  systemId,
51
51
  src,
@@ -56,12 +56,13 @@ function resolveInvoke(actorContext, state, actionArgs, {
56
56
  src,
57
57
  input: undefined
58
58
  };
59
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
59
60
  let actorRef;
60
61
  if (referenced) {
61
62
  // TODO: inline `input: undefined` should win over the referenced one
62
63
  const configuredInput = input || referenced.input;
63
64
  actorRef = interpreter.createActor(referenced.src, {
64
- id,
65
+ id: resolvedId,
65
66
  src: typeof src === 'string' ? src : undefined,
66
67
  parent: actorContext?.self,
67
68
  systemId,
@@ -81,9 +82,7 @@ function resolveInvoke(actorContext, state, actionArgs, {
81
82
  });
82
83
  }
83
84
  },
84
- error: () => {
85
- /* TODO */
86
- }
85
+ error: () => {}
87
86
  });
88
87
  }
89
88
  }
@@ -93,14 +92,14 @@ function resolveInvoke(actorContext, state, actionArgs, {
93
92
  return [cloneState(state, {
94
93
  children: {
95
94
  ...state.children,
96
- [id]: actorRef
95
+ [resolvedId]: actorRef
97
96
  }
98
97
  }), {
99
98
  id,
100
99
  actorRef
101
100
  }];
102
101
  }
103
- function executeInvoke(actorContext, {
102
+ function executeSpawn(actorContext, {
104
103
  id,
105
104
  actorRef
106
105
  }) {
@@ -119,36 +118,32 @@ function executeInvoke(actorContext, {
119
118
  }
120
119
  });
121
120
  }
122
-
123
- // we don't export this since it's an internal action that is not meant to be used in the user's code
124
-
125
- function invoke({
121
+ function spawn(...[src, {
126
122
  id,
127
123
  systemId,
128
- src,
129
124
  input,
130
- onSnapshot
131
- }) {
132
- function invoke(_) {
125
+ syncSnapshot = false
126
+ } = {}]) {
127
+ function spawn(args, params) {
133
128
  {
134
129
  throw new Error(`This isn't supposed to be called`);
135
130
  }
136
131
  }
137
- invoke.type = 'xstate.invoke';
138
- invoke.id = id;
139
- invoke.systemId = systemId;
140
- invoke.src = src;
141
- invoke.input = input;
142
- invoke.syncSnapshot = !!onSnapshot;
143
- invoke.resolve = resolveInvoke;
144
- invoke.execute = executeInvoke;
145
- return invoke;
132
+ spawn.type = 'xstate.spawn';
133
+ spawn.id = id;
134
+ spawn.systemId = systemId;
135
+ spawn.src = src;
136
+ spawn.input = input;
137
+ spawn.syncSnapshot = syncSnapshot;
138
+ spawn.resolve = resolveSpawn;
139
+ spawn.execute = executeSpawn;
140
+ return spawn;
146
141
  }
147
142
 
148
- function resolveStop(_, state, args, {
143
+ function resolveStop(_, state, args, actionParams, {
149
144
  actorRef
150
145
  }) {
151
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args) : actorRef;
146
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
152
147
  const resolvedActorRef = typeof actorRefOrString === 'string' ? state.children[actorRefOrString] : actorRefOrString;
153
148
  let children = state.children;
154
149
  if (resolvedActorRef) {
@@ -165,6 +160,12 @@ function executeStop(actorContext, actorRef) {
165
160
  if (!actorRef) {
166
161
  return;
167
162
  }
163
+
164
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
165
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
166
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
167
+ actorContext.system._unregister(actorRef);
168
+
168
169
  // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
169
170
  // this can happen, for example, when the invoking state is being exited immediately by an always transition
170
171
  if (actorRef.status !== interpreter.ActorStatus.Running) {
@@ -185,7 +186,7 @@ function executeStop(actorContext, actorRef) {
185
186
  * @param actorRef The actor to stop.
186
187
  */
187
188
  function stop(actorRef) {
188
- function stop(_) {
189
+ function stop(args, params) {
189
190
  {
190
191
  throw new Error(`This isn't supposed to be called`);
191
192
  }
@@ -207,7 +208,7 @@ function checkStateIn(state, _, {
207
208
  return state.matches(stateValue);
208
209
  }
209
210
  function stateIn(stateValue) {
210
- function stateIn(_) {
211
+ function stateIn(args, params) {
211
212
  {
212
213
  throw new Error(`This isn't supposed to be called`);
213
214
  }
@@ -225,7 +226,7 @@ function checkNot(state, {
225
226
  return !evaluateGuard(guards[0], context, event, state);
226
227
  }
227
228
  function not(guard) {
228
- function not(_) {
229
+ function not(args, params) {
229
230
  {
230
231
  throw new Error(`This isn't supposed to be called`);
231
232
  }
@@ -243,7 +244,7 @@ function checkAnd(state, {
243
244
  return guards.every(guard => evaluateGuard(guard, context, event, state));
244
245
  }
245
246
  function and(guards) {
246
- function and(_) {
247
+ function and(args, params) {
247
248
  {
248
249
  throw new Error(`This isn't supposed to be called`);
249
250
  }
@@ -261,7 +262,7 @@ function checkOr(state, {
261
262
  return guards.some(guard => evaluateGuard(guard, context, event, state));
262
263
  }
263
264
  function or(guards) {
264
- function or(_) {
265
+ function or(args, params) {
265
266
  {
266
267
  throw new Error(`This isn't supposed to be called`);
267
268
  }
@@ -286,22 +287,17 @@ function evaluateGuard(guard, context, event, state) {
286
287
  }
287
288
  const guardArgs = {
288
289
  context,
289
- event,
290
- guard: isInline ? undefined : typeof guard === 'string' ? {
291
- type: guard
292
- } : typeof guard.params === 'function' ? {
293
- type: guard.type,
294
- params: guard.params({
295
- context,
296
- event
297
- })
298
- } : guard
290
+ event
299
291
  };
292
+ const guardParams = isInline || typeof guard === 'string' ? undefined : 'params' in guard ? typeof guard.params === 'function' ? guard.params({
293
+ context,
294
+ event
295
+ }) : guard.params : undefined;
300
296
  if (!('check' in resolved)) {
301
297
  // the existing type of `.guards` assumes non-nullable `TExpressionGuard`
302
298
  // inline guards expect `TExpressionGuard` to be set to `undefined`
303
299
  // it's fine to cast this here, our logic makes sure that we call those 2 "variants" correctly
304
- return resolved(guardArgs);
300
+ return resolved(guardArgs, guardParams);
305
301
  }
306
302
  const builtinGuard = resolved;
307
303
  return builtinGuard.check(state, guardArgs, resolved // this holds all params
@@ -890,11 +886,6 @@ function areConfigurationsEqual(previousConfiguration, nextConfigurationSet) {
890
886
 
891
887
  /**
892
888
  * https://www.w3.org/TR/scxml/#microstepProcedure
893
- *
894
- * @private
895
- * @param transitions
896
- * @param currentState
897
- * @param mutConfiguration
898
889
  */
899
890
  function microstep(transitions, currentState, actorCtx, event, isInitial, internalQueue) {
900
891
  if (!transitions.length) {
@@ -961,7 +952,10 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
961
952
  // Add entry actions
962
953
  actions.push(...stateNodeToEnter.entry);
963
954
  for (const invokeDef of stateNodeToEnter.invoke) {
964
- actions.push(invoke(invokeDef));
955
+ actions.push(spawn(invokeDef.src, {
956
+ ...invokeDef,
957
+ syncSnapshot: !!invokeDef.onSnapshot
958
+ }));
965
959
  }
966
960
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
967
961
  const initialActions = stateNodeToEnter.initial.actions;
@@ -1125,31 +1119,24 @@ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions,
1125
1119
  context: intermediateState.context,
1126
1120
  event,
1127
1121
  self: actorCtx?.self,
1128
- system: actorCtx?.system,
1129
- action: isInline ? undefined : typeof action === 'string' ? {
1130
- type: action
1131
- } : typeof action.params === 'function' ? {
1132
- type: action.type,
1133
- params: action.params({
1134
- context: intermediateState.context,
1135
- event
1136
- })
1137
- } :
1138
- // TS isn't able to narrow it down here
1139
- action
1122
+ system: actorCtx?.system
1140
1123
  };
1124
+ const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
1125
+ context: intermediateState.context,
1126
+ event
1127
+ }) : action.params : undefined;
1141
1128
  if (!('resolve' in resolvedAction)) {
1142
1129
  if (actorCtx?.self.status === interpreter.ActorStatus.Running) {
1143
- resolvedAction(actionArgs);
1130
+ resolvedAction(actionArgs, actionParams);
1144
1131
  } else {
1145
1132
  actorCtx?.defer(() => {
1146
- resolvedAction(actionArgs);
1133
+ resolvedAction(actionArgs, actionParams);
1147
1134
  });
1148
1135
  }
1149
1136
  continue;
1150
1137
  }
1151
1138
  const builtinAction = resolvedAction;
1152
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1139
+ const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, actionParams, resolvedAction,
1153
1140
  // this holds all params
1154
1141
  extra);
1155
1142
  intermediateState = nextState;
@@ -1189,7 +1176,9 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1189
1176
 
1190
1177
  // Handle stop event
1191
1178
  if (event.type === interpreter.XSTATE_STOP) {
1192
- nextState = stopChildren(nextState, event, actorCtx);
1179
+ nextState = cloneState(stopChildren(nextState, event, actorCtx), {
1180
+ status: 'stopped'
1181
+ });
1193
1182
  states.push(nextState);
1194
1183
  return {
1195
1184
  state: nextState,
@@ -1452,17 +1441,17 @@ function getPersistedState(state) {
1452
1441
  throw new Error('An inline child actor cannot be persisted.');
1453
1442
  }
1454
1443
  childrenJson[id] = {
1455
- state: child.getPersistedState?.(),
1456
- src: child.src
1444
+ state: child.getPersistedState(),
1445
+ src: child.src,
1446
+ systemId: child._systemId
1457
1447
  };
1458
1448
  }
1459
- return {
1449
+ const persisted = {
1460
1450
  ...jsonValues,
1461
- // TODO: this makes `PersistedMachineState`'s type kind of a lie
1462
- // it doesn't truly use `TContext` but rather some kind of a derived form of it
1463
1451
  context: persistContext(context),
1464
1452
  children: childrenJson
1465
1453
  };
1454
+ return persisted;
1466
1455
  }
1467
1456
  function persistContext(contextPart) {
1468
1457
  let copy;
@@ -1491,7 +1480,7 @@ function persistContext(contextPart) {
1491
1480
  return copy ?? contextPart;
1492
1481
  }
1493
1482
 
1494
- function resolveRaise(_, state, args, {
1483
+ function resolveRaise(_, state, args, actionParams, {
1495
1484
  event: eventOrExpr,
1496
1485
  id,
1497
1486
  delay
@@ -1502,13 +1491,13 @@ function resolveRaise(_, state, args, {
1502
1491
  if (typeof eventOrExpr === 'string') {
1503
1492
  throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1504
1493
  }
1505
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
1494
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1506
1495
  let resolvedDelay;
1507
1496
  if (typeof delay === 'string') {
1508
1497
  const configDelay = delaysMap && delaysMap[delay];
1509
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
1498
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1510
1499
  } else {
1511
- resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
1500
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1512
1501
  }
1513
1502
  if (typeof resolvedDelay !== 'number') {
1514
1503
  internalQueue.push(resolvedEvent);
@@ -1532,7 +1521,7 @@ function executeRaise(actorContext, params) {
1532
1521
  * @param eventType The event to raise.
1533
1522
  */
1534
1523
  function raise(eventOrExpr, options) {
1535
- function raise(_) {
1524
+ function raise(args, params) {
1536
1525
  {
1537
1526
  throw new Error(`This isn't supposed to be called`);
1538
1527
  }
@@ -1572,6 +1561,7 @@ exports.or = or;
1572
1561
  exports.raise = raise;
1573
1562
  exports.resolveActionsAndContext = resolveActionsAndContext;
1574
1563
  exports.resolveStateValue = resolveStateValue;
1564
+ exports.spawn = spawn;
1575
1565
  exports.stateIn = stateIn;
1576
1566
  exports.stop = stop;
1577
1567
  exports.transitionNode = transitionNode;