1 | 'use strict';
|
2 |
|
3 | var guards_dist_xstateGuards = require('./raise-40b1a1f5.cjs.js');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | function createSpawner(actorScope, {
|
9 | machine,
|
10 | context
|
11 | }, event, spawnedChildren) {
|
12 | const spawn = (src, options = {}) => {
|
13 | const {
|
14 | systemId,
|
15 | input
|
16 | } = options;
|
17 | if (typeof src === 'string') {
|
18 | const logic = guards_dist_xstateGuards.resolveReferencedActor(machine, src);
|
19 | if (!logic) {
|
20 | throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
|
21 | }
|
22 | const actorRef = guards_dist_xstateGuards.createActor(logic, {
|
23 | id: options.id,
|
24 | parent: actorScope.self,
|
25 | syncSnapshot: options.syncSnapshot,
|
26 | input: typeof input === 'function' ? input({
|
27 | context,
|
28 | event,
|
29 | self: actorScope.self
|
30 | }) : input,
|
31 | src,
|
32 | systemId
|
33 | });
|
34 | spawnedChildren[actorRef.id] = actorRef;
|
35 | return actorRef;
|
36 | } else {
|
37 | const actorRef = guards_dist_xstateGuards.createActor(src, {
|
38 | id: options.id,
|
39 | parent: actorScope.self,
|
40 | syncSnapshot: options.syncSnapshot,
|
41 | input: options.input,
|
42 | src,
|
43 | systemId
|
44 | });
|
45 | return actorRef;
|
46 | }
|
47 | };
|
48 | return (src, options) => {
|
49 | const actorRef = spawn(src, options);
|
50 | spawnedChildren[actorRef.id] = actorRef;
|
51 | actorScope.defer(() => {
|
52 | if (actorRef._processingStatus === guards_dist_xstateGuards.ProcessingStatus.Stopped) {
|
53 | return;
|
54 | }
|
55 | actorRef.start();
|
56 | });
|
57 | return actorRef;
|
58 | };
|
59 | }
|
60 |
|
61 | function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
|
62 | assignment
|
63 | }) {
|
64 | if (!snapshot.context) {
|
65 | throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
|
66 | }
|
67 | const spawnedChildren = {};
|
68 | const assignArgs = {
|
69 | context: snapshot.context,
|
70 | event: actionArgs.event,
|
71 | spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
|
72 | self: actorScope.self,
|
73 | system: actorScope.system
|
74 | };
|
75 | let partialUpdate = {};
|
76 | if (typeof assignment === 'function') {
|
77 | partialUpdate = assignment(assignArgs, actionParams);
|
78 | } else {
|
79 | for (const key of Object.keys(assignment)) {
|
80 | const propAssignment = assignment[key];
|
81 | partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs, actionParams) : propAssignment;
|
82 | }
|
83 | }
|
84 | const updatedContext = Object.assign({}, snapshot.context, partialUpdate);
|
85 | return [guards_dist_xstateGuards.cloneMachineSnapshot(snapshot, {
|
86 | context: updatedContext,
|
87 | children: Object.keys(spawnedChildren).length ? {
|
88 | ...snapshot.children,
|
89 | ...spawnedChildren
|
90 | } : snapshot.children
|
91 | })];
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function assign(assignment) {
|
126 | function assign(args, params) {
|
127 | }
|
128 | assign.type = 'xstate.assign';
|
129 | assign.assignment = assignment;
|
130 | assign.resolve = resolveAssign;
|
131 | return assign;
|
132 | }
|
133 |
|
134 | function resolveEmit(_, snapshot, args, actionParams, {
|
135 | event: eventOrExpr
|
136 | }) {
|
137 | const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
|
138 | return [snapshot, {
|
139 | event: resolvedEvent
|
140 | }];
|
141 | }
|
142 | function executeEmit(actorScope, {
|
143 | event
|
144 | }) {
|
145 | actorScope.defer(() => actorScope.emit(event));
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | function emit(
|
182 | /**
|
183 | * The event to emit, or an expression that returns an event to emit.
|
184 | */
|
185 | eventOrExpr) {
|
186 | function emit(args, params) {
|
187 | }
|
188 | emit.type = 'xstate.emit';
|
189 | emit.event = eventOrExpr;
|
190 | emit.resolve = resolveEmit;
|
191 | emit.execute = executeEmit;
|
192 | return emit;
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | let SpecialTargets = function (SpecialTargets) {
|
236 | SpecialTargets["Parent"] = "#_parent";
|
237 | SpecialTargets["Internal"] = "#_internal";
|
238 | return SpecialTargets;
|
239 | }({});
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | function resolveSendTo(actorScope, snapshot, args, actionParams, {
|
263 | to,
|
264 | event: eventOrExpr,
|
265 | id,
|
266 | delay
|
267 | }, extra) {
|
268 | const delaysMap = snapshot.machine.implementations.delays;
|
269 | if (typeof eventOrExpr === 'string') {
|
270 | throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
|
271 | }
|
272 | const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
|
273 | let resolvedDelay;
|
274 | if (typeof delay === 'string') {
|
275 | const configDelay = delaysMap && delaysMap[delay];
|
276 | resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
|
277 | } else {
|
278 | resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
|
279 | }
|
280 | const resolvedTarget = typeof to === 'function' ? to(args, actionParams) : to;
|
281 | let targetActorRef;
|
282 | if (typeof resolvedTarget === 'string') {
|
283 | if (resolvedTarget === SpecialTargets.Parent) {
|
284 | targetActorRef = actorScope.self._parent;
|
285 | } else if (resolvedTarget === SpecialTargets.Internal) {
|
286 | targetActorRef = actorScope.self;
|
287 | } else if (resolvedTarget.startsWith('#_')) {
|
288 |
|
289 |
|
290 | targetActorRef = snapshot.children[resolvedTarget.slice(2)];
|
291 | } else {
|
292 | targetActorRef = extra.deferredActorIds?.includes(resolvedTarget) ? resolvedTarget : snapshot.children[resolvedTarget];
|
293 | }
|
294 | if (!targetActorRef) {
|
295 | throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${snapshot.machine.id}'.`);
|
296 | }
|
297 | } else {
|
298 | targetActorRef = resolvedTarget || actorScope.self;
|
299 | }
|
300 | return [snapshot, {
|
301 | to: targetActorRef,
|
302 | event: resolvedEvent,
|
303 | id,
|
304 | delay: resolvedDelay
|
305 | }];
|
306 | }
|
307 | function retryResolveSendTo(_, snapshot, params) {
|
308 | if (typeof params.to === 'string') {
|
309 | params.to = snapshot.children[params.to];
|
310 | }
|
311 | }
|
312 | function executeSendTo(actorScope, params) {
|
313 |
|
314 |
|
315 | actorScope.defer(() => {
|
316 | const {
|
317 | to,
|
318 | event,
|
319 | delay,
|
320 | id
|
321 | } = params;
|
322 | if (typeof delay === 'number') {
|
323 | actorScope.system.scheduler.schedule(actorScope.self, to, event, delay, id);
|
324 | return;
|
325 | }
|
326 | actorScope.system._relay(actorScope.self,
|
327 |
|
328 |
|
329 | to, event.type === guards_dist_xstateGuards.XSTATE_ERROR ? guards_dist_xstateGuards.createErrorActorEvent(actorScope.self.id, event.data) : event);
|
330 | });
|
331 | }
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | function sendTo(to, eventOrExpr, options) {
|
342 | function sendTo(args, params) {
|
343 | }
|
344 | sendTo.type = 'xsnapshot.sendTo';
|
345 | sendTo.to = to;
|
346 | sendTo.event = eventOrExpr;
|
347 | sendTo.id = options?.id;
|
348 | sendTo.delay = options?.delay;
|
349 | sendTo.resolve = resolveSendTo;
|
350 | sendTo.retryResolve = retryResolveSendTo;
|
351 | sendTo.execute = executeSendTo;
|
352 | return sendTo;
|
353 | }
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | function sendParent(event, options) {
|
362 | return sendTo(SpecialTargets.Parent, event, options);
|
363 | }
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 | function forwardTo(target, options) {
|
371 | return sendTo(target, ({
|
372 | event
|
373 | }) => event, options);
|
374 | }
|
375 |
|
376 | function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
|
377 | collect
|
378 | }) {
|
379 | const actions = [];
|
380 | const enqueue = function enqueue(action) {
|
381 | actions.push(action);
|
382 | };
|
383 | enqueue.assign = (...args) => {
|
384 | actions.push(assign(...args));
|
385 | };
|
386 | enqueue.cancel = (...args) => {
|
387 | actions.push(guards_dist_xstateGuards.cancel(...args));
|
388 | };
|
389 | enqueue.raise = (...args) => {
|
390 |
|
391 |
|
392 | actions.push(guards_dist_xstateGuards.raise(...args));
|
393 | };
|
394 | enqueue.sendTo = (...args) => {
|
395 |
|
396 |
|
397 | actions.push(sendTo(...args));
|
398 | };
|
399 | enqueue.spawnChild = (...args) => {
|
400 | actions.push(guards_dist_xstateGuards.spawnChild(...args));
|
401 | };
|
402 | enqueue.stopChild = (...args) => {
|
403 | actions.push(guards_dist_xstateGuards.stopChild(...args));
|
404 | };
|
405 | enqueue.emit = (...args) => {
|
406 | actions.push(emit(...args));
|
407 | };
|
408 | collect({
|
409 | context: args.context,
|
410 | event: args.event,
|
411 | enqueue,
|
412 | check: guard => guards_dist_xstateGuards.evaluateGuard(guard, snapshot.context, args.event, snapshot),
|
413 | self: actorScope.self,
|
414 | system: actorScope.system
|
415 | }, actionParams);
|
416 | return [snapshot, undefined, actions];
|
417 | }
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 | function enqueueActions(collect) {
|
439 | function enqueueActions(args, params) {
|
440 | }
|
441 | enqueueActions.type = 'xstate.enqueueActions';
|
442 | enqueueActions.collect = collect;
|
443 | enqueueActions.resolve = resolveEnqueueActions;
|
444 | return enqueueActions;
|
445 | }
|
446 |
|
447 | function resolveLog(_, snapshot, actionArgs, actionParams, {
|
448 | value,
|
449 | label
|
450 | }) {
|
451 | return [snapshot, {
|
452 | value: typeof value === 'function' ? value(actionArgs, actionParams) : value,
|
453 | label
|
454 | }];
|
455 | }
|
456 | function executeLog({
|
457 | logger
|
458 | }, {
|
459 | value,
|
460 | label
|
461 | }) {
|
462 | if (label) {
|
463 | logger(label, value);
|
464 | } else {
|
465 | logger(value);
|
466 | }
|
467 | }
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | function log(value = ({
|
477 | context,
|
478 | event
|
479 | }) => ({
|
480 | context,
|
481 | event
|
482 | }), label) {
|
483 | function log(args, params) {
|
484 | }
|
485 | log.type = 'xstate.log';
|
486 | log.value = value;
|
487 | log.label = label;
|
488 | log.resolve = resolveLog;
|
489 | log.execute = executeLog;
|
490 | return log;
|
491 | }
|
492 |
|
493 | exports.SpecialTargets = SpecialTargets;
|
494 | exports.assign = assign;
|
495 | exports.emit = emit;
|
496 | exports.enqueueActions = enqueueActions;
|
497 | exports.forwardTo = forwardTo;
|
498 | exports.log = log;
|
499 | exports.sendParent = sendParent;
|
500 | exports.sendTo = sendTo;
|