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,4 +1,4 @@
1
- import { r as resolveReferencedActor, d as createActor, f as ActorStatus, j as createErrorActorEvent, k as toStateValue, l as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, o as toStatePath, q as createDoneStateEvent, s as resolveOutput, W as WILDCARD, X as XSTATE_STOP, u as XSTATE_INIT, v as createAfterEvent, w as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-1c52b23c.development.esm.js';
1
+ import { r as resolveReferencedActor, d as createActor, f as ActorStatus, j as createErrorActorEvent, k as toStateValue, l as STATE_IDENTIFIER, n as normalizeTarget, t as toArray, N as NULL_EVENT, a as toTransitionConfigArray, S as STATE_DELIMITER, o as toStatePath, q as createDoneStateEvent, s as resolveOutput, W as WILDCARD, X as XSTATE_STOP, u as XSTATE_INIT, v as createAfterEvent, w as flatten, e as matchesState, $ as $$ACTOR_TYPE } from './interpreter-4005eb36.development.esm.js';
2
2
 
3
3
  const cache = new WeakMap();
4
4
  function memo(object, key, fn) {
@@ -14,10 +14,10 @@ function memo(object, key, fn) {
14
14
  return memoizedData[key];
15
15
  }
16
16
 
17
- function resolveCancel(_, state, actionArgs, {
17
+ function resolveCancel(_, state, actionArgs, actionParams, {
18
18
  sendId
19
19
  }) {
20
- const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs) : sendId;
20
+ const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
21
21
  return [state, resolvedSendId];
22
22
  }
23
23
  function executeCancel(actorContext, resolvedSendId) {
@@ -31,7 +31,7 @@ function executeCancel(actorContext, resolvedSendId) {
31
31
  * @param sendId The `id` of the `send(...)` action to cancel.
32
32
  */
33
33
  function cancel(sendId) {
34
- function cancel(_) {
34
+ function cancel(args, params) {
35
35
  {
36
36
  throw new Error(`This isn't supposed to be called`);
37
37
  }
@@ -43,7 +43,7 @@ function cancel(sendId) {
43
43
  return cancel;
44
44
  }
45
45
 
46
- function resolveInvoke(actorContext, state, actionArgs, {
46
+ function resolveSpawn(actorContext, state, actionArgs, _actionParams, {
47
47
  id,
48
48
  systemId,
49
49
  src,
@@ -54,12 +54,13 @@ function resolveInvoke(actorContext, state, actionArgs, {
54
54
  src,
55
55
  input: undefined
56
56
  };
57
+ const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
57
58
  let actorRef;
58
59
  if (referenced) {
59
60
  // TODO: inline `input: undefined` should win over the referenced one
60
61
  const configuredInput = input || referenced.input;
61
62
  actorRef = createActor(referenced.src, {
62
- id,
63
+ id: resolvedId,
63
64
  src: typeof src === 'string' ? src : undefined,
64
65
  parent: actorContext?.self,
65
66
  systemId,
@@ -79,9 +80,7 @@ function resolveInvoke(actorContext, state, actionArgs, {
79
80
  });
80
81
  }
81
82
  },
82
- error: () => {
83
- /* TODO */
84
- }
83
+ error: () => {}
85
84
  });
86
85
  }
87
86
  }
@@ -91,14 +90,14 @@ function resolveInvoke(actorContext, state, actionArgs, {
91
90
  return [cloneState(state, {
92
91
  children: {
93
92
  ...state.children,
94
- [id]: actorRef
93
+ [resolvedId]: actorRef
95
94
  }
96
95
  }), {
97
96
  id,
98
97
  actorRef
99
98
  }];
100
99
  }
101
- function executeInvoke(actorContext, {
100
+ function executeSpawn(actorContext, {
102
101
  id,
103
102
  actorRef
104
103
  }) {
@@ -117,36 +116,32 @@ function executeInvoke(actorContext, {
117
116
  }
118
117
  });
119
118
  }
120
-
121
- // we don't export this since it's an internal action that is not meant to be used in the user's code
122
-
123
- function invoke({
119
+ function spawn(...[src, {
124
120
  id,
125
121
  systemId,
126
- src,
127
122
  input,
128
- onSnapshot
129
- }) {
130
- function invoke(_) {
123
+ syncSnapshot = false
124
+ } = {}]) {
125
+ function spawn(args, params) {
131
126
  {
132
127
  throw new Error(`This isn't supposed to be called`);
133
128
  }
134
129
  }
135
- invoke.type = 'xstate.invoke';
136
- invoke.id = id;
137
- invoke.systemId = systemId;
138
- invoke.src = src;
139
- invoke.input = input;
140
- invoke.syncSnapshot = !!onSnapshot;
141
- invoke.resolve = resolveInvoke;
142
- invoke.execute = executeInvoke;
143
- return invoke;
130
+ spawn.type = 'xstate.spawn';
131
+ spawn.id = id;
132
+ spawn.systemId = systemId;
133
+ spawn.src = src;
134
+ spawn.input = input;
135
+ spawn.syncSnapshot = syncSnapshot;
136
+ spawn.resolve = resolveSpawn;
137
+ spawn.execute = executeSpawn;
138
+ return spawn;
144
139
  }
145
140
 
146
- function resolveStop(_, state, args, {
141
+ function resolveStop(_, state, args, actionParams, {
147
142
  actorRef
148
143
  }) {
149
- const actorRefOrString = typeof actorRef === 'function' ? actorRef(args) : actorRef;
144
+ const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
150
145
  const resolvedActorRef = typeof actorRefOrString === 'string' ? state.children[actorRefOrString] : actorRefOrString;
151
146
  let children = state.children;
152
147
  if (resolvedActorRef) {
@@ -163,6 +158,12 @@ function executeStop(actorContext, actorRef) {
163
158
  if (!actorRef) {
164
159
  return;
165
160
  }
161
+
162
+ // we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
163
+ // since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
164
+ // this could throw on `systemId` collision, for example, when dealing with reentering transitions
165
+ actorContext.system._unregister(actorRef);
166
+
166
167
  // this allows us to prevent an actor from being started if it gets stopped within the same macrostep
167
168
  // this can happen, for example, when the invoking state is being exited immediately by an always transition
168
169
  if (actorRef.status !== ActorStatus.Running) {
@@ -183,7 +184,7 @@ function executeStop(actorContext, actorRef) {
183
184
  * @param actorRef The actor to stop.
184
185
  */
185
186
  function stop(actorRef) {
186
- function stop(_) {
187
+ function stop(args, params) {
187
188
  {
188
189
  throw new Error(`This isn't supposed to be called`);
189
190
  }
@@ -205,7 +206,7 @@ function checkStateIn(state, _, {
205
206
  return state.matches(stateValue);
206
207
  }
207
208
  function stateIn(stateValue) {
208
- function stateIn(_) {
209
+ function stateIn(args, params) {
209
210
  {
210
211
  throw new Error(`This isn't supposed to be called`);
211
212
  }
@@ -223,7 +224,7 @@ function checkNot(state, {
223
224
  return !evaluateGuard(guards[0], context, event, state);
224
225
  }
225
226
  function not(guard) {
226
- function not(_) {
227
+ function not(args, params) {
227
228
  {
228
229
  throw new Error(`This isn't supposed to be called`);
229
230
  }
@@ -241,7 +242,7 @@ function checkAnd(state, {
241
242
  return guards.every(guard => evaluateGuard(guard, context, event, state));
242
243
  }
243
244
  function and(guards) {
244
- function and(_) {
245
+ function and(args, params) {
245
246
  {
246
247
  throw new Error(`This isn't supposed to be called`);
247
248
  }
@@ -259,7 +260,7 @@ function checkOr(state, {
259
260
  return guards.some(guard => evaluateGuard(guard, context, event, state));
260
261
  }
261
262
  function or(guards) {
262
- function or(_) {
263
+ function or(args, params) {
263
264
  {
264
265
  throw new Error(`This isn't supposed to be called`);
265
266
  }
@@ -284,22 +285,17 @@ function evaluateGuard(guard, context, event, state) {
284
285
  }
285
286
  const guardArgs = {
286
287
  context,
287
- event,
288
- guard: isInline ? undefined : typeof guard === 'string' ? {
289
- type: guard
290
- } : typeof guard.params === 'function' ? {
291
- type: guard.type,
292
- params: guard.params({
293
- context,
294
- event
295
- })
296
- } : guard
288
+ event
297
289
  };
290
+ const guardParams = isInline || typeof guard === 'string' ? undefined : 'params' in guard ? typeof guard.params === 'function' ? guard.params({
291
+ context,
292
+ event
293
+ }) : guard.params : undefined;
298
294
  if (!('check' in resolved)) {
299
295
  // the existing type of `.guards` assumes non-nullable `TExpressionGuard`
300
296
  // inline guards expect `TExpressionGuard` to be set to `undefined`
301
297
  // it's fine to cast this here, our logic makes sure that we call those 2 "variants" correctly
302
- return resolved(guardArgs);
298
+ return resolved(guardArgs, guardParams);
303
299
  }
304
300
  const builtinGuard = resolved;
305
301
  return builtinGuard.check(state, guardArgs, resolved // this holds all params
@@ -888,11 +884,6 @@ function areConfigurationsEqual(previousConfiguration, nextConfigurationSet) {
888
884
 
889
885
  /**
890
886
  * https://www.w3.org/TR/scxml/#microstepProcedure
891
- *
892
- * @private
893
- * @param transitions
894
- * @param currentState
895
- * @param mutConfiguration
896
887
  */
897
888
  function microstep(transitions, currentState, actorCtx, event, isInitial, internalQueue) {
898
889
  if (!transitions.length) {
@@ -959,7 +950,10 @@ function enterStates(currentState, event, actorCtx, filteredTransitions, mutConf
959
950
  // Add entry actions
960
951
  actions.push(...stateNodeToEnter.entry);
961
952
  for (const invokeDef of stateNodeToEnter.invoke) {
962
- actions.push(invoke(invokeDef));
953
+ actions.push(spawn(invokeDef.src, {
954
+ ...invokeDef,
955
+ syncSnapshot: !!invokeDef.onSnapshot
956
+ }));
963
957
  }
964
958
  if (statesForDefaultEntry.has(stateNodeToEnter)) {
965
959
  const initialActions = stateNodeToEnter.initial.actions;
@@ -1123,31 +1117,24 @@ function resolveActionsAndContextWorker(currentState, event, actorCtx, actions,
1123
1117
  context: intermediateState.context,
1124
1118
  event,
1125
1119
  self: actorCtx?.self,
1126
- system: actorCtx?.system,
1127
- action: isInline ? undefined : typeof action === 'string' ? {
1128
- type: action
1129
- } : typeof action.params === 'function' ? {
1130
- type: action.type,
1131
- params: action.params({
1132
- context: intermediateState.context,
1133
- event
1134
- })
1135
- } :
1136
- // TS isn't able to narrow it down here
1137
- action
1120
+ system: actorCtx?.system
1138
1121
  };
1122
+ const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
1123
+ context: intermediateState.context,
1124
+ event
1125
+ }) : action.params : undefined;
1139
1126
  if (!('resolve' in resolvedAction)) {
1140
1127
  if (actorCtx?.self.status === ActorStatus.Running) {
1141
- resolvedAction(actionArgs);
1128
+ resolvedAction(actionArgs, actionParams);
1142
1129
  } else {
1143
1130
  actorCtx?.defer(() => {
1144
- resolvedAction(actionArgs);
1131
+ resolvedAction(actionArgs, actionParams);
1145
1132
  });
1146
1133
  }
1147
1134
  continue;
1148
1135
  }
1149
1136
  const builtinAction = resolvedAction;
1150
- const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, resolvedAction,
1137
+ const [nextState, params, actions] = builtinAction.resolve(actorCtx, intermediateState, actionArgs, actionParams, resolvedAction,
1151
1138
  // this holds all params
1152
1139
  extra);
1153
1140
  intermediateState = nextState;
@@ -1187,7 +1174,9 @@ function macrostep(state, event, actorCtx, internalQueue = []) {
1187
1174
 
1188
1175
  // Handle stop event
1189
1176
  if (event.type === XSTATE_STOP) {
1190
- nextState = stopChildren(nextState, event, actorCtx);
1177
+ nextState = cloneState(stopChildren(nextState, event, actorCtx), {
1178
+ status: 'stopped'
1179
+ });
1191
1180
  states.push(nextState);
1192
1181
  return {
1193
1182
  state: nextState,
@@ -1450,17 +1439,17 @@ function getPersistedState(state) {
1450
1439
  throw new Error('An inline child actor cannot be persisted.');
1451
1440
  }
1452
1441
  childrenJson[id] = {
1453
- state: child.getPersistedState?.(),
1454
- src: child.src
1442
+ state: child.getPersistedState(),
1443
+ src: child.src,
1444
+ systemId: child._systemId
1455
1445
  };
1456
1446
  }
1457
- return {
1447
+ const persisted = {
1458
1448
  ...jsonValues,
1459
- // TODO: this makes `PersistedMachineState`'s type kind of a lie
1460
- // it doesn't truly use `TContext` but rather some kind of a derived form of it
1461
1449
  context: persistContext(context),
1462
1450
  children: childrenJson
1463
1451
  };
1452
+ return persisted;
1464
1453
  }
1465
1454
  function persistContext(contextPart) {
1466
1455
  let copy;
@@ -1489,7 +1478,7 @@ function persistContext(contextPart) {
1489
1478
  return copy ?? contextPart;
1490
1479
  }
1491
1480
 
1492
- function resolveRaise(_, state, args, {
1481
+ function resolveRaise(_, state, args, actionParams, {
1493
1482
  event: eventOrExpr,
1494
1483
  id,
1495
1484
  delay
@@ -1500,13 +1489,13 @@ function resolveRaise(_, state, args, {
1500
1489
  if (typeof eventOrExpr === 'string') {
1501
1490
  throw new Error(`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
1502
1491
  }
1503
- const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
1492
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
1504
1493
  let resolvedDelay;
1505
1494
  if (typeof delay === 'string') {
1506
1495
  const configDelay = delaysMap && delaysMap[delay];
1507
- resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
1496
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
1508
1497
  } else {
1509
- resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
1498
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
1510
1499
  }
1511
1500
  if (typeof resolvedDelay !== 'number') {
1512
1501
  internalQueue.push(resolvedEvent);
@@ -1530,7 +1519,7 @@ function executeRaise(actorContext, params) {
1530
1519
  * @param eventType The event to raise.
1531
1520
  */
1532
1521
  function raise(eventOrExpr, options) {
1533
- function raise(_) {
1522
+ function raise(args, params) {
1534
1523
  {
1535
1524
  throw new Error(`This isn't supposed to be called`);
1536
1525
  }
@@ -1544,4 +1533,4 @@ function raise(eventOrExpr, options) {
1544
1533
  return raise;
1545
1534
  }
1546
1535
 
1547
- export { raise as A, stop as B, 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 };
1536
+ 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 };