1 | import { __values, __spreadArray, __read, __assign } from './_virtual/_tslib.js';
|
2 | import { SpecialTargets, ActionTypes } from './types.js';
|
3 | import { isStateConfig, State, bindActionToState } from './State.js';
|
4 | import { errorPlatform, update, error as error$1, log, stop, start, cancel, send, raise } from './actionTypes.js';
|
5 | import { initEvent, doneInvoke, toActionObjects, resolveActions, error, getActionFunction } from './actions.js';
|
6 | import { IS_PRODUCTION } from './environment.js';
|
7 | import { warn, mapContext, toObserver, isFunction, toSCXMLEvent, flatten, isRaisableAction, isPromiseLike, isObservable, isMachine, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, toInvokeSource, uniqueId } from './utils.js';
|
8 | import { Scheduler } from './scheduler.js';
|
9 | import { createDeferredActor, isSpawnedActor } from './Actor.js';
|
10 | import { registry } from './registry.js';
|
11 | import { getGlobal, registerService } from './devTools.js';
|
12 | import { provide, consume } from './serviceScope.js';
|
13 | import { spawnBehavior } from './behaviors.js';
|
14 |
|
15 | var DEFAULT_SPAWN_OPTIONS = {
|
16 | sync: false,
|
17 | autoForward: false
|
18 | };
|
19 | var InterpreterStatus;
|
20 |
|
21 | (function (InterpreterStatus) {
|
22 | InterpreterStatus[InterpreterStatus["NotStarted"] = 0] = "NotStarted";
|
23 | InterpreterStatus[InterpreterStatus["Running"] = 1] = "Running";
|
24 | InterpreterStatus[InterpreterStatus["Stopped"] = 2] = "Stopped";
|
25 | })(InterpreterStatus || (InterpreterStatus = {}));
|
26 |
|
27 | var Interpreter =
|
28 |
|
29 |
|
30 |
|
31 | function () {
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function Interpreter(machine, options) {
|
39 | if (options === void 0) {
|
40 | options = Interpreter.defaultOptions;
|
41 | }
|
42 |
|
43 | var _this = this;
|
44 |
|
45 | this.machine = machine;
|
46 | this.delayedEventsMap = {};
|
47 | this.listeners = new Set();
|
48 | this.contextListeners = new Set();
|
49 | this.stopListeners = new Set();
|
50 | this.doneListeners = new Set();
|
51 | this.eventListeners = new Set();
|
52 | this.sendListeners = new Set();
|
53 | |
54 |
|
55 |
|
56 |
|
57 | this.initialized = false;
|
58 | this.status = InterpreterStatus.NotStarted;
|
59 | this.children = new Map();
|
60 | this.forwardTo = new Set();
|
61 | this._outgoingQueue = [];
|
62 | |
63 |
|
64 |
|
65 |
|
66 | this.init = this.start;
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | this.send = function (event, payload) {
|
78 | if (isArray(event)) {
|
79 | _this.batch(event);
|
80 |
|
81 | return _this.state;
|
82 | }
|
83 |
|
84 | var _event = toSCXMLEvent(toEventObject(event, payload));
|
85 |
|
86 | if (_this.status === InterpreterStatus.Stopped) {
|
87 |
|
88 | if (!IS_PRODUCTION) {
|
89 | warn(false, "Event \"".concat(_event.name, "\" was sent to stopped service \"").concat(_this.machine.id, "\". This service has already reached its final state, and will not transition.\nEvent: ").concat(JSON.stringify(_event.data)));
|
90 | }
|
91 |
|
92 | return _this.state;
|
93 | }
|
94 |
|
95 | if (_this.status !== InterpreterStatus.Running && !_this.options.deferEvents) {
|
96 | throw new Error("Event \"".concat(_event.name, "\" was sent to uninitialized service \"").concat(_this.machine.id
|
97 | , "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: ").concat(JSON.stringify(_event.data)));
|
98 | }
|
99 |
|
100 | _this.scheduler.schedule(function () {
|
101 |
|
102 | _this.forward(_event);
|
103 |
|
104 | var nextState = _this._nextState(_event);
|
105 |
|
106 | _this.update(nextState, _event);
|
107 | });
|
108 |
|
109 | return _this._state;
|
110 |
|
111 | };
|
112 |
|
113 | this.sendTo = function (event, to, immediate) {
|
114 | var isParent = _this.parent && (to === SpecialTargets.Parent || _this.parent.id === to);
|
115 | var target = isParent ? _this.parent : isString(to) ? to === SpecialTargets.Internal ? _this : _this.children.get(to) || registry.get(to) : isActor(to) ? to : undefined;
|
116 |
|
117 | if (!target) {
|
118 | if (!isParent) {
|
119 | throw new Error("Unable to send event to child '".concat(to, "' from service '").concat(_this.id, "'."));
|
120 | }
|
121 |
|
122 |
|
123 | if (!IS_PRODUCTION) {
|
124 | warn(false, "Service '".concat(_this.id, "' has no parent: unable to send event ").concat(event.type));
|
125 | }
|
126 |
|
127 | return;
|
128 | }
|
129 |
|
130 | if ('machine' in target) {
|
131 |
|
132 |
|
133 | if (_this.status !== InterpreterStatus.Stopped || _this.parent !== target ||
|
134 | _this.state.done) {
|
135 |
|
136 | var scxmlEvent = __assign(__assign({}, event), {
|
137 | name: event.name === error$1 ? "".concat(error(_this.id)) : event.name,
|
138 | origin: _this.sessionId
|
139 | });
|
140 |
|
141 | if (!immediate && _this.machine.config.predictableActionArguments) {
|
142 | _this._outgoingQueue.push([target, scxmlEvent]);
|
143 | } else {
|
144 | target.send(scxmlEvent);
|
145 | }
|
146 | }
|
147 | } else {
|
148 |
|
149 | if (!immediate && _this.machine.config.predictableActionArguments) {
|
150 | _this._outgoingQueue.push([target, event.data]);
|
151 | } else {
|
152 | target.send(event.data);
|
153 | }
|
154 | }
|
155 | };
|
156 |
|
157 | this._exec = function (action, context, _event, actionFunctionMap) {
|
158 | if (actionFunctionMap === void 0) {
|
159 | actionFunctionMap = _this.machine.options.actions;
|
160 | }
|
161 |
|
162 | var actionOrExec = action.exec || getActionFunction(action.type, actionFunctionMap);
|
163 | var exec = isFunction(actionOrExec) ? actionOrExec : actionOrExec ? actionOrExec.exec : action.exec;
|
164 |
|
165 | if (exec) {
|
166 | try {
|
167 | return exec(context, _event.data, !_this.machine.config.predictableActionArguments ? {
|
168 | action: action,
|
169 | state: _this.state,
|
170 | _event: _event
|
171 | } : {
|
172 | action: action,
|
173 | _event: _event
|
174 | });
|
175 | } catch (err) {
|
176 | if (_this.parent) {
|
177 | _this.parent.send({
|
178 | type: 'xstate.error',
|
179 | data: err
|
180 | });
|
181 | }
|
182 |
|
183 | throw err;
|
184 | }
|
185 | }
|
186 |
|
187 | switch (action.type) {
|
188 | case raise:
|
189 | {
|
190 |
|
191 | var sendAction_1 = action;
|
192 |
|
193 | _this.defer(sendAction_1);
|
194 |
|
195 | break;
|
196 | }
|
197 |
|
198 | case send:
|
199 | var sendAction = action;
|
200 |
|
201 | if (typeof sendAction.delay === 'number') {
|
202 | _this.defer(sendAction);
|
203 |
|
204 | return;
|
205 | } else {
|
206 | if (sendAction.to) {
|
207 | _this.sendTo(sendAction._event, sendAction.to, _event === initEvent);
|
208 | } else {
|
209 | _this.send(sendAction._event);
|
210 | }
|
211 | }
|
212 |
|
213 | break;
|
214 |
|
215 | case cancel:
|
216 | _this.cancel(action.sendId);
|
217 |
|
218 | break;
|
219 |
|
220 | case start:
|
221 | {
|
222 | if (_this.status !== InterpreterStatus.Running) {
|
223 | return;
|
224 | }
|
225 |
|
226 | var activity = action.activity;
|
227 |
|
228 |
|
229 |
|
230 | if (
|
231 | !_this.machine.config.predictableActionArguments && !_this.state.activities[activity.id || activity.type]) {
|
232 | break;
|
233 | }
|
234 |
|
235 |
|
236 | if (activity.type === ActionTypes.Invoke) {
|
237 | var invokeSource = toInvokeSource(activity.src);
|
238 | var serviceCreator = _this.machine.options.services ? _this.machine.options.services[invokeSource.type] : undefined;
|
239 | var id = activity.id,
|
240 | data = activity.data;
|
241 |
|
242 | if (!IS_PRODUCTION) {
|
243 | warn(!('forward' in activity),
|
244 | "`forward` property is deprecated (found in invocation of '".concat(activity.src, "' in in machine '").concat(_this.machine.id, "'). ") + "Please use `autoForward` instead.");
|
245 | }
|
246 |
|
247 | var autoForward = 'autoForward' in activity ? activity.autoForward : !!activity.forward;
|
248 |
|
249 | if (!serviceCreator) {
|
250 |
|
251 | if (!IS_PRODUCTION) {
|
252 | warn(false, "No service found for invocation '".concat(activity.src, "' in machine '").concat(_this.machine.id, "'."));
|
253 | }
|
254 |
|
255 | return;
|
256 | }
|
257 |
|
258 | var resolvedData = data ? mapContext(data, context, _event) : undefined;
|
259 |
|
260 | if (typeof serviceCreator === 'string') {
|
261 |
|
262 | return;
|
263 | }
|
264 |
|
265 | var source = isFunction(serviceCreator) ? serviceCreator(context, _event.data, {
|
266 | data: resolvedData,
|
267 | src: invokeSource,
|
268 | meta: activity.meta
|
269 | }) : serviceCreator;
|
270 |
|
271 | if (!source) {
|
272 |
|
273 | return;
|
274 | }
|
275 |
|
276 | var options = void 0;
|
277 |
|
278 | if (isMachine(source)) {
|
279 | source = resolvedData ? source.withContext(resolvedData) : source;
|
280 | options = {
|
281 | autoForward: autoForward
|
282 | };
|
283 | }
|
284 |
|
285 | _this.spawn(source, id, options);
|
286 | } else {
|
287 | _this.spawnActivity(activity);
|
288 | }
|
289 |
|
290 | break;
|
291 | }
|
292 |
|
293 | case stop:
|
294 | {
|
295 | _this.stopChild(action.activity.id);
|
296 |
|
297 | break;
|
298 | }
|
299 |
|
300 | case log:
|
301 | var _a = action,
|
302 | label = _a.label,
|
303 | value = _a.value;
|
304 |
|
305 | if (label) {
|
306 | _this.logger(label, value);
|
307 | } else {
|
308 | _this.logger(value);
|
309 | }
|
310 |
|
311 | break;
|
312 |
|
313 | default:
|
314 | if (!IS_PRODUCTION) {
|
315 | warn(false, "No implementation found for action type '".concat(action.type, "'"));
|
316 | }
|
317 |
|
318 | break;
|
319 | }
|
320 | };
|
321 |
|
322 | var resolvedOptions = __assign(__assign({}, Interpreter.defaultOptions), options);
|
323 |
|
324 | var clock = resolvedOptions.clock,
|
325 | logger = resolvedOptions.logger,
|
326 | parent = resolvedOptions.parent,
|
327 | id = resolvedOptions.id;
|
328 | var resolvedId = id !== undefined ? id : machine.id;
|
329 | this.id = resolvedId;
|
330 | this.logger = logger;
|
331 | this.clock = clock;
|
332 | this.parent = parent;
|
333 | this.options = resolvedOptions;
|
334 | this.scheduler = new Scheduler({
|
335 | deferEvents: this.options.deferEvents
|
336 | });
|
337 | this.sessionId = registry.bookId();
|
338 | }
|
339 |
|
340 | Object.defineProperty(Interpreter.prototype, "initialState", {
|
341 | get: function () {
|
342 | var _this = this;
|
343 |
|
344 | if (this._initialState) {
|
345 | return this._initialState;
|
346 | }
|
347 |
|
348 | return provide(this, function () {
|
349 | _this._initialState = _this.machine.initialState;
|
350 | return _this._initialState;
|
351 | });
|
352 | },
|
353 | enumerable: false,
|
354 | configurable: true
|
355 | });
|
356 | Object.defineProperty(Interpreter.prototype, "state", {
|
357 | |
358 |
|
359 |
|
360 | get: function () {
|
361 | if (!IS_PRODUCTION) {
|
362 | warn(this.status !== InterpreterStatus.NotStarted, "Attempted to read state from uninitialized service '".concat(this.id, "'. Make sure the service is started first."));
|
363 | }
|
364 |
|
365 | return this._state;
|
366 | },
|
367 | enumerable: false,
|
368 | configurable: true
|
369 | });
|
370 | |
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | Interpreter.prototype.execute = function (state, actionsConfig) {
|
378 | var e_1, _a;
|
379 |
|
380 | try {
|
381 | for (var _b = __values(state.actions), _c = _b.next(); !_c.done; _c = _b.next()) {
|
382 | var action = _c.value;
|
383 | this.exec(action, state, actionsConfig);
|
384 | }
|
385 | } catch (e_1_1) {
|
386 | e_1 = {
|
387 | error: e_1_1
|
388 | };
|
389 | } finally {
|
390 | try {
|
391 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
392 | } finally {
|
393 | if (e_1) throw e_1.error;
|
394 | }
|
395 | }
|
396 | };
|
397 |
|
398 | Interpreter.prototype.update = function (state, _event) {
|
399 | var e_2, _a, e_3, _b, e_4, _c, e_5, _d;
|
400 |
|
401 | var _this = this;
|
402 |
|
403 |
|
404 | state._sessionid = this.sessionId;
|
405 |
|
406 | this._state = state;
|
407 |
|
408 | if ((!this.machine.config.predictableActionArguments ||
|
409 |
|
410 | _event === initEvent) && this.options.execute) {
|
411 | this.execute(this.state);
|
412 | } else {
|
413 | var item = void 0;
|
414 |
|
415 | while (item = this._outgoingQueue.shift()) {
|
416 | item[0].send(item[1]);
|
417 | }
|
418 | }
|
419 |
|
420 |
|
421 | this.children.forEach(function (child) {
|
422 | _this.state.children[child.id] = child;
|
423 | });
|
424 |
|
425 | if (this.devTools) {
|
426 | this.devTools.send(_event.data, state);
|
427 | }
|
428 |
|
429 |
|
430 | if (state.event) {
|
431 | try {
|
432 | for (var _e = __values(this.eventListeners), _f = _e.next(); !_f.done; _f = _e.next()) {
|
433 | var listener = _f.value;
|
434 | listener(state.event);
|
435 | }
|
436 | } catch (e_2_1) {
|
437 | e_2 = {
|
438 | error: e_2_1
|
439 | };
|
440 | } finally {
|
441 | try {
|
442 | if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
|
443 | } finally {
|
444 | if (e_2) throw e_2.error;
|
445 | }
|
446 | }
|
447 | }
|
448 |
|
449 | try {
|
450 | for (var _g = __values(this.listeners), _h = _g.next(); !_h.done; _h = _g.next()) {
|
451 | var listener = _h.value;
|
452 | listener(state, state.event);
|
453 | }
|
454 | } catch (e_3_1) {
|
455 | e_3 = {
|
456 | error: e_3_1
|
457 | };
|
458 | } finally {
|
459 | try {
|
460 | if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
461 | } finally {
|
462 | if (e_3) throw e_3.error;
|
463 | }
|
464 | }
|
465 |
|
466 | try {
|
467 | for (var _j = __values(this.contextListeners), _k = _j.next(); !_k.done; _k = _j.next()) {
|
468 | var contextListener = _k.value;
|
469 | contextListener(this.state.context, this.state.history ? this.state.history.context : undefined);
|
470 | }
|
471 | } catch (e_4_1) {
|
472 | e_4 = {
|
473 | error: e_4_1
|
474 | };
|
475 | } finally {
|
476 | try {
|
477 | if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
|
478 | } finally {
|
479 | if (e_4) throw e_4.error;
|
480 | }
|
481 | }
|
482 |
|
483 | if (this.state.done) {
|
484 |
|
485 | var finalChildStateNode = state.configuration.find(function (sn) {
|
486 | return sn.type === 'final' && sn.parent === _this.machine;
|
487 | });
|
488 | var doneData = finalChildStateNode && finalChildStateNode.doneData ? mapContext(finalChildStateNode.doneData, state.context, _event) : undefined;
|
489 | this._doneEvent = doneInvoke(this.id, doneData);
|
490 |
|
491 | try {
|
492 | for (var _l = __values(this.doneListeners), _m = _l.next(); !_m.done; _m = _l.next()) {
|
493 | var listener = _m.value;
|
494 | listener(this._doneEvent);
|
495 | }
|
496 | } catch (e_5_1) {
|
497 | e_5 = {
|
498 | error: e_5_1
|
499 | };
|
500 | } finally {
|
501 | try {
|
502 | if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
|
503 | } finally {
|
504 | if (e_5) throw e_5.error;
|
505 | }
|
506 | }
|
507 |
|
508 | this._stop();
|
509 |
|
510 | this._stopChildren();
|
511 |
|
512 | registry.free(this.sessionId);
|
513 | }
|
514 | };
|
515 | |
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 | Interpreter.prototype.onTransition = function (listener) {
|
524 | this.listeners.add(listener);
|
525 |
|
526 | if (this.status === InterpreterStatus.Running) {
|
527 | listener(this.state, this.state.event);
|
528 | }
|
529 |
|
530 | return this;
|
531 | };
|
532 |
|
533 | Interpreter.prototype.subscribe = function (nextListenerOrObserver, _, // TODO: error listener
|
534 | completeListener) {
|
535 | var _this = this;
|
536 |
|
537 | var observer = toObserver(nextListenerOrObserver, _, completeListener);
|
538 | this.listeners.add(observer.next);
|
539 |
|
540 | if (this.status !== InterpreterStatus.NotStarted) {
|
541 | observer.next(this.state);
|
542 | }
|
543 |
|
544 | var completeOnce = function () {
|
545 | _this.doneListeners.delete(completeOnce);
|
546 |
|
547 | _this.stopListeners.delete(completeOnce);
|
548 |
|
549 | observer.complete();
|
550 | };
|
551 |
|
552 | if (this.status === InterpreterStatus.Stopped) {
|
553 | observer.complete();
|
554 | } else {
|
555 | this.onDone(completeOnce);
|
556 | this.onStop(completeOnce);
|
557 | }
|
558 |
|
559 | return {
|
560 | unsubscribe: function () {
|
561 | _this.listeners.delete(observer.next);
|
562 |
|
563 | _this.doneListeners.delete(completeOnce);
|
564 |
|
565 | _this.stopListeners.delete(completeOnce);
|
566 | }
|
567 | };
|
568 | };
|
569 | |
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 | Interpreter.prototype.onEvent = function (listener) {
|
576 | this.eventListeners.add(listener);
|
577 | return this;
|
578 | };
|
579 | |
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 | Interpreter.prototype.onSend = function (listener) {
|
586 | this.sendListeners.add(listener);
|
587 | return this;
|
588 | };
|
589 | |
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 | Interpreter.prototype.onChange = function (listener) {
|
596 | this.contextListeners.add(listener);
|
597 | return this;
|
598 | };
|
599 | |
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 | Interpreter.prototype.onStop = function (listener) {
|
606 | this.stopListeners.add(listener);
|
607 | return this;
|
608 | };
|
609 | |
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 | Interpreter.prototype.onDone = function (listener) {
|
616 | if (this.status === InterpreterStatus.Stopped && this._doneEvent) {
|
617 | listener(this._doneEvent);
|
618 | } else {
|
619 | this.doneListeners.add(listener);
|
620 | }
|
621 |
|
622 | return this;
|
623 | };
|
624 | |
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 | Interpreter.prototype.off = function (listener) {
|
631 | this.listeners.delete(listener);
|
632 | this.eventListeners.delete(listener);
|
633 | this.sendListeners.delete(listener);
|
634 | this.stopListeners.delete(listener);
|
635 | this.doneListeners.delete(listener);
|
636 | this.contextListeners.delete(listener);
|
637 | return this;
|
638 | };
|
639 | |
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 | Interpreter.prototype.start = function (initialState) {
|
646 | var _this = this;
|
647 |
|
648 | if (this.status === InterpreterStatus.Running) {
|
649 |
|
650 | return this;
|
651 | }
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 | this.machine._init();
|
658 |
|
659 | registry.register(this.sessionId, this);
|
660 | this.initialized = true;
|
661 | this.status = InterpreterStatus.Running;
|
662 | var resolvedState = initialState === undefined ? this.initialState : provide(this, function () {
|
663 | return isStateConfig(initialState) ? _this.machine.resolveState(initialState) : _this.machine.resolveState(State.from(initialState, _this.machine.context));
|
664 | });
|
665 |
|
666 | if (this.options.devTools) {
|
667 | this.attachDev();
|
668 | }
|
669 |
|
670 | this.scheduler.initialize(function () {
|
671 | _this.update(resolvedState, initEvent);
|
672 | });
|
673 | return this;
|
674 | };
|
675 |
|
676 | Interpreter.prototype._stopChildren = function () {
|
677 |
|
678 | this.children.forEach(function (child) {
|
679 | if (isFunction(child.stop)) {
|
680 | child.stop();
|
681 | }
|
682 | });
|
683 | this.children.clear();
|
684 | };
|
685 |
|
686 | Interpreter.prototype._stop = function () {
|
687 | var e_6, _a, e_7, _b, e_8, _c, e_9, _d, e_10, _e;
|
688 |
|
689 | try {
|
690 | for (var _f = __values(this.listeners), _g = _f.next(); !_g.done; _g = _f.next()) {
|
691 | var listener = _g.value;
|
692 | this.listeners.delete(listener);
|
693 | }
|
694 | } catch (e_6_1) {
|
695 | e_6 = {
|
696 | error: e_6_1
|
697 | };
|
698 | } finally {
|
699 | try {
|
700 | if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
|
701 | } finally {
|
702 | if (e_6) throw e_6.error;
|
703 | }
|
704 | }
|
705 |
|
706 | try {
|
707 | for (var _h = __values(this.stopListeners), _j = _h.next(); !_j.done; _j = _h.next()) {
|
708 | var listener = _j.value;
|
709 |
|
710 | listener();
|
711 | this.stopListeners.delete(listener);
|
712 | }
|
713 | } catch (e_7_1) {
|
714 | e_7 = {
|
715 | error: e_7_1
|
716 | };
|
717 | } finally {
|
718 | try {
|
719 | if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
|
720 | } finally {
|
721 | if (e_7) throw e_7.error;
|
722 | }
|
723 | }
|
724 |
|
725 | try {
|
726 | for (var _k = __values(this.contextListeners), _l = _k.next(); !_l.done; _l = _k.next()) {
|
727 | var listener = _l.value;
|
728 | this.contextListeners.delete(listener);
|
729 | }
|
730 | } catch (e_8_1) {
|
731 | e_8 = {
|
732 | error: e_8_1
|
733 | };
|
734 | } finally {
|
735 | try {
|
736 | if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
|
737 | } finally {
|
738 | if (e_8) throw e_8.error;
|
739 | }
|
740 | }
|
741 |
|
742 | try {
|
743 | for (var _m = __values(this.doneListeners), _o = _m.next(); !_o.done; _o = _m.next()) {
|
744 | var listener = _o.value;
|
745 | this.doneListeners.delete(listener);
|
746 | }
|
747 | } catch (e_9_1) {
|
748 | e_9 = {
|
749 | error: e_9_1
|
750 | };
|
751 | } finally {
|
752 | try {
|
753 | if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
|
754 | } finally {
|
755 | if (e_9) throw e_9.error;
|
756 | }
|
757 | }
|
758 |
|
759 | if (!this.initialized) {
|
760 |
|
761 | return this;
|
762 | }
|
763 |
|
764 | this.initialized = false;
|
765 | this.status = InterpreterStatus.Stopped;
|
766 | this._initialState = undefined;
|
767 |
|
768 | try {
|
769 |
|
770 |
|
771 | for (var _p = __values(Object.keys(this.delayedEventsMap)), _q = _p.next(); !_q.done; _q = _p.next()) {
|
772 | var key = _q.value;
|
773 | this.clock.clearTimeout(this.delayedEventsMap[key]);
|
774 | }
|
775 | } catch (e_10_1) {
|
776 | e_10 = {
|
777 | error: e_10_1
|
778 | };
|
779 | } finally {
|
780 | try {
|
781 | if (_q && !_q.done && (_e = _p.return)) _e.call(_p);
|
782 | } finally {
|
783 | if (e_10) throw e_10.error;
|
784 | }
|
785 | }
|
786 |
|
787 |
|
788 | this.scheduler.clear();
|
789 | this.scheduler = new Scheduler({
|
790 | deferEvents: this.options.deferEvents
|
791 | });
|
792 | };
|
793 | |
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 | Interpreter.prototype.stop = function () {
|
801 |
|
802 | var _this = this;
|
803 |
|
804 |
|
805 | var scheduler = this.scheduler;
|
806 |
|
807 | this._stop();
|
808 |
|
809 |
|
810 | scheduler.schedule(function () {
|
811 |
|
812 | var _event = toSCXMLEvent({
|
813 | type: 'xstate.stop'
|
814 | });
|
815 |
|
816 | var nextState = provide(_this, function () {
|
817 | var exitActions = flatten(__spreadArray([], __read(_this.state.configuration), false).sort(function (a, b) {
|
818 | return b.order - a.order;
|
819 | }).map(function (stateNode) {
|
820 | return toActionObjects(stateNode.onExit, _this.machine.options.actions);
|
821 | }));
|
822 |
|
823 | var _a = __read(resolveActions(_this.machine, _this.state, _this.state.context, _event, [{
|
824 | type: 'exit',
|
825 | actions: exitActions
|
826 | }], _this.machine.config.predictableActionArguments ? _this._exec : undefined, _this.machine.config.predictableActionArguments || _this.machine.config.preserveActionOrder), 2),
|
827 | resolvedActions = _a[0],
|
828 | updatedContext = _a[1];
|
829 |
|
830 | var newState = new State({
|
831 | value: _this.state.value,
|
832 | context: updatedContext,
|
833 | _event: _event,
|
834 | _sessionid: _this.sessionId,
|
835 | historyValue: undefined,
|
836 | history: _this.state,
|
837 | actions: resolvedActions.filter(function (action) {
|
838 | return !isRaisableAction(action);
|
839 | }),
|
840 | activities: {},
|
841 | events: [],
|
842 | configuration: [],
|
843 | transitions: [],
|
844 | children: {},
|
845 | done: _this.state.done,
|
846 | tags: _this.state.tags,
|
847 | machine: _this.machine
|
848 | });
|
849 | newState.changed = true;
|
850 | return newState;
|
851 | });
|
852 |
|
853 | _this.update(nextState, _event);
|
854 |
|
855 | _this._stopChildren();
|
856 |
|
857 | registry.free(_this.sessionId);
|
858 | });
|
859 | return this;
|
860 | };
|
861 |
|
862 | Interpreter.prototype.batch = function (events) {
|
863 | var _this = this;
|
864 |
|
865 | if (this.status === InterpreterStatus.NotStarted && this.options.deferEvents) {
|
866 |
|
867 | if (!IS_PRODUCTION) {
|
868 | warn(false, "".concat(events.length, " event(s) were sent to uninitialized service \"").concat(this.machine.id, "\" and are deferred. Make sure .start() is called for this service.\nEvent: ").concat(JSON.stringify(event)));
|
869 | }
|
870 | } else if (this.status !== InterpreterStatus.Running) {
|
871 | throw new Error(
|
872 | "".concat(events.length, " event(s) were sent to uninitialized service \"").concat(this.machine.id, "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options."));
|
873 | }
|
874 |
|
875 | if (!events.length) {
|
876 | return;
|
877 | }
|
878 |
|
879 | var exec = !!this.machine.config.predictableActionArguments && this._exec;
|
880 | this.scheduler.schedule(function () {
|
881 | var e_11, _a;
|
882 |
|
883 | var nextState = _this.state;
|
884 | var batchChanged = false;
|
885 | var batchedActions = [];
|
886 |
|
887 | var _loop_1 = function (event_1) {
|
888 | var _event = toSCXMLEvent(event_1);
|
889 |
|
890 | _this.forward(_event);
|
891 |
|
892 | nextState = provide(_this, function () {
|
893 | return _this.machine.transition(nextState, _event, undefined, exec || undefined);
|
894 | });
|
895 | batchedActions.push.apply(batchedActions, __spreadArray([], __read(_this.machine.config.predictableActionArguments ? nextState.actions : nextState.actions.map(function (a) {
|
896 | return bindActionToState(a, nextState);
|
897 | })), false));
|
898 | batchChanged = batchChanged || !!nextState.changed;
|
899 | };
|
900 |
|
901 | try {
|
902 | for (var events_1 = __values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
|
903 | var event_1 = events_1_1.value;
|
904 |
|
905 | _loop_1(event_1);
|
906 | }
|
907 | } catch (e_11_1) {
|
908 | e_11 = {
|
909 | error: e_11_1
|
910 | };
|
911 | } finally {
|
912 | try {
|
913 | if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
|
914 | } finally {
|
915 | if (e_11) throw e_11.error;
|
916 | }
|
917 | }
|
918 |
|
919 | nextState.changed = batchChanged;
|
920 | nextState.actions = batchedActions;
|
921 |
|
922 | _this.update(nextState, toSCXMLEvent(events[events.length - 1]));
|
923 | });
|
924 | };
|
925 | |
926 |
|
927 |
|
928 |
|
929 |
|
930 |
|
931 |
|
932 | Interpreter.prototype.sender = function (event) {
|
933 | return this.send.bind(this, event);
|
934 | };
|
935 |
|
936 | Interpreter.prototype._nextState = function (event, exec) {
|
937 | var _this = this;
|
938 |
|
939 | if (exec === void 0) {
|
940 | exec = !!this.machine.config.predictableActionArguments && this._exec;
|
941 | }
|
942 |
|
943 | var _event = toSCXMLEvent(event);
|
944 |
|
945 | if (_event.name.indexOf(errorPlatform) === 0 && !this.state.nextEvents.some(function (nextEvent) {
|
946 | return nextEvent.indexOf(errorPlatform) === 0;
|
947 | })) {
|
948 | throw _event.data.data;
|
949 | }
|
950 |
|
951 | var nextState = provide(this, function () {
|
952 | return _this.machine.transition(_this.state, _event, undefined, exec || undefined);
|
953 | });
|
954 | return nextState;
|
955 | };
|
956 | |
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 | Interpreter.prototype.nextState = function (event) {
|
966 | return this._nextState(event, false);
|
967 | };
|
968 |
|
969 | Interpreter.prototype.forward = function (event) {
|
970 | var e_12, _a;
|
971 |
|
972 | try {
|
973 | for (var _b = __values(this.forwardTo), _c = _b.next(); !_c.done; _c = _b.next()) {
|
974 | var id = _c.value;
|
975 | var child = this.children.get(id);
|
976 |
|
977 | if (!child) {
|
978 | throw new Error("Unable to forward event '".concat(event, "' from interpreter '").concat(this.id, "' to nonexistant child '").concat(id, "'."));
|
979 | }
|
980 |
|
981 | child.send(event);
|
982 | }
|
983 | } catch (e_12_1) {
|
984 | e_12 = {
|
985 | error: e_12_1
|
986 | };
|
987 | } finally {
|
988 | try {
|
989 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
990 | } finally {
|
991 | if (e_12) throw e_12.error;
|
992 | }
|
993 | }
|
994 | };
|
995 |
|
996 | Interpreter.prototype.defer = function (sendAction) {
|
997 | var _this = this;
|
998 |
|
999 | var timerId = this.clock.setTimeout(function () {
|
1000 | if ('to' in sendAction && sendAction.to) {
|
1001 | _this.sendTo(sendAction._event, sendAction.to, true);
|
1002 | } else {
|
1003 | _this.send(sendAction._event);
|
1004 | }
|
1005 | }, sendAction.delay);
|
1006 |
|
1007 | if (sendAction.id) {
|
1008 | this.delayedEventsMap[sendAction.id] = timerId;
|
1009 | }
|
1010 | };
|
1011 |
|
1012 | Interpreter.prototype.cancel = function (sendId) {
|
1013 | this.clock.clearTimeout(this.delayedEventsMap[sendId]);
|
1014 | delete this.delayedEventsMap[sendId];
|
1015 | };
|
1016 |
|
1017 | Interpreter.prototype.exec = function (action, state, actionFunctionMap) {
|
1018 | if (actionFunctionMap === void 0) {
|
1019 | actionFunctionMap = this.machine.options.actions;
|
1020 | }
|
1021 |
|
1022 | this._exec(action, state.context, state._event, actionFunctionMap);
|
1023 | };
|
1024 |
|
1025 | Interpreter.prototype.removeChild = function (childId) {
|
1026 | var _a;
|
1027 |
|
1028 | this.children.delete(childId);
|
1029 | this.forwardTo.delete(childId);
|
1030 |
|
1031 |
|
1032 | (_a = this.state) === null || _a === void 0 ? true : delete _a.children[childId];
|
1033 | };
|
1034 |
|
1035 | Interpreter.prototype.stopChild = function (childId) {
|
1036 | var child = this.children.get(childId);
|
1037 |
|
1038 | if (!child) {
|
1039 | return;
|
1040 | }
|
1041 |
|
1042 | this.removeChild(childId);
|
1043 |
|
1044 | if (isFunction(child.stop)) {
|
1045 | child.stop();
|
1046 | }
|
1047 | };
|
1048 |
|
1049 | Interpreter.prototype.spawn = function (entity, name, options) {
|
1050 | if (this.status !== InterpreterStatus.Running) {
|
1051 | return createDeferredActor(entity, name);
|
1052 | }
|
1053 |
|
1054 | if (isPromiseLike(entity)) {
|
1055 | return this.spawnPromise(Promise.resolve(entity), name);
|
1056 | } else if (isFunction(entity)) {
|
1057 | return this.spawnCallback(entity, name);
|
1058 | } else if (isSpawnedActor(entity)) {
|
1059 | return this.spawnActor(entity, name);
|
1060 | } else if (isObservable(entity)) {
|
1061 | return this.spawnObservable(entity, name);
|
1062 | } else if (isMachine(entity)) {
|
1063 | return this.spawnMachine(entity, __assign(__assign({}, options), {
|
1064 | id: name
|
1065 | }));
|
1066 | } else if (isBehavior(entity)) {
|
1067 | return this.spawnBehavior(entity, name);
|
1068 | } else {
|
1069 | throw new Error("Unable to spawn entity \"".concat(name, "\" of type \"").concat(typeof entity, "\"."));
|
1070 | }
|
1071 | };
|
1072 |
|
1073 | Interpreter.prototype.spawnMachine = function (machine, options) {
|
1074 | var _this = this;
|
1075 |
|
1076 | if (options === void 0) {
|
1077 | options = {};
|
1078 | }
|
1079 |
|
1080 | var childService = new Interpreter(machine, __assign(__assign({}, this.options), {
|
1081 | parent: this,
|
1082 | id: options.id || machine.id
|
1083 | }));
|
1084 |
|
1085 | var resolvedOptions = __assign(__assign({}, DEFAULT_SPAWN_OPTIONS), options);
|
1086 |
|
1087 | if (resolvedOptions.sync) {
|
1088 | childService.onTransition(function (state) {
|
1089 | _this.send(update, {
|
1090 | state: state,
|
1091 | id: childService.id
|
1092 | });
|
1093 | });
|
1094 | }
|
1095 |
|
1096 | var actor = childService;
|
1097 | this.children.set(childService.id, actor);
|
1098 |
|
1099 | if (resolvedOptions.autoForward) {
|
1100 | this.forwardTo.add(childService.id);
|
1101 | }
|
1102 |
|
1103 | childService.onDone(function (doneEvent) {
|
1104 | _this.removeChild(childService.id);
|
1105 |
|
1106 | _this.send(toSCXMLEvent(doneEvent, {
|
1107 | origin: childService.id
|
1108 | }));
|
1109 | }).start();
|
1110 | return actor;
|
1111 | };
|
1112 |
|
1113 | Interpreter.prototype.spawnBehavior = function (behavior, id) {
|
1114 | var actorRef = spawnBehavior(behavior, {
|
1115 | id: id,
|
1116 | parent: this
|
1117 | });
|
1118 | this.children.set(id, actorRef);
|
1119 | return actorRef;
|
1120 | };
|
1121 |
|
1122 | Interpreter.prototype.spawnPromise = function (promise, id) {
|
1123 | var _a;
|
1124 |
|
1125 | var _this = this;
|
1126 |
|
1127 | var canceled = false;
|
1128 | var resolvedData;
|
1129 | promise.then(function (response) {
|
1130 | if (!canceled) {
|
1131 | resolvedData = response;
|
1132 |
|
1133 | _this.removeChild(id);
|
1134 |
|
1135 | _this.send(toSCXMLEvent(doneInvoke(id, response), {
|
1136 | origin: id
|
1137 | }));
|
1138 | }
|
1139 | }, function (errorData) {
|
1140 | if (!canceled) {
|
1141 | _this.removeChild(id);
|
1142 |
|
1143 | var errorEvent = error(id, errorData);
|
1144 |
|
1145 | try {
|
1146 |
|
1147 | _this.send(toSCXMLEvent(errorEvent, {
|
1148 | origin: id
|
1149 | }));
|
1150 | } catch (error) {
|
1151 | reportUnhandledExceptionOnInvocation(errorData, error, id);
|
1152 |
|
1153 | if (_this.devTools) {
|
1154 | _this.devTools.send(errorEvent, _this.state);
|
1155 | }
|
1156 |
|
1157 | if (_this.machine.strict) {
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 | _this.stop();
|
1163 | }
|
1164 | }
|
1165 | }
|
1166 | });
|
1167 | var actor = (_a = {
|
1168 | id: id,
|
1169 | send: function () {
|
1170 | return void 0;
|
1171 | },
|
1172 | subscribe: function (next, handleError, complete) {
|
1173 | var observer = toObserver(next, handleError, complete);
|
1174 | var unsubscribed = false;
|
1175 | promise.then(function (response) {
|
1176 | if (unsubscribed) {
|
1177 | return;
|
1178 | }
|
1179 |
|
1180 | observer.next(response);
|
1181 |
|
1182 | if (unsubscribed) {
|
1183 | return;
|
1184 | }
|
1185 |
|
1186 | observer.complete();
|
1187 | }, function (err) {
|
1188 | if (unsubscribed) {
|
1189 | return;
|
1190 | }
|
1191 |
|
1192 | observer.error(err);
|
1193 | });
|
1194 | return {
|
1195 | unsubscribe: function () {
|
1196 | return unsubscribed = true;
|
1197 | }
|
1198 | };
|
1199 | },
|
1200 | stop: function () {
|
1201 | canceled = true;
|
1202 | },
|
1203 | toJSON: function () {
|
1204 | return {
|
1205 | id: id
|
1206 | };
|
1207 | },
|
1208 | getSnapshot: function () {
|
1209 | return resolvedData;
|
1210 | }
|
1211 | }, _a[symbolObservable] = function () {
|
1212 | return this;
|
1213 | }, _a);
|
1214 | this.children.set(id, actor);
|
1215 | return actor;
|
1216 | };
|
1217 |
|
1218 | Interpreter.prototype.spawnCallback = function (callback, id) {
|
1219 | var _a;
|
1220 |
|
1221 | var _this = this;
|
1222 |
|
1223 | var canceled = false;
|
1224 | var receivers = new Set();
|
1225 | var listeners = new Set();
|
1226 | var emitted;
|
1227 |
|
1228 | var receive = function (e) {
|
1229 | emitted = e;
|
1230 | listeners.forEach(function (listener) {
|
1231 | return listener(e);
|
1232 | });
|
1233 |
|
1234 | if (canceled) {
|
1235 | return;
|
1236 | }
|
1237 |
|
1238 | _this.send(toSCXMLEvent(e, {
|
1239 | origin: id
|
1240 | }));
|
1241 | };
|
1242 |
|
1243 | var callbackStop;
|
1244 |
|
1245 | try {
|
1246 | callbackStop = callback(receive, function (newListener) {
|
1247 | receivers.add(newListener);
|
1248 | });
|
1249 | } catch (err) {
|
1250 | this.send(error(id, err));
|
1251 | }
|
1252 |
|
1253 | if (isPromiseLike(callbackStop)) {
|
1254 |
|
1255 |
|
1256 | return this.spawnPromise(callbackStop, id);
|
1257 | }
|
1258 |
|
1259 | var actor = (_a = {
|
1260 | id: id,
|
1261 | send: function (event) {
|
1262 | return receivers.forEach(function (receiver) {
|
1263 | return receiver(event);
|
1264 | });
|
1265 | },
|
1266 | subscribe: function (next) {
|
1267 | var observer = toObserver(next);
|
1268 | listeners.add(observer.next);
|
1269 | return {
|
1270 | unsubscribe: function () {
|
1271 | listeners.delete(observer.next);
|
1272 | }
|
1273 | };
|
1274 | },
|
1275 | stop: function () {
|
1276 | canceled = true;
|
1277 |
|
1278 | if (isFunction(callbackStop)) {
|
1279 | callbackStop();
|
1280 | }
|
1281 | },
|
1282 | toJSON: function () {
|
1283 | return {
|
1284 | id: id
|
1285 | };
|
1286 | },
|
1287 | getSnapshot: function () {
|
1288 | return emitted;
|
1289 | }
|
1290 | }, _a[symbolObservable] = function () {
|
1291 | return this;
|
1292 | }, _a);
|
1293 | this.children.set(id, actor);
|
1294 | return actor;
|
1295 | };
|
1296 |
|
1297 | Interpreter.prototype.spawnObservable = function (source, id) {
|
1298 | var _a;
|
1299 |
|
1300 | var _this = this;
|
1301 |
|
1302 | var emitted;
|
1303 | var subscription = source.subscribe(function (value) {
|
1304 | emitted = value;
|
1305 |
|
1306 | _this.send(toSCXMLEvent(value, {
|
1307 | origin: id
|
1308 | }));
|
1309 | }, function (err) {
|
1310 | _this.removeChild(id);
|
1311 |
|
1312 | _this.send(toSCXMLEvent(error(id, err), {
|
1313 | origin: id
|
1314 | }));
|
1315 | }, function () {
|
1316 | _this.removeChild(id);
|
1317 |
|
1318 | _this.send(toSCXMLEvent(doneInvoke(id), {
|
1319 | origin: id
|
1320 | }));
|
1321 | });
|
1322 | var actor = (_a = {
|
1323 | id: id,
|
1324 | send: function () {
|
1325 | return void 0;
|
1326 | },
|
1327 | subscribe: function (next, handleError, complete) {
|
1328 | return source.subscribe(next, handleError, complete);
|
1329 | },
|
1330 | stop: function () {
|
1331 | return subscription.unsubscribe();
|
1332 | },
|
1333 | getSnapshot: function () {
|
1334 | return emitted;
|
1335 | },
|
1336 | toJSON: function () {
|
1337 | return {
|
1338 | id: id
|
1339 | };
|
1340 | }
|
1341 | }, _a[symbolObservable] = function () {
|
1342 | return this;
|
1343 | }, _a);
|
1344 | this.children.set(id, actor);
|
1345 | return actor;
|
1346 | };
|
1347 |
|
1348 | Interpreter.prototype.spawnActor = function (actor, name) {
|
1349 | this.children.set(name, actor);
|
1350 | return actor;
|
1351 | };
|
1352 |
|
1353 | Interpreter.prototype.spawnActivity = function (activity) {
|
1354 | var implementation = this.machine.options && this.machine.options.activities ? this.machine.options.activities[activity.type] : undefined;
|
1355 |
|
1356 | if (!implementation) {
|
1357 | if (!IS_PRODUCTION) {
|
1358 | warn(false, "No implementation found for activity '".concat(activity.type, "'"));
|
1359 | }
|
1360 |
|
1361 |
|
1362 | return;
|
1363 | }
|
1364 |
|
1365 |
|
1366 | var dispose = implementation(this.state.context, activity);
|
1367 | this.spawnEffect(activity.id, dispose);
|
1368 | };
|
1369 |
|
1370 | Interpreter.prototype.spawnEffect = function (id, dispose) {
|
1371 | var _a;
|
1372 |
|
1373 | this.children.set(id, (_a = {
|
1374 | id: id,
|
1375 | send: function () {
|
1376 | return void 0;
|
1377 | },
|
1378 | subscribe: function () {
|
1379 | return {
|
1380 | unsubscribe: function () {
|
1381 | return void 0;
|
1382 | }
|
1383 | };
|
1384 | },
|
1385 | stop: dispose || undefined,
|
1386 | getSnapshot: function () {
|
1387 | return undefined;
|
1388 | },
|
1389 | toJSON: function () {
|
1390 | return {
|
1391 | id: id
|
1392 | };
|
1393 | }
|
1394 | }, _a[symbolObservable] = function () {
|
1395 | return this;
|
1396 | }, _a));
|
1397 | };
|
1398 |
|
1399 | Interpreter.prototype.attachDev = function () {
|
1400 | var global = getGlobal();
|
1401 |
|
1402 | if (this.options.devTools && global) {
|
1403 | if (global.__REDUX_DEVTOOLS_EXTENSION__) {
|
1404 | var devToolsOptions = typeof this.options.devTools === 'object' ? this.options.devTools : undefined;
|
1405 | this.devTools = global.__REDUX_DEVTOOLS_EXTENSION__.connect(__assign(__assign({
|
1406 | name: this.id,
|
1407 | autoPause: true,
|
1408 | stateSanitizer: function (state) {
|
1409 | return {
|
1410 | value: state.value,
|
1411 | context: state.context,
|
1412 | actions: state.actions
|
1413 | };
|
1414 | }
|
1415 | }, devToolsOptions), {
|
1416 | features: __assign({
|
1417 | jump: false,
|
1418 | skip: false
|
1419 | }, devToolsOptions ? devToolsOptions.features : undefined)
|
1420 | }), this.machine);
|
1421 | this.devTools.init(this.state);
|
1422 | }
|
1423 |
|
1424 |
|
1425 | registerService(this);
|
1426 | }
|
1427 | };
|
1428 |
|
1429 | Interpreter.prototype.toJSON = function () {
|
1430 | return {
|
1431 | id: this.id
|
1432 | };
|
1433 | };
|
1434 |
|
1435 | Interpreter.prototype[symbolObservable] = function () {
|
1436 | return this;
|
1437 | };
|
1438 |
|
1439 | Interpreter.prototype.getSnapshot = function () {
|
1440 | if (this.status === InterpreterStatus.NotStarted) {
|
1441 | return this.initialState;
|
1442 | }
|
1443 |
|
1444 | return this._state;
|
1445 | };
|
1446 | |
1447 |
|
1448 |
|
1449 |
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 | Interpreter.defaultOptions = {
|
1455 | execute: true,
|
1456 | deferEvents: true,
|
1457 | clock: {
|
1458 | setTimeout: function (fn, ms) {
|
1459 | return setTimeout(fn, ms);
|
1460 | },
|
1461 | clearTimeout: function (id) {
|
1462 | return clearTimeout(id);
|
1463 | }
|
1464 | },
|
1465 | logger: console.log.bind(console),
|
1466 | devTools: false
|
1467 | };
|
1468 | Interpreter.interpret = interpret;
|
1469 | return Interpreter;
|
1470 | }();
|
1471 |
|
1472 | var resolveSpawnOptions = function (nameOrOptions) {
|
1473 | if (isString(nameOrOptions)) {
|
1474 | return __assign(__assign({}, DEFAULT_SPAWN_OPTIONS), {
|
1475 | name: nameOrOptions
|
1476 | });
|
1477 | }
|
1478 |
|
1479 | return __assign(__assign(__assign({}, DEFAULT_SPAWN_OPTIONS), {
|
1480 | name: uniqueId()
|
1481 | }), nameOrOptions);
|
1482 | };
|
1483 |
|
1484 | function spawn(entity, nameOrOptions) {
|
1485 | var resolvedOptions = resolveSpawnOptions(nameOrOptions);
|
1486 | return consume(function (service) {
|
1487 | if (!IS_PRODUCTION) {
|
1488 | var isLazyEntity = isMachine(entity) || isFunction(entity);
|
1489 | warn(!!service || isLazyEntity, "Attempted to spawn an Actor (ID: \"".concat(isMachine(entity) ? entity.id : 'undefined', "\") outside of a service. This will have no effect."));
|
1490 | }
|
1491 |
|
1492 | if (service) {
|
1493 | return service.spawn(entity, resolvedOptions.name, resolvedOptions);
|
1494 | } else {
|
1495 | return createDeferredActor(entity, resolvedOptions.name);
|
1496 | }
|
1497 | });
|
1498 | }
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 | function interpret(machine, options) {
|
1507 | var interpreter = new Interpreter(machine, options);
|
1508 | return interpreter;
|
1509 | }
|
1510 |
|
1511 | export { Interpreter, InterpreterStatus, interpret, spawn };
|