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,349 @@
1
+ 'use strict';
2
+
3
+ var guards_dist_xstateGuards = require('./raise-436a57a2.cjs.js');
4
+ var interpreter = require('./interpreter-672794ae.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
+ assign.type = 'xstate.assign';
104
+ assign.assignment = assignment;
105
+ assign.resolve = resolve$4;
106
+ return assign;
107
+ }
108
+
109
+ function resolve$3(_, state, actionArgs, {
110
+ branches
111
+ }) {
112
+ const matchedActions = branches.find(condition => {
113
+ return !condition.guard || guards_dist_xstateGuards.evaluateGuard(condition.guard, state.context, actionArgs.event, state);
114
+ })?.actions;
115
+ return [state, undefined, interpreter.toArray(matchedActions)];
116
+ }
117
+ function choose(branches) {
118
+ function choose(_) {
119
+ }
120
+ choose.type = 'xstate.choose';
121
+ choose.branches = branches;
122
+ choose.resolve = resolve$3;
123
+ return choose;
124
+ }
125
+
126
+ function resolve$2(_, state, actionArgs, {
127
+ value,
128
+ label
129
+ }) {
130
+ return [state, {
131
+ value: typeof value === 'function' ? value(actionArgs) : value,
132
+ label
133
+ }];
134
+ }
135
+ function execute$1({
136
+ logger
137
+ }, {
138
+ value,
139
+ label
140
+ }) {
141
+ if (label) {
142
+ logger(label, value);
143
+ } else {
144
+ logger(value);
145
+ }
146
+ }
147
+ /**
148
+ *
149
+ * @param expr The expression function to evaluate which will be logged.
150
+ * Takes in 2 arguments:
151
+ * - `ctx` - the current state context
152
+ * - `event` - the event that caused this action to be executed.
153
+ * @param label The label to give to the logged expression.
154
+ */
155
+ function log(value = ({
156
+ context,
157
+ event
158
+ }) => ({
159
+ context,
160
+ event
161
+ }), label) {
162
+ function log(_) {
163
+ }
164
+ log.type = 'xstate.log';
165
+ log.value = value;
166
+ log.label = label;
167
+ log.resolve = resolve$2;
168
+ log.execute = execute$1;
169
+ return log;
170
+ }
171
+
172
+ function resolve$1(_, state, args, {
173
+ get
174
+ }) {
175
+ return [state, undefined, interpreter.toArray(get({
176
+ context: args.context,
177
+ event: args.event
178
+ }))];
179
+ }
180
+ function pure(getActions) {
181
+ function pure(_) {
182
+ }
183
+ pure.type = 'xstate.pure';
184
+ pure.get = getActions;
185
+ pure.resolve = resolve$1;
186
+ return pure;
187
+ }
188
+
189
+ /**
190
+ * `T | unknown` reduces to `unknown` and that can be problematic when it comes to contextual typing.
191
+ * It especially is a problem when the union has a function member, like here:
192
+ *
193
+ * ```ts
194
+ * declare function test(cbOrVal: ((arg: number) => unknown) | unknown): void;
195
+ * test((arg) => {}) // oops, implicit any
196
+ * ```
197
+ *
198
+ * This type can be used to avoid this problem. This union represents the same value space as `unknown`.
199
+ */
200
+
201
+ // https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
202
+
203
+ /**
204
+ * The full definition of an event, with a string `type`.
205
+ */
206
+
207
+ /**
208
+ * The string or object representing the state value relative to the parent state node.
209
+ *
210
+ * - For a child atomic state node, this is a string, e.g., `"pending"`.
211
+ * - For complex state nodes, this is an object, e.g., `{ success: "someChildState" }`.
212
+ */
213
+
214
+ // TODO: remove once TS fixes this type-widening issue
215
+
216
+ // TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
217
+
218
+ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
219
+ SpecialTargets["Parent"] = "#_parent";
220
+ SpecialTargets["Internal"] = "#_internal";
221
+ return SpecialTargets;
222
+ }({});
223
+
224
+ function resolve(actorContext, state, args, {
225
+ to,
226
+ event: eventOrExpr,
227
+ id,
228
+ delay
229
+ }) {
230
+ const delaysMap = state.machine.implementations.delays;
231
+ if (typeof eventOrExpr === 'string') {
232
+ throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
233
+ }
234
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
235
+ let resolvedDelay;
236
+ if (typeof delay === 'string') {
237
+ const configDelay = delaysMap && delaysMap[delay];
238
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
239
+ } else {
240
+ resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
241
+ }
242
+ const resolvedTarget = typeof to === 'function' ? to(args) : to;
243
+ let targetActorRef;
244
+ if (typeof resolvedTarget === 'string') {
245
+ if (resolvedTarget === SpecialTargets.Parent) {
246
+ targetActorRef = actorContext?.self._parent;
247
+ } else if (resolvedTarget === SpecialTargets.Internal) {
248
+ targetActorRef = actorContext?.self;
249
+ } else if (resolvedTarget.startsWith('#_')) {
250
+ // SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
251
+ // #_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.
252
+ targetActorRef = state.children[resolvedTarget.slice(2)];
253
+ } else {
254
+ targetActorRef = state.children[resolvedTarget];
255
+ }
256
+ if (!targetActorRef) {
257
+ throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${state.machine.id}'.`);
258
+ }
259
+ } else {
260
+ targetActorRef = resolvedTarget || actorContext?.self;
261
+ }
262
+ return [state, {
263
+ to: targetActorRef,
264
+ event: resolvedEvent,
265
+ id,
266
+ delay: resolvedDelay
267
+ }];
268
+ }
269
+ function execute(actorContext, params) {
270
+ if (typeof params.delay === 'number') {
271
+ actorContext.self.delaySend(params);
272
+ return;
273
+ }
274
+ const {
275
+ to,
276
+ event
277
+ } = params;
278
+ actorContext.defer(() => {
279
+ to.send(event.type === interpreter.XSTATE_ERROR ? interpreter.createErrorActorEvent(actorContext.self.id, event.data) : event);
280
+ });
281
+ }
282
+ /**
283
+ * Sends an event to an actor.
284
+ *
285
+ * @param actor The `ActorRef` to send the event to.
286
+ * @param event The event to send, or an expression that evaluates to the event to send
287
+ * @param options Send action options
288
+ * - `id` - The unique send event identifier (used with `cancel()`).
289
+ * - `delay` - The number of milliseconds to delay the sending of the event.
290
+ */
291
+ function sendTo(to, eventOrExpr, options) {
292
+ function sendTo(_) {
293
+ }
294
+ sendTo.type = 'xstate.sendTo';
295
+ sendTo.to = to;
296
+ sendTo.event = eventOrExpr;
297
+ sendTo.id = options?.id;
298
+ sendTo.delay = options?.delay;
299
+ sendTo.resolve = resolve;
300
+ sendTo.execute = execute;
301
+ return sendTo;
302
+ }
303
+
304
+ /**
305
+ * Sends an event to this machine's parent.
306
+ *
307
+ * @param event The event to send to the parent machine.
308
+ * @param options Options to pass into the send event.
309
+ */
310
+ function sendParent(event, options) {
311
+ return sendTo(SpecialTargets.Parent, event, options);
312
+ }
313
+ /**
314
+ * Forwards (sends) an event to a specified service.
315
+ *
316
+ * @param target The target service to forward the event to.
317
+ * @param options Options to pass into the send action creator.
318
+ */
319
+ function forwardTo(target, options) {
320
+ return sendTo(target, ({
321
+ event
322
+ }) => event, options);
323
+ }
324
+
325
+ /**
326
+ * Escalates an error by sending it as an event to this machine's parent.
327
+ *
328
+ * @param errorData The error data to send, or the expression function that
329
+ * takes in the `context`, `event`, and `meta`, and returns the error data to send.
330
+ * @param options Options to pass into the send action creator.
331
+ */
332
+ function escalate(errorData, options) {
333
+ return sendParent(arg => {
334
+ return {
335
+ type: interpreter.XSTATE_ERROR,
336
+ data: typeof errorData === 'function' ? errorData(arg) : errorData
337
+ };
338
+ }, options);
339
+ }
340
+
341
+ exports.SpecialTargets = SpecialTargets;
342
+ exports.assign = assign;
343
+ exports.choose = choose;
344
+ exports.escalate = escalate;
345
+ exports.forwardTo = forwardTo;
346
+ exports.log = log;
347
+ exports.pure = pure;
348
+ exports.sendParent = sendParent;
349
+ exports.sendTo = sendTo;
@@ -0,0 +1,339 @@
1
+ import { j as cloneState, e as evaluateGuard } from './raise-b9c9a234.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-b5203bcb.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
+ assign.type = 'xstate.assign';
102
+ assign.assignment = assignment;
103
+ assign.resolve = resolve$4;
104
+ return assign;
105
+ }
106
+
107
+ function resolve$3(_, state, actionArgs, {
108
+ branches
109
+ }) {
110
+ const matchedActions = branches.find(condition => {
111
+ return !condition.guard || evaluateGuard(condition.guard, state.context, actionArgs.event, state);
112
+ })?.actions;
113
+ return [state, undefined, toArray(matchedActions)];
114
+ }
115
+ function choose(branches) {
116
+ function choose(_) {
117
+ }
118
+ choose.type = 'xstate.choose';
119
+ choose.branches = branches;
120
+ choose.resolve = resolve$3;
121
+ return choose;
122
+ }
123
+
124
+ function resolve$2(_, state, actionArgs, {
125
+ value,
126
+ label
127
+ }) {
128
+ return [state, {
129
+ value: typeof value === 'function' ? value(actionArgs) : value,
130
+ label
131
+ }];
132
+ }
133
+ function execute$1({
134
+ logger
135
+ }, {
136
+ value,
137
+ label
138
+ }) {
139
+ if (label) {
140
+ logger(label, value);
141
+ } else {
142
+ logger(value);
143
+ }
144
+ }
145
+ /**
146
+ *
147
+ * @param expr The expression function to evaluate which will be logged.
148
+ * Takes in 2 arguments:
149
+ * - `ctx` - the current state context
150
+ * - `event` - the event that caused this action to be executed.
151
+ * @param label The label to give to the logged expression.
152
+ */
153
+ function log(value = ({
154
+ context,
155
+ event
156
+ }) => ({
157
+ context,
158
+ event
159
+ }), label) {
160
+ function log(_) {
161
+ }
162
+ log.type = 'xstate.log';
163
+ log.value = value;
164
+ log.label = label;
165
+ log.resolve = resolve$2;
166
+ log.execute = execute$1;
167
+ return log;
168
+ }
169
+
170
+ function resolve$1(_, state, args, {
171
+ get
172
+ }) {
173
+ return [state, undefined, toArray(get({
174
+ context: args.context,
175
+ event: args.event
176
+ }))];
177
+ }
178
+ function pure(getActions) {
179
+ function pure(_) {
180
+ }
181
+ pure.type = 'xstate.pure';
182
+ pure.get = getActions;
183
+ pure.resolve = resolve$1;
184
+ return pure;
185
+ }
186
+
187
+ /**
188
+ * `T | unknown` reduces to `unknown` and that can be problematic when it comes to contextual typing.
189
+ * It especially is a problem when the union has a function member, like here:
190
+ *
191
+ * ```ts
192
+ * declare function test(cbOrVal: ((arg: number) => unknown) | unknown): void;
193
+ * test((arg) => {}) // oops, implicit any
194
+ * ```
195
+ *
196
+ * This type can be used to avoid this problem. This union represents the same value space as `unknown`.
197
+ */
198
+
199
+ // https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
200
+
201
+ /**
202
+ * The full definition of an event, with a string `type`.
203
+ */
204
+
205
+ /**
206
+ * The string or object representing the state value relative to the parent state node.
207
+ *
208
+ * - For a child atomic state node, this is a string, e.g., `"pending"`.
209
+ * - For complex state nodes, this is an object, e.g., `{ success: "someChildState" }`.
210
+ */
211
+
212
+ // TODO: remove once TS fixes this type-widening issue
213
+
214
+ // TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
215
+
216
+ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
217
+ SpecialTargets["Parent"] = "#_parent";
218
+ SpecialTargets["Internal"] = "#_internal";
219
+ return SpecialTargets;
220
+ }({});
221
+
222
+ function resolve(actorContext, state, args, {
223
+ to,
224
+ event: eventOrExpr,
225
+ id,
226
+ delay
227
+ }) {
228
+ const delaysMap = state.machine.implementations.delays;
229
+ if (typeof eventOrExpr === 'string') {
230
+ throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
231
+ }
232
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args) : eventOrExpr;
233
+ let resolvedDelay;
234
+ if (typeof delay === 'string') {
235
+ const configDelay = delaysMap && delaysMap[delay];
236
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args) : configDelay;
237
+ } else {
238
+ resolvedDelay = typeof delay === 'function' ? delay(args) : delay;
239
+ }
240
+ const resolvedTarget = typeof to === 'function' ? to(args) : to;
241
+ let targetActorRef;
242
+ if (typeof resolvedTarget === 'string') {
243
+ if (resolvedTarget === SpecialTargets.Parent) {
244
+ targetActorRef = actorContext?.self._parent;
245
+ } else if (resolvedTarget === SpecialTargets.Internal) {
246
+ targetActorRef = actorContext?.self;
247
+ } else if (resolvedTarget.startsWith('#_')) {
248
+ // SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
249
+ // #_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.
250
+ targetActorRef = state.children[resolvedTarget.slice(2)];
251
+ } else {
252
+ targetActorRef = state.children[resolvedTarget];
253
+ }
254
+ if (!targetActorRef) {
255
+ throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${state.machine.id}'.`);
256
+ }
257
+ } else {
258
+ targetActorRef = resolvedTarget || actorContext?.self;
259
+ }
260
+ return [state, {
261
+ to: targetActorRef,
262
+ event: resolvedEvent,
263
+ id,
264
+ delay: resolvedDelay
265
+ }];
266
+ }
267
+ function execute(actorContext, params) {
268
+ if (typeof params.delay === 'number') {
269
+ actorContext.self.delaySend(params);
270
+ return;
271
+ }
272
+ const {
273
+ to,
274
+ event
275
+ } = params;
276
+ actorContext.defer(() => {
277
+ to.send(event.type === XSTATE_ERROR ? createErrorActorEvent(actorContext.self.id, event.data) : event);
278
+ });
279
+ }
280
+ /**
281
+ * Sends an event to an actor.
282
+ *
283
+ * @param actor The `ActorRef` to send the event to.
284
+ * @param event The event to send, or an expression that evaluates to the event to send
285
+ * @param options Send action options
286
+ * - `id` - The unique send event identifier (used with `cancel()`).
287
+ * - `delay` - The number of milliseconds to delay the sending of the event.
288
+ */
289
+ function sendTo(to, eventOrExpr, options) {
290
+ function sendTo(_) {
291
+ }
292
+ sendTo.type = 'xstate.sendTo';
293
+ sendTo.to = to;
294
+ sendTo.event = eventOrExpr;
295
+ sendTo.id = options?.id;
296
+ sendTo.delay = options?.delay;
297
+ sendTo.resolve = resolve;
298
+ sendTo.execute = execute;
299
+ return sendTo;
300
+ }
301
+
302
+ /**
303
+ * Sends an event to this machine's parent.
304
+ *
305
+ * @param event The event to send to the parent machine.
306
+ * @param options Options to pass into the send event.
307
+ */
308
+ function sendParent(event, options) {
309
+ return sendTo(SpecialTargets.Parent, event, options);
310
+ }
311
+ /**
312
+ * Forwards (sends) an event to a specified service.
313
+ *
314
+ * @param target The target service to forward the event to.
315
+ * @param options Options to pass into the send action creator.
316
+ */
317
+ function forwardTo(target, options) {
318
+ return sendTo(target, ({
319
+ event
320
+ }) => event, options);
321
+ }
322
+
323
+ /**
324
+ * Escalates an error by sending it as an event to this machine's parent.
325
+ *
326
+ * @param errorData The error data to send, or the expression function that
327
+ * takes in the `context`, `event`, and `meta`, and returns the error data to send.
328
+ * @param options Options to pass into the send action creator.
329
+ */
330
+ function escalate(errorData, options) {
331
+ return sendParent(arg => {
332
+ return {
333
+ type: XSTATE_ERROR,
334
+ data: typeof errorData === 'function' ? errorData(arg) : errorData
335
+ };
336
+ }, options);
337
+ }
338
+
339
+ 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 };