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-97aff8d2.cjs.js');
3
+ var interpreter = require('./interpreter-b6f22ee2.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
  cancel.type = 'xstate.cancel';
39
39
  cancel.sendId = sendId;
@@ -42,7 +42,7 @@ function cancel(sendId) {
42
42
  return cancel;
43
43
  }
44
44
 
45
- function resolveInvoke(actorContext, state, actionArgs, {
45
+ function resolveSpawn(actorContext, state, actionArgs, _actionParams, {
46
46
  id,
47
47
  systemId,
48
48
  src,
@@ -53,12 +53,13 @@ function resolveInvoke(actorContext, state, actionArgs, {
53
53
  src,
54
54
  input: undefined
55
55
  };
56
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
56
57
  let actorRef;
57
58
  if (referenced) {
58
59
  // TODO: inline `input: undefined` should win over the referenced one
59
60
  const configuredInput = input || referenced.input;
60
61
  actorRef = interpreter.createActor(referenced.src, {
61
- id,
62
+ id: resolvedId,
62
63
  src: typeof src === 'string' ? src : undefined,
63
64
  parent: actorContext?.self,
64
65
  systemId,
@@ -78,23 +79,21 @@ function resolveInvoke(actorContext, state, actionArgs, {
78
79
  });
79
80
  }
80
81
  },
81
- error: () => {
82
- /* TODO */
83
- }
82
+ error: () => {}
84
83
  });
85
84
  }
86
85
  }
87
86
  return [cloneState(state, {
88
87
  children: {
89
88
  ...state.children,
90
- [id]: actorRef
89
+ [resolvedId]: actorRef
91
90
  }
92
91
  }), {
93
92
  id,
94
93
  actorRef
95
94
  }];
96
95
  }
97
- function executeInvoke(actorContext, {
96
+ function executeSpawn(actorContext, {
98
97
  id,
99
98
  actorRef
100
99
  }) {
@@ -113,33 +112,29 @@ function executeInvoke(actorContext, {
113
112
  }
114
113
  });
115
114
  }
116
-
117
- // we don't export this since it's an internal action that is not meant to be used in the user's code
118
-
119
- function invoke({
115
+ function spawn(...[src, {
120
116
  id,
121
117
  systemId,
122
- src,
123
118
  input,
124
- onSnapshot
125
- }) {
126
- function invoke(_) {
127
- }
128
- invoke.type = 'xstate.invoke';
129
- invoke.id = id;
130
- invoke.systemId = systemId;
131
- invoke.src = src;
132
- invoke.input = input;
133
- invoke.syncSnapshot = !!onSnapshot;
134
- invoke.resolve = resolveInvoke;
135
- invoke.execute = executeInvoke;
136
- return invoke;
119
+ syncSnapshot = false
120
+ } = {}]) {
121
+ function spawn(args, params) {
122
+ }
123
+ spawn.type = 'xstate.spawn';
124
+ spawn.id = id;
125
+ spawn.systemId = systemId;
126
+ spawn.src = src;
127
+ spawn.input = input;
128
+ spawn.syncSnapshot = syncSnapshot;
129
+ spawn.resolve = resolveSpawn;
130
+ spawn.execute = executeSpawn;
131
+ return spawn;
137
132
  }
138
133
 
139
- function resolveStop(_, state, args, {
134
+ function resolveStop(_, state, args, actionParams, {
140
135
  actorRef
141
136
  }) {
142
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args) : actorRef;
137
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
143
138
  const resolvedActorRef = typeof actorRefOrString === 'string' ? state.children[actorRefOrString] : actorRefOrString;
144
139
  let children = state.children;
145
140
  if (resolvedActorRef) {
@@ -156,6 +151,12 @@ function executeStop(actorContext, actorRef) {
156
151
  if (!actorRef) {
157
152
  return;
158
153
  }
154
+
155
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
156
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
157
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
158
+ actorContext.system._unregister(actorRef);
159
+
159
160
  // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
160
161
  // this can happen, for example, when the invoking state is being exited immediately by an always transition
161
162
  if (actorRef.status !== interpreter.ActorStatus.Running) {
@@ -176,7 +177,7 @@ function executeStop(actorContext, actorRef) {
176
177
  * @param actorRef The actor to stop.
177
178
  */
178
179
  function stop(actorRef) {
179
- function stop(_) {
180
+ function stop(args, params) {
180
181
  }
181
182
  stop.type = 'xstate.stop';
182
183
  stop.actorRef = actorRef;
@@ -195,7 +196,7 @@ function checkStateIn(state, _, {
195
196
  return state.matches(stateValue);
196
197
  }
197
198
  function stateIn(stateValue) {
198
- function stateIn(_) {
199
+ function stateIn(args, params) {
199
200
  return false;
200
201
  }
201
202
  stateIn.check = checkStateIn;
@@ -211,7 +212,7 @@ function checkNot(state, {
211
212
  return !evaluateGuard(guards[0], context, event, state);
212
213
  }
213
214
  function not(guard) {
214
- function not(_) {
215
+ function not(args, params) {
215
216
  return false;
216
217
  }
217
218
  not.check = checkNot;
@@ -227,7 +228,7 @@ function checkAnd(state, {
227
228
  return guards.every(guard => evaluateGuard(guard, context, event, state));
228
229
  }
229
230
  function and(guards) {
230
- function and(_) {
231
+ function and(args, params) {
231
232
  return false;
232
233
  }
233
234
  and.check = checkAnd;
@@ -243,7 +244,7 @@ function checkOr(state, {
243
244
  return guards.some(guard => evaluateGuard(guard, context, event, state));
244
245
  }
245
246
  function or(guards) {
246
- function or(_) {
247
+ function or(args, params) {
247
248
  return false;
248
249
  }
249
250
  or.check = checkOr;
@@ -266,22 +267,17 @@ function evaluateGuard(guard, context, event, state) {
266
267
  }
267
268
  const guardArgs = {
268
269
  context,
269
- event,
270
- guard: isInline ? undefined : typeof guard === 'string' ? {
271
- type: guard
272
- } : typeof guard.params === 'function' ? {
273
- type: guard.type,
274
- params: guard.params({
275
- context,
276
- event
277
- })
278
- } : guard
270
+ event
279
271
  };
272
+ const guardParams = isInline || typeof guard === 'string' ? undefined : 'params' in guard ? typeof guard.params === 'function' ? guard.params({
273
+ context,
274
+ event
275
+ }) : guard.params : undefined;
280
276
  if (!('check' in resolved)) {
281
277
  // the existing type of `.guards` assumes non-nullable `TExpressionGuard`
282
278
  // inline guards expect `TExpressionGuard` to be set to `undefined`
283
279
  // it's fine to cast this here, our logic makes sure that we call those 2 "variants" correctly
284
- return resolved(guardArgs);
280
+ return resolved(guardArgs, guardParams);
285
281
  }
286
282
  const builtinGuard = resolved;
287
283
  return builtinGuard.check(state, guardArgs, resolved // this holds all params
@@ -859,11 +855,6 @@ function areConfigurationsEqual(previousConfiguration, nextConfigurationSet) {
859
855
 
860
856
  /**
861
857
  * https://www.w3.org/TR/scxml/#microstepProcedure
862
- *
863
- * @private
864
- * @param transitions
865
- * @param currentState
866
- * @param mutConfiguration
867
858
  */
868
859
  function microstep(transitions, currentState, actorCtx, event, isInitial, internalQueue) {
869
860
  if (!transitions.length) {
@@ -930,7 +921,10 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
930
921
  // Add entry actions
931
922
  actions.push(...stateNodeToEnter.entry);
932
923
  for (const invokeDef of stateNodeToEnter.invoke) {
933
- actions.push(invoke(invokeDef));
924
+ actions.push(spawn(invokeDef.src, {
925
+ ...invokeDef,
926
+ syncSnapshot: !!invokeDef.onSnapshot
927
+ }));
934
928
  }
935
929
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
936
930
  const initialActions = stateNodeToEnter.initial.actions;
@@ -1094,31 +1088,24 @@ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions,
1094
1088
  context: intermediateState.context,
1095
1089
  event,
1096
1090
  self: actorCtx?.self,
1097
- system: actorCtx?.system,
1098
- action: isInline ? undefined : typeof action === 'string' ? {
1099
- type: action
1100
- } : typeof action.params === 'function' ? {
1101
- type: action.type,
1102
- params: action.params({
1103
- context: intermediateState.context,
1104
- event
1105
- })
1106
- } :
1107
- // TS isn't able to narrow it down here
1108
- action
1091
+ system: actorCtx?.system
1109
1092
  };
1093
+ const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
1094
+ context: intermediateState.context,
1095
+ event
1096
+ }) : action.params : undefined;
1110
1097
  if (!('resolve' in resolvedAction)) {
1111
1098
  if (actorCtx?.self.status === interpreter.ActorStatus.Running) {
1112
- resolvedAction(actionArgs);
1099
+ resolvedAction(actionArgs, actionParams);
1113
1100
  } else {
1114
1101
  actorCtx?.defer(() => {
1115
- resolvedAction(actionArgs);
1102
+ resolvedAction(actionArgs, actionParams);
1116
1103
  });
1117
1104
  }
1118
1105
  continue;
1119
1106
  }
1120
1107
  const builtinAction = resolvedAction;
1121
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1108
+ const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, actionParams, resolvedAction,
1122
1109
  // this holds all params
1123
1110
  extra);
1124
1111
  intermediateState = nextState;
@@ -1155,7 +1142,9 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1155
1142
 
1156
1143
  // Handle stop event
1157
1144
  if (event.type === interpreter.XSTATE_STOP) {
1158
- nextState = stopChildren(nextState, event, actorCtx);
1145
+ nextState = cloneState(stopChildren(nextState, event, actorCtx), {
1146
+ status: 'stopped'
1147
+ });
1159
1148
  states.push(nextState);
1160
1149
  return {
1161
1150
  state: nextState,
@@ -1412,17 +1401,17 @@ function getPersistedState(state) {
1412
1401
  for (const id in children) {
1413
1402
  const child = children[id];
1414
1403
  childrenJson[id] = {
1415
- state: child.getPersistedState?.(),
1416
- src: child.src
1404
+ state: child.getPersistedState(),
1405
+ src: child.src,
1406
+ systemId: child._systemId
1417
1407
  };
1418
1408
  }
1419
- return {
1409
+ const persisted = {
1420
1410
  ...jsonValues,
1421
- // TODO: this makes `PersistedMachineState`'s type kind of a lie
1422
- // it doesn't truly use `TContext` but rather some kind of a derived form of it
1423
1411
  context: persistContext(context),
1424
1412
  children: childrenJson
1425
1413
  };
1414
+ return persisted;
1426
1415
  }
1427
1416
  function persistContext(contextPart) {
1428
1417
  let copy;
@@ -1451,7 +1440,7 @@ function persistContext(contextPart) {
1451
1440
  return copy ?? contextPart;
1452
1441
  }
1453
1442
 
1454
- function resolveRaise(_, state, args, {
1443
+ function resolveRaise(_, state, args, actionParams, {
1455
1444
  event: eventOrExpr,
1456
1445
  id,
1457
1446
  delay
@@ -1462,13 +1451,13 @@ function resolveRaise(_, state, args, {
1462
1451
  if (typeof eventOrExpr === 'string') {
1463
1452
  throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1464
1453
  }
1465
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
1454
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1466
1455
  let resolvedDelay;
1467
1456
  if (typeof delay === 'string') {
1468
1457
  const configDelay = delaysMap && delaysMap[delay];
1469
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
1458
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1470
1459
  } else {
1471
- resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
1460
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1472
1461
  }
1473
1462
  if (typeof resolvedDelay !== 'number') {
1474
1463
  internalQueue.push(resolvedEvent);
@@ -1492,7 +1481,7 @@ function executeRaise(actorContext, params) {
1492
1481
  * @param eventType The event to raise.
1493
1482
  */
1494
1483
  function raise(eventOrExpr, options) {
1495
- function raise(_) {
1484
+ function raise(args, params) {
1496
1485
  }
1497
1486
  raise.type = 'xstate.raise';
1498
1487
  raise.event = eventOrExpr;
@@ -1529,6 +1518,7 @@ exports.or = or;
1529
1518
  exports.raise = raise;
1530
1519
  exports.resolveActionsAndContext = resolveActionsAndContext;
1531
1520
  exports.resolveStateValue = resolveStateValue;
1521
+ exports.spawn = spawn;
1532
1522
  exports.stateIn = stateIn;
1533
1523
  exports.stop = stop;
1534
1524
  exports.transitionNode = transitionNode;