xstate 5.0.0-beta.26 → 5.0.0-beta.28

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 (71) hide show
  1. package/README.md +9 -7
  2. package/actions/dist/xstate-actions.cjs.js +14 -18
  3. package/actions/dist/xstate-actions.cjs.mjs +0 -6
  4. package/actions/dist/xstate-actions.development.cjs.js +14 -18
  5. package/actions/dist/xstate-actions.development.cjs.mjs +0 -6
  6. package/actions/dist/xstate-actions.development.esm.js +3 -1
  7. package/actions/dist/xstate-actions.esm.js +3 -1
  8. package/actions/dist/xstate-actions.umd.min.js +1 -1
  9. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  10. package/actors/dist/xstate-actors.cjs.js +439 -14
  11. package/actors/dist/xstate-actors.cjs.mjs +0 -5
  12. package/actors/dist/xstate-actors.development.cjs.js +439 -14
  13. package/actors/dist/xstate-actors.development.cjs.mjs +0 -5
  14. package/actors/dist/xstate-actors.development.esm.js +435 -1
  15. package/actors/dist/xstate-actors.esm.js +435 -1
  16. package/actors/dist/xstate-actors.umd.min.js +1 -1
  17. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  18. package/dist/declarations/src/Machine.d.ts +4 -3
  19. package/dist/declarations/src/State.d.ts +1 -1
  20. package/dist/declarations/src/StateNode.d.ts +3 -12
  21. package/dist/declarations/src/actions/assign.d.ts +11 -11
  22. package/dist/declarations/src/actions/cancel.d.ts +5 -12
  23. package/dist/declarations/src/actions/choose.d.ts +9 -7
  24. package/dist/declarations/src/actions/log.d.ts +5 -20
  25. package/dist/declarations/src/actions/pure.d.ts +11 -9
  26. package/dist/declarations/src/actions/raise.d.ts +6 -5
  27. package/dist/declarations/src/actions/send.d.ts +7 -12
  28. package/dist/declarations/src/actions/stop.d.ts +5 -12
  29. package/dist/declarations/src/actions.d.ts +8 -42
  30. package/dist/declarations/src/actors/index.d.ts +2 -22
  31. package/dist/declarations/src/actors/promise.d.ts +2 -1
  32. package/dist/declarations/src/constants.d.ts +8 -0
  33. package/dist/declarations/src/guards.d.ts +2 -2
  34. package/dist/declarations/src/index.d.ts +11 -18
  35. package/dist/declarations/src/spawn.d.ts +25 -0
  36. package/dist/declarations/src/stateUtils.d.ts +1 -1
  37. package/dist/declarations/src/typegenTypes.d.ts +5 -5
  38. package/dist/declarations/src/types.d.ts +81 -108
  39. package/dist/declarations/src/utils.d.ts +2 -3
  40. package/dist/interpreter-672794ae.cjs.js +792 -0
  41. package/dist/interpreter-a1432c7d.development.cjs.js +800 -0
  42. package/dist/interpreter-a77bb0ec.development.esm.js +765 -0
  43. package/dist/interpreter-b5203bcb.esm.js +757 -0
  44. package/dist/raise-436a57a2.cjs.js +1458 -0
  45. package/dist/raise-74b72ca5.development.cjs.js +1498 -0
  46. package/dist/raise-a60c9290.development.esm.js +1468 -0
  47. package/dist/raise-b9c9a234.esm.js +1428 -0
  48. package/dist/send-35e1a689.cjs.js +349 -0
  49. package/dist/send-4192e7bc.esm.js +339 -0
  50. package/dist/send-e63b7b83.development.esm.js +364 -0
  51. package/dist/send-e8b55d00.development.cjs.js +374 -0
  52. package/dist/xstate.cjs.js +140 -160
  53. package/dist/xstate.cjs.mjs +4 -2
  54. package/dist/xstate.development.cjs.js +140 -160
  55. package/dist/xstate.development.cjs.mjs +4 -2
  56. package/dist/xstate.development.esm.js +75 -95
  57. package/dist/xstate.esm.js +75 -95
  58. package/dist/xstate.umd.min.js +1 -1
  59. package/dist/xstate.umd.min.js.map +1 -1
  60. package/guards/dist/xstate-guards.cjs.js +7 -6
  61. package/guards/dist/xstate-guards.development.cjs.js +7 -6
  62. package/guards/dist/xstate-guards.development.esm.js +2 -1
  63. package/guards/dist/xstate-guards.esm.js +2 -1
  64. package/guards/dist/xstate-guards.umd.min.js +1 -1
  65. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  66. package/package.json +1 -1
  67. package/dist/actions-0971b43d.development.cjs.js +0 -3168
  68. package/dist/actions-319cefe7.cjs.js +0 -3095
  69. package/dist/actions-5943a9db.esm.js +0 -3018
  70. package/dist/actions-cf69419d.development.esm.js +0 -3091
  71. package/dist/declarations/src/constantPrefixes.d.ts +0 -6
@@ -0,0 +1,364 @@
1
+ import { j as cloneState, e as evaluateGuard } from './raise-a60c9290.development.esm.js';
2
+ import { f as ActorStatus, l as createErrorActorEvent, r as resolveReferencedActor, d as createActor, t as toArray, C as XSTATE_ERROR } from './interpreter-a77bb0ec.development.esm.js';
3
+
4
+ function createSpawner(actorContext, {
5
+ machine,
6
+ context
7
+ }, event, spawnedChildren) {
8
+ const spawn = (src, options = {}) => {
9
+ const {
10
+ systemId
11
+ } = options;
12
+ if (typeof src === 'string') {
13
+ const referenced = resolveReferencedActor(machine.implementations.actors[src]);
14
+ if (!referenced) {
15
+ throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
16
+ }
17
+ const input = 'input' in options ? options.input : referenced.input;
18
+
19
+ // TODO: this should also receive `src`
20
+ const actor = createActor(referenced.src, {
21
+ id: options.id,
22
+ parent: actorContext.self,
23
+ input: typeof input === 'function' ? input({
24
+ context,
25
+ event,
26
+ self: actorContext.self
27
+ }) : input,
28
+ systemId
29
+ });
30
+ spawnedChildren[actor.id] = actor;
31
+ return actor;
32
+ } else {
33
+ // TODO: this should also receive `src`
34
+ return createActor(src, {
35
+ id: options.id,
36
+ parent: actorContext.self,
37
+ input: options.input,
38
+ systemId
39
+ });
40
+ }
41
+ };
42
+ return (src, options) => {
43
+ const actorRef = spawn(src, options); // TODO: fix types
44
+ spawnedChildren[actorRef.id] = actorRef;
45
+ actorContext.defer(() => {
46
+ if (actorRef.status === ActorStatus.Stopped) {
47
+ return;
48
+ }
49
+ try {
50
+ actorRef.start?.();
51
+ } catch (err) {
52
+ actorContext.self.send(createErrorActorEvent(actorRef.id, err));
53
+ return;
54
+ }
55
+ });
56
+ return actorRef;
57
+ };
58
+ }
59
+
60
+ function resolve$4(actorContext, state, actionArgs, {
61
+ assignment
62
+ }) {
63
+ if (!state.context) {
64
+ throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
65
+ }
66
+ const spawnedChildren = {};
67
+ const assignArgs = {
68
+ context: state.context,
69
+ event: actionArgs.event,
70
+ action: actionArgs.action,
71
+ spawn: createSpawner(actorContext, state, actionArgs.event, spawnedChildren),
72
+ self: actorContext?.self,
73
+ system: actorContext?.system
74
+ };
75
+ let partialUpdate = {};
76
+ if (typeof assignment === 'function') {
77
+ partialUpdate = assignment(assignArgs);
78
+ } else {
79
+ for (const key of Object.keys(assignment)) {
80
+ const propAssignment = assignment[key];
81
+ partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs) : propAssignment;
82
+ }
83
+ }
84
+ const updatedContext = Object.assign({}, state.context, partialUpdate);
85
+ return [cloneState(state, {
86
+ context: updatedContext,
87
+ children: Object.keys(spawnedChildren).length ? {
88
+ ...state.children,
89
+ ...spawnedChildren
90
+ } : state.children
91
+ })];
92
+ }
93
+ /**
94
+ * Updates the current context of the machine.
95
+ *
96
+ * @param assignment An object that represents the partial context to update.
97
+ */
98
+ function assign(assignment) {
99
+ function assign(_) {
100
+ {
101
+ throw new Error(`This isn't supposed to be called`);
102
+ }
103
+ }
104
+ assign.type = 'xstate.assign';
105
+ assign.assignment = assignment;
106
+ assign.resolve = resolve$4;
107
+ return assign;
108
+ }
109
+
110
+ function resolve$3(_, state, actionArgs, {
111
+ branches
112
+ }) {
113
+ const matchedActions = branches.find(condition => {
114
+ return !condition.guard || evaluateGuard(condition.guard, state.context, actionArgs.event, state);
115
+ })?.actions;
116
+ return [state, undefined, toArray(matchedActions)];
117
+ }
118
+ function choose(branches) {
119
+ function choose(_) {
120
+ {
121
+ throw new Error(`This isn't supposed to be called`);
122
+ }
123
+ }
124
+ choose.type = 'xstate.choose';
125
+ choose.branches = branches;
126
+ choose.resolve = resolve$3;
127
+ return choose;
128
+ }
129
+
130
+ function resolve$2(_, state, actionArgs, {
131
+ value,
132
+ label
133
+ }) {
134
+ return [state, {
135
+ value: typeof value === 'function' ? value(actionArgs) : value,
136
+ label
137
+ }];
138
+ }
139
+ function execute$1({
140
+ logger
141
+ }, {
142
+ value,
143
+ label
144
+ }) {
145
+ if (label) {
146
+ logger(label, value);
147
+ } else {
148
+ logger(value);
149
+ }
150
+ }
151
+ /**
152
+ *
153
+ * @param expr The expression function to evaluate which will be logged.
154
+ * Takes in 2 arguments:
155
+ * - `ctx` - the current state context
156
+ * - `event` - the event that caused this action to be executed.
157
+ * @param label The label to give to the logged expression.
158
+ */
159
+ function log(value = ({
160
+ context,
161
+ event
162
+ }) => ({
163
+ context,
164
+ event
165
+ }), label) {
166
+ function log(_) {
167
+ {
168
+ throw new Error(`This isn't supposed to be called`);
169
+ }
170
+ }
171
+ log.type = 'xstate.log';
172
+ log.value = value;
173
+ log.label = label;
174
+ log.resolve = resolve$2;
175
+ log.execute = execute$1;
176
+ return log;
177
+ }
178
+
179
+ function resolve$1(_, state, args, {
180
+ get
181
+ }) {
182
+ return [state, undefined, toArray(get({
183
+ context: args.context,
184
+ event: args.event
185
+ }))];
186
+ }
187
+ function pure(getActions) {
188
+ function pure(_) {
189
+ {
190
+ throw new Error(`This isn't supposed to be called`);
191
+ }
192
+ }
193
+ pure.type = 'xstate.pure';
194
+ pure.get = getActions;
195
+ pure.resolve = resolve$1;
196
+ return pure;
197
+ }
198
+
199
+ /**
200
+ * `T | unknown` reduces to `unknown` and that can be problematic when it comes to contextual typing.
201
+ * It especially is a problem when the union has a function member, like here:
202
+ *
203
+ * ```ts
204
+ * declare function test(cbOrVal: ((arg: number) => unknown) | unknown): void;
205
+ * test((arg) => {}) // oops, implicit any
206
+ * ```
207
+ *
208
+ * This type can be used to avoid this problem. This union represents the same value space as `unknown`.
209
+ */
210
+
211
+ // https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
212
+
213
+ /**
214
+ * The full definition of an event, with a string `type`.
215
+ */
216
+
217
+ /**
218
+ * The string or object representing the state value relative to the parent state node.
219
+ *
220
+ * - For a child atomic state node, this is a string, e.g., `"pending"`.
221
+ * - For complex state nodes, this is an object, e.g., `{ success: "someChildState" }`.
222
+ */
223
+
224
+ // TODO: remove once TS fixes this type-widening issue
225
+
226
+ // TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
227
+
228
+ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
229
+ SpecialTargets["Parent"] = "#_parent";
230
+ SpecialTargets["Internal"] = "#_internal";
231
+ return SpecialTargets;
232
+ }({});
233
+
234
+ function resolve(actorContext, state, args, {
235
+ to,
236
+ event: eventOrExpr,
237
+ id,
238
+ delay
239
+ }) {
240
+ const delaysMap = state.machine.implementations.delays;
241
+ if (typeof eventOrExpr === 'string') {
242
+ throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
243
+ }
244
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
245
+ let resolvedDelay;
246
+ if (typeof delay === 'string') {
247
+ const configDelay = delaysMap && delaysMap[delay];
248
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
249
+ } else {
250
+ resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
251
+ }
252
+ const resolvedTarget = typeof to === 'function' ? to(args) : to;
253
+ let targetActorRef;
254
+ if (typeof resolvedTarget === 'string') {
255
+ if (resolvedTarget === SpecialTargets.Parent) {
256
+ targetActorRef = actorContext?.self._parent;
257
+ } else if (resolvedTarget === SpecialTargets.Internal) {
258
+ targetActorRef = actorContext?.self;
259
+ } else if (resolvedTarget.startsWith('#_')) {
260
+ // SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
261
+ // #_invokeid. If the target is the special term '#_invokeid', where invokeid is the invokeid of an SCXML session that the sending session has created by <invoke>, the Processor must add the event to the external queue of that session.
262
+ targetActorRef = state.children[resolvedTarget.slice(2)];
263
+ } else {
264
+ targetActorRef = state.children[resolvedTarget];
265
+ }
266
+ if (!targetActorRef) {
267
+ throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${state.machine.id}'.`);
268
+ }
269
+ } else {
270
+ targetActorRef = resolvedTarget || actorContext?.self;
271
+ }
272
+ return [state, {
273
+ to: targetActorRef,
274
+ event: resolvedEvent,
275
+ id,
276
+ delay: resolvedDelay
277
+ }];
278
+ }
279
+ function execute(actorContext, params) {
280
+ if (typeof params.delay === 'number') {
281
+ actorContext.self.delaySend(params);
282
+ return;
283
+ }
284
+ const {
285
+ to,
286
+ event
287
+ } = params;
288
+ actorContext.defer(() => {
289
+ to.send(event.type === XSTATE_ERROR ? createErrorActorEvent(actorContext.self.id, event.data) : event);
290
+ });
291
+ }
292
+ /**
293
+ * Sends an event to an actor.
294
+ *
295
+ * @param actor The `ActorRef` to send the event to.
296
+ * @param event The event to send, or an expression that evaluates to the event to send
297
+ * @param options Send action options
298
+ * - `id` - The unique send event identifier (used with `cancel()`).
299
+ * - `delay` - The number of milliseconds to delay the sending of the event.
300
+ */
301
+ function sendTo(to, eventOrExpr, options) {
302
+ function sendTo(_) {
303
+ {
304
+ throw new Error(`This isn't supposed to be called`);
305
+ }
306
+ }
307
+ sendTo.type = 'xstate.sendTo';
308
+ sendTo.to = to;
309
+ sendTo.event = eventOrExpr;
310
+ sendTo.id = options?.id;
311
+ sendTo.delay = options?.delay;
312
+ sendTo.resolve = resolve;
313
+ sendTo.execute = execute;
314
+ return sendTo;
315
+ }
316
+
317
+ /**
318
+ * Sends an event to this machine's parent.
319
+ *
320
+ * @param event The event to send to the parent machine.
321
+ * @param options Options to pass into the send event.
322
+ */
323
+ function sendParent(event, options) {
324
+ return sendTo(SpecialTargets.Parent, event, options);
325
+ }
326
+ /**
327
+ * Forwards (sends) an event to a specified service.
328
+ *
329
+ * @param target The target service to forward the event to.
330
+ * @param options Options to pass into the send action creator.
331
+ */
332
+ function forwardTo(target, options) {
333
+ if ((!target || typeof target === 'function')) {
334
+ const originalTarget = target;
335
+ target = (...args) => {
336
+ const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
337
+ if (!resolvedTarget) {
338
+ throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
339
+ }
340
+ return resolvedTarget;
341
+ };
342
+ }
343
+ return sendTo(target, ({
344
+ event
345
+ }) => event, options);
346
+ }
347
+
348
+ /**
349
+ * Escalates an error by sending it as an event to this machine's parent.
350
+ *
351
+ * @param errorData The error data to send, or the expression function that
352
+ * takes in the `context`, `event`, and `meta`, and returns the error data to send.
353
+ * @param options Options to pass into the send action creator.
354
+ */
355
+ function escalate(errorData, options) {
356
+ return sendParent(arg => {
357
+ return {
358
+ type: XSTATE_ERROR,
359
+ data: typeof errorData === 'function' ? errorData(arg) : errorData
360
+ };
361
+ }, options);
362
+ }
363
+
364
+ export { SpecialTargets as S, assign as a, sendTo as b, choose as c, escalate as e, forwardTo as f, log as l, pure as p, sendParent as s };