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