xstate 4.10.0 → 4.14.0
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.
- package/CHANGELOG.md +205 -0
- package/LICENSE +22 -0
- package/README.md +2 -2
- package/dist/xstate.interpreter.js +1 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.web.js +2 -2
- package/es/Actor.d.ts +5 -3
- package/es/Actor.js +26 -4
- package/es/State.d.ts +5 -2
- package/es/StateNode.d.ts +8 -8
- package/es/StateNode.js +61 -41
- package/es/actions.d.ts +9 -5
- package/es/actions.js +28 -13
- package/es/index.d.ts +2 -2
- package/es/index.js +1 -1
- package/es/interpreter.d.ts +29 -15
- package/es/interpreter.js +99 -112
- package/es/invokeUtils.d.ts +7 -0
- package/es/invokeUtils.js +39 -0
- package/es/match.d.ts +4 -1
- package/es/serviceScope.d.ts +10 -0
- package/es/serviceScope.js +18 -0
- package/es/stateUtils.d.ts +1 -1
- package/es/types.d.ts +79 -32
- package/es/utils.d.ts +4 -3
- package/es/utils.js +12 -3
- package/lib/Actor.d.ts +5 -3
- package/lib/Actor.js +24 -3
- package/lib/Machine.js +1 -0
- package/lib/SimulatedClock.js +1 -0
- package/lib/State.d.ts +5 -2
- package/lib/State.js +1 -0
- package/lib/StateNode.d.ts +8 -8
- package/lib/StateNode.js +69 -42
- package/lib/actionTypes.js +1 -0
- package/lib/actions.d.ts +9 -5
- package/lib/actions.js +27 -19
- package/lib/constants.js +1 -0
- package/lib/devTools.js +1 -0
- package/lib/each.js +1 -0
- package/lib/environment.js +1 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +29 -20
- package/lib/interpreter.d.ts +28 -14
- package/lib/interpreter.js +82 -88
- package/lib/invokeUtils.d.ts +7 -0
- package/lib/invokeUtils.js +42 -0
- package/lib/json.js +1 -0
- package/lib/mapState.js +1 -0
- package/lib/match.d.ts +4 -1
- package/lib/match.js +1 -0
- package/lib/patterns.js +1 -0
- package/lib/registry.js +1 -0
- package/lib/scheduler.js +1 -0
- package/lib/scxml.js +1 -0
- package/lib/serviceScope.d.ts +10 -0
- package/lib/serviceScope.js +15 -0
- package/lib/stateUtils.d.ts +1 -1
- package/lib/stateUtils.js +1 -0
- package/lib/types.d.ts +79 -32
- package/lib/types.js +1 -0
- package/lib/utils.d.ts +4 -3
- package/lib/utils.js +9 -2
- package/package.json +6 -6
package/es/interpreter.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { __values, __assign, __spread } from './_virtual/_tslib.js';
|
|
2
2
|
import { IS_PRODUCTION } from './environment.js';
|
|
3
|
-
import { warn, mapContext, isFunction, keys, toSCXMLEvent, isPromiseLike, isObservable, isMachine, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, uniqueId } from './utils.js';
|
|
3
|
+
import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isPromiseLike, isObservable, isMachine, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, uniqueId } from './utils.js';
|
|
4
4
|
import { ActionTypes, SpecialTargets } from './types.js';
|
|
5
5
|
import { isInFinalState } from './stateUtils.js';
|
|
6
6
|
import { errorPlatform, log, stop, start, cancel, send, update, error as error$1 } from './actionTypes.js';
|
|
7
7
|
import { doneInvoke, initEvent, getActionFunction, error } from './actions.js';
|
|
8
8
|
import { isState, State, bindActionToState } from './State.js';
|
|
9
|
-
import {
|
|
9
|
+
import { provide, consume } from './serviceScope.js';
|
|
10
|
+
import { isActor, createDeferredActor } from './Actor.js';
|
|
10
11
|
import { Scheduler } from './scheduler.js';
|
|
11
12
|
import { registry } from './registry.js';
|
|
12
13
|
import { registerService } from './devTools.js';
|
|
@@ -14,23 +15,6 @@ var DEFAULT_SPAWN_OPTIONS = {
|
|
|
14
15
|
sync: false,
|
|
15
16
|
autoForward: false
|
|
16
17
|
};
|
|
17
|
-
/**
|
|
18
|
-
* Maintains a stack of the current service in scope.
|
|
19
|
-
* This is used to provide the correct service to spawn().
|
|
20
|
-
*
|
|
21
|
-
* @private
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
var withServiceScope = /*#__PURE__*/function () {
|
|
25
|
-
var serviceStack = [];
|
|
26
|
-
return function (service, fn) {
|
|
27
|
-
service && serviceStack.push(service);
|
|
28
|
-
var result = fn(service || serviceStack[serviceStack.length - 1]);
|
|
29
|
-
service && serviceStack.pop();
|
|
30
|
-
return result;
|
|
31
|
-
};
|
|
32
|
-
}();
|
|
33
|
-
|
|
34
18
|
var InterpreterStatus;
|
|
35
19
|
|
|
36
20
|
(function (InterpreterStatus) {
|
|
@@ -71,7 +55,7 @@ function () {
|
|
|
71
55
|
*/
|
|
72
56
|
|
|
73
57
|
this.initialized = false;
|
|
74
|
-
this.
|
|
58
|
+
this.status = InterpreterStatus.NotStarted;
|
|
75
59
|
this.children = new Map();
|
|
76
60
|
this.forwardTo = new Set();
|
|
77
61
|
/**
|
|
@@ -98,7 +82,7 @@ function () {
|
|
|
98
82
|
|
|
99
83
|
var _event = toSCXMLEvent(toEventObject(event, payload));
|
|
100
84
|
|
|
101
|
-
if (_this.
|
|
85
|
+
if (_this.status === InterpreterStatus.Stopped) {
|
|
102
86
|
// do nothing
|
|
103
87
|
if (!IS_PRODUCTION) {
|
|
104
88
|
warn(false, "Event \"" + _event.name + "\" was sent to stopped service \"" + _this.machine.id + "\". This service has already reached its final state, and will not transition.\nEvent: " + JSON.stringify(_event.data));
|
|
@@ -107,12 +91,7 @@ function () {
|
|
|
107
91
|
return _this.state;
|
|
108
92
|
}
|
|
109
93
|
|
|
110
|
-
if (_this.
|
|
111
|
-
// tslint:disable-next-line:no-console
|
|
112
|
-
if (!IS_PRODUCTION) {
|
|
113
|
-
warn(false, "Event \"" + _event.name + "\" was sent to uninitialized service \"" + _this.machine.id + "\" and is deferred. Make sure .start() is called for this service.\nEvent: " + JSON.stringify(_event.data));
|
|
114
|
-
}
|
|
115
|
-
} else if (_this._status !== InterpreterStatus.Running) {
|
|
94
|
+
if (_this.status !== InterpreterStatus.Running && !_this.options.deferEvents) {
|
|
116
95
|
throw new Error("Event \"" + _event.name + "\" was sent to uninitialized service \"" + _this.machine.id + "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: " + JSON.stringify(_event.data));
|
|
117
96
|
}
|
|
118
97
|
|
|
@@ -184,23 +163,23 @@ function () {
|
|
|
184
163
|
return this._initialState;
|
|
185
164
|
}
|
|
186
165
|
|
|
187
|
-
return
|
|
166
|
+
return provide(this, function () {
|
|
188
167
|
_this._initialState = _this.machine.initialState;
|
|
189
168
|
return _this._initialState;
|
|
190
169
|
});
|
|
191
170
|
},
|
|
192
|
-
enumerable:
|
|
171
|
+
enumerable: false,
|
|
193
172
|
configurable: true
|
|
194
173
|
});
|
|
195
174
|
Object.defineProperty(Interpreter.prototype, "state", {
|
|
196
175
|
get: function () {
|
|
197
176
|
if (!IS_PRODUCTION) {
|
|
198
|
-
warn(this.
|
|
177
|
+
warn(this.status !== InterpreterStatus.NotStarted, "Attempted to read state from uninitialized service '" + this.id + "'. Make sure the service is started first.");
|
|
199
178
|
}
|
|
200
179
|
|
|
201
180
|
return this._state;
|
|
202
181
|
},
|
|
203
|
-
enumerable:
|
|
182
|
+
enumerable: false,
|
|
204
183
|
configurable: true
|
|
205
184
|
});
|
|
206
185
|
/**
|
|
@@ -243,8 +222,12 @@ function () {
|
|
|
243
222
|
|
|
244
223
|
if (this.options.execute) {
|
|
245
224
|
this.execute(this.state);
|
|
246
|
-
} //
|
|
225
|
+
} // Update children
|
|
226
|
+
|
|
247
227
|
|
|
228
|
+
this.children.forEach(function (child) {
|
|
229
|
+
_this.state.children[child.id] = child;
|
|
230
|
+
}); // Dev tools
|
|
248
231
|
|
|
249
232
|
if (this.devTools) {
|
|
250
233
|
this.devTools.send(_event.data, state);
|
|
@@ -307,11 +290,33 @@ function () {
|
|
|
307
290
|
var isDone = isInFinalState(state.configuration || [], this.machine);
|
|
308
291
|
|
|
309
292
|
if (this.state.configuration && isDone) {
|
|
310
|
-
//
|
|
293
|
+
// exit interpreter procedure: https://www.w3.org/TR/scxml/#exitInterpreter
|
|
294
|
+
this.state.configuration.forEach(function (stateNode) {
|
|
295
|
+
var e_6, _a;
|
|
296
|
+
|
|
297
|
+
try {
|
|
298
|
+
for (var _b = __values(stateNode.definition.exit), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
299
|
+
var action = _c.value;
|
|
300
|
+
|
|
301
|
+
_this.exec(action, state);
|
|
302
|
+
}
|
|
303
|
+
} catch (e_6_1) {
|
|
304
|
+
e_6 = {
|
|
305
|
+
error: e_6_1
|
|
306
|
+
};
|
|
307
|
+
} finally {
|
|
308
|
+
try {
|
|
309
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
310
|
+
} finally {
|
|
311
|
+
if (e_6) throw e_6.error;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}); // get final child state node
|
|
315
|
+
|
|
311
316
|
var finalChildStateNode = state.configuration.find(function (sn) {
|
|
312
317
|
return sn.type === 'final' && sn.parent === _this.machine;
|
|
313
318
|
});
|
|
314
|
-
var doneData = finalChildStateNode && finalChildStateNode.
|
|
319
|
+
var doneData = finalChildStateNode && finalChildStateNode.doneData ? mapContext(finalChildStateNode.doneData, state.context, _event) : undefined;
|
|
315
320
|
|
|
316
321
|
try {
|
|
317
322
|
for (var _l = __values(this.doneListeners), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
@@ -344,15 +349,15 @@ function () {
|
|
|
344
349
|
Interpreter.prototype.onTransition = function (listener) {
|
|
345
350
|
this.listeners.add(listener); // Send current state to listener
|
|
346
351
|
|
|
347
|
-
if (this.
|
|
352
|
+
if (this.status === InterpreterStatus.Running) {
|
|
348
353
|
listener(this.state, this.state.event);
|
|
349
354
|
}
|
|
350
355
|
|
|
351
356
|
return this;
|
|
352
357
|
};
|
|
353
358
|
|
|
354
|
-
Interpreter.prototype.subscribe = function (nextListenerOrObserver, //
|
|
355
|
-
|
|
359
|
+
Interpreter.prototype.subscribe = function (nextListenerOrObserver, _, // TODO: error listener
|
|
360
|
+
completeListener) {
|
|
356
361
|
var _this = this;
|
|
357
362
|
|
|
358
363
|
if (!nextListenerOrObserver) {
|
|
@@ -375,7 +380,7 @@ function () {
|
|
|
375
380
|
|
|
376
381
|
this.listeners.add(listener); // Send current state to listener
|
|
377
382
|
|
|
378
|
-
if (this.
|
|
383
|
+
if (this.status === InterpreterStatus.Running) {
|
|
379
384
|
listener(this.state);
|
|
380
385
|
}
|
|
381
386
|
|
|
@@ -464,15 +469,15 @@ function () {
|
|
|
464
469
|
Interpreter.prototype.start = function (initialState) {
|
|
465
470
|
var _this = this;
|
|
466
471
|
|
|
467
|
-
if (this.
|
|
472
|
+
if (this.status === InterpreterStatus.Running) {
|
|
468
473
|
// Do not restart the service if it is already started
|
|
469
474
|
return this;
|
|
470
475
|
}
|
|
471
476
|
|
|
472
477
|
registry.register(this.sessionId, this);
|
|
473
478
|
this.initialized = true;
|
|
474
|
-
this.
|
|
475
|
-
var resolvedState = initialState === undefined ? this.initialState :
|
|
479
|
+
this.status = InterpreterStatus.Running;
|
|
480
|
+
var resolvedState = initialState === undefined ? this.initialState : provide(this, function () {
|
|
476
481
|
return isState(initialState) ? _this.machine.resolveState(initialState) : _this.machine.resolveState(State.from(initialState, _this.machine.context));
|
|
477
482
|
});
|
|
478
483
|
|
|
@@ -493,22 +498,22 @@ function () {
|
|
|
493
498
|
|
|
494
499
|
|
|
495
500
|
Interpreter.prototype.stop = function () {
|
|
496
|
-
var
|
|
501
|
+
var e_7, _a, e_8, _b, e_9, _c, e_10, _d, e_11, _e;
|
|
497
502
|
|
|
498
503
|
try {
|
|
499
504
|
for (var _f = __values(this.listeners), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
500
505
|
var listener = _g.value;
|
|
501
506
|
this.listeners.delete(listener);
|
|
502
507
|
}
|
|
503
|
-
} catch (
|
|
504
|
-
|
|
505
|
-
error:
|
|
508
|
+
} catch (e_7_1) {
|
|
509
|
+
e_7 = {
|
|
510
|
+
error: e_7_1
|
|
506
511
|
};
|
|
507
512
|
} finally {
|
|
508
513
|
try {
|
|
509
514
|
if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
|
|
510
515
|
} finally {
|
|
511
|
-
if (
|
|
516
|
+
if (e_7) throw e_7.error;
|
|
512
517
|
}
|
|
513
518
|
}
|
|
514
519
|
|
|
@@ -519,15 +524,15 @@ function () {
|
|
|
519
524
|
listener();
|
|
520
525
|
this.stopListeners.delete(listener);
|
|
521
526
|
}
|
|
522
|
-
} catch (
|
|
523
|
-
|
|
524
|
-
error:
|
|
527
|
+
} catch (e_8_1) {
|
|
528
|
+
e_8 = {
|
|
529
|
+
error: e_8_1
|
|
525
530
|
};
|
|
526
531
|
} finally {
|
|
527
532
|
try {
|
|
528
533
|
if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
|
|
529
534
|
} finally {
|
|
530
|
-
if (
|
|
535
|
+
if (e_8) throw e_8.error;
|
|
531
536
|
}
|
|
532
537
|
}
|
|
533
538
|
|
|
@@ -536,15 +541,15 @@ function () {
|
|
|
536
541
|
var listener = _l.value;
|
|
537
542
|
this.contextListeners.delete(listener);
|
|
538
543
|
}
|
|
539
|
-
} catch (
|
|
540
|
-
|
|
541
|
-
error:
|
|
544
|
+
} catch (e_9_1) {
|
|
545
|
+
e_9 = {
|
|
546
|
+
error: e_9_1
|
|
542
547
|
};
|
|
543
548
|
} finally {
|
|
544
549
|
try {
|
|
545
550
|
if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
|
|
546
551
|
} finally {
|
|
547
|
-
if (
|
|
552
|
+
if (e_9) throw e_9.error;
|
|
548
553
|
}
|
|
549
554
|
}
|
|
550
555
|
|
|
@@ -553,15 +558,15 @@ function () {
|
|
|
553
558
|
var listener = _o.value;
|
|
554
559
|
this.doneListeners.delete(listener);
|
|
555
560
|
}
|
|
556
|
-
} catch (
|
|
557
|
-
|
|
558
|
-
error:
|
|
561
|
+
} catch (e_10_1) {
|
|
562
|
+
e_10 = {
|
|
563
|
+
error: e_10_1
|
|
559
564
|
};
|
|
560
565
|
} finally {
|
|
561
566
|
try {
|
|
562
567
|
if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
|
|
563
568
|
} finally {
|
|
564
|
-
if (
|
|
569
|
+
if (e_10) throw e_10.error;
|
|
565
570
|
}
|
|
566
571
|
} // Stop all children
|
|
567
572
|
|
|
@@ -578,21 +583,21 @@ function () {
|
|
|
578
583
|
var key = _q.value;
|
|
579
584
|
this.clock.clearTimeout(this.delayedEventsMap[key]);
|
|
580
585
|
}
|
|
581
|
-
} catch (
|
|
582
|
-
|
|
583
|
-
error:
|
|
586
|
+
} catch (e_11_1) {
|
|
587
|
+
e_11 = {
|
|
588
|
+
error: e_11_1
|
|
584
589
|
};
|
|
585
590
|
} finally {
|
|
586
591
|
try {
|
|
587
592
|
if (_q && !_q.done && (_e = _p.return)) _e.call(_p);
|
|
588
593
|
} finally {
|
|
589
|
-
if (
|
|
594
|
+
if (e_11) throw e_11.error;
|
|
590
595
|
}
|
|
591
596
|
}
|
|
592
597
|
|
|
593
598
|
this.scheduler.clear();
|
|
594
599
|
this.initialized = false;
|
|
595
|
-
this.
|
|
600
|
+
this.status = InterpreterStatus.Stopped;
|
|
596
601
|
registry.free(this.sessionId);
|
|
597
602
|
return this;
|
|
598
603
|
};
|
|
@@ -600,18 +605,18 @@ function () {
|
|
|
600
605
|
Interpreter.prototype.batch = function (events) {
|
|
601
606
|
var _this = this;
|
|
602
607
|
|
|
603
|
-
if (this.
|
|
608
|
+
if (this.status === InterpreterStatus.NotStarted && this.options.deferEvents) {
|
|
604
609
|
// tslint:disable-next-line:no-console
|
|
605
610
|
if (!IS_PRODUCTION) {
|
|
606
611
|
warn(false, events.length + " event(s) were sent to uninitialized service \"" + this.machine.id + "\" and are deferred. Make sure .start() is called for this service.\nEvent: " + JSON.stringify(event));
|
|
607
612
|
}
|
|
608
|
-
} else if (this.
|
|
613
|
+
} else if (this.status !== InterpreterStatus.Running) {
|
|
609
614
|
throw new Error( // tslint:disable-next-line:max-line-length
|
|
610
615
|
events.length + " event(s) were sent to uninitialized service \"" + this.machine.id + "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.");
|
|
611
616
|
}
|
|
612
617
|
|
|
613
618
|
this.scheduler.schedule(function () {
|
|
614
|
-
var
|
|
619
|
+
var e_12, _a;
|
|
615
620
|
|
|
616
621
|
var nextState = _this.state;
|
|
617
622
|
var batchChanged = false;
|
|
@@ -622,7 +627,7 @@ function () {
|
|
|
622
627
|
|
|
623
628
|
_this.forward(_event);
|
|
624
629
|
|
|
625
|
-
nextState =
|
|
630
|
+
nextState = provide(_this, function () {
|
|
626
631
|
return _this.machine.transition(nextState, _event);
|
|
627
632
|
});
|
|
628
633
|
batchedActions.push.apply(batchedActions, __spread(nextState.actions.map(function (a) {
|
|
@@ -637,15 +642,15 @@ function () {
|
|
|
637
642
|
|
|
638
643
|
_loop_1(event_1);
|
|
639
644
|
}
|
|
640
|
-
} catch (
|
|
641
|
-
|
|
642
|
-
error:
|
|
645
|
+
} catch (e_12_1) {
|
|
646
|
+
e_12 = {
|
|
647
|
+
error: e_12_1
|
|
643
648
|
};
|
|
644
649
|
} finally {
|
|
645
650
|
try {
|
|
646
651
|
if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
|
|
647
652
|
} finally {
|
|
648
|
-
if (
|
|
653
|
+
if (e_12) throw e_12.error;
|
|
649
654
|
}
|
|
650
655
|
}
|
|
651
656
|
|
|
@@ -685,14 +690,14 @@ function () {
|
|
|
685
690
|
throw _event.data.data;
|
|
686
691
|
}
|
|
687
692
|
|
|
688
|
-
var nextState =
|
|
693
|
+
var nextState = provide(this, function () {
|
|
689
694
|
return _this.machine.transition(_this.state, _event);
|
|
690
695
|
});
|
|
691
696
|
return nextState;
|
|
692
697
|
};
|
|
693
698
|
|
|
694
699
|
Interpreter.prototype.forward = function (event) {
|
|
695
|
-
var
|
|
700
|
+
var e_13, _a;
|
|
696
701
|
|
|
697
702
|
try {
|
|
698
703
|
for (var _b = __values(this.forwardTo), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
@@ -705,15 +710,15 @@ function () {
|
|
|
705
710
|
|
|
706
711
|
child.send(event);
|
|
707
712
|
}
|
|
708
|
-
} catch (
|
|
709
|
-
|
|
710
|
-
error:
|
|
713
|
+
} catch (e_13_1) {
|
|
714
|
+
e_13 = {
|
|
715
|
+
error: e_13_1
|
|
711
716
|
};
|
|
712
717
|
} finally {
|
|
713
718
|
try {
|
|
714
719
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
715
720
|
} finally {
|
|
716
|
-
if (
|
|
721
|
+
if (e_13) throw e_13.error;
|
|
717
722
|
}
|
|
718
723
|
}
|
|
719
724
|
};
|
|
@@ -797,7 +802,8 @@ function () {
|
|
|
797
802
|
|
|
798
803
|
|
|
799
804
|
if (activity.type === ActionTypes.Invoke) {
|
|
800
|
-
var
|
|
805
|
+
var invokeSource = toInvokeSource(activity.src);
|
|
806
|
+
var serviceCreator = this.machine.options.services ? this.machine.options.services[invokeSource.type] : undefined;
|
|
801
807
|
var id = activity.id,
|
|
802
808
|
data = activity.data;
|
|
803
809
|
|
|
@@ -817,17 +823,21 @@ function () {
|
|
|
817
823
|
return;
|
|
818
824
|
}
|
|
819
825
|
|
|
820
|
-
var
|
|
826
|
+
var resolvedData = data ? mapContext(data, context, _event) : undefined;
|
|
827
|
+
var source = isFunction(serviceCreator) ? serviceCreator(context, _event.data, {
|
|
828
|
+
data: resolvedData,
|
|
829
|
+
src: invokeSource
|
|
830
|
+
}) : serviceCreator;
|
|
821
831
|
|
|
822
832
|
if (isPromiseLike(source)) {
|
|
823
|
-
this.
|
|
833
|
+
this.spawnPromise(Promise.resolve(source), id);
|
|
824
834
|
} else if (isFunction(source)) {
|
|
825
|
-
this.
|
|
835
|
+
this.spawnCallback(source, id);
|
|
826
836
|
} else if (isObservable(source)) {
|
|
827
|
-
this.
|
|
837
|
+
this.spawnObservable(source, id);
|
|
828
838
|
} else if (isMachine(source)) {
|
|
829
839
|
// TODO: try/catch here
|
|
830
|
-
this.
|
|
840
|
+
this.spawnMachine(resolvedData ? source.withContext(resolvedData) : source, {
|
|
831
841
|
id: id,
|
|
832
842
|
autoForward: autoForward
|
|
833
843
|
});
|
|
@@ -1243,36 +1253,12 @@ function () {
|
|
|
1243
1253
|
logger: global.console.log.bind(console),
|
|
1244
1254
|
devTools: false
|
|
1245
1255
|
};
|
|
1246
|
-
}(typeof
|
|
1256
|
+
}(typeof self !== 'undefined' ? self : global);
|
|
1247
1257
|
|
|
1248
1258
|
Interpreter.interpret = interpret;
|
|
1249
1259
|
return Interpreter;
|
|
1250
1260
|
}();
|
|
1251
1261
|
|
|
1252
|
-
var createNullActor = function (name) {
|
|
1253
|
-
if (name === void 0) {
|
|
1254
|
-
name = 'null';
|
|
1255
|
-
}
|
|
1256
|
-
|
|
1257
|
-
return {
|
|
1258
|
-
id: name,
|
|
1259
|
-
send: function () {
|
|
1260
|
-
return void 0;
|
|
1261
|
-
},
|
|
1262
|
-
subscribe: function () {
|
|
1263
|
-
// tslint:disable-next-line:no-empty
|
|
1264
|
-
return {
|
|
1265
|
-
unsubscribe: function () {}
|
|
1266
|
-
};
|
|
1267
|
-
},
|
|
1268
|
-
toJSON: function () {
|
|
1269
|
-
return {
|
|
1270
|
-
id: name
|
|
1271
|
-
};
|
|
1272
|
-
}
|
|
1273
|
-
};
|
|
1274
|
-
};
|
|
1275
|
-
|
|
1276
1262
|
var resolveSpawnOptions = function (nameOrOptions) {
|
|
1277
1263
|
if (isString(nameOrOptions)) {
|
|
1278
1264
|
return __assign(__assign({}, DEFAULT_SPAWN_OPTIONS), {
|
|
@@ -1287,15 +1273,16 @@ var resolveSpawnOptions = function (nameOrOptions) {
|
|
|
1287
1273
|
|
|
1288
1274
|
function spawn(entity, nameOrOptions) {
|
|
1289
1275
|
var resolvedOptions = resolveSpawnOptions(nameOrOptions);
|
|
1290
|
-
return
|
|
1276
|
+
return consume(function (service) {
|
|
1291
1277
|
if (!IS_PRODUCTION) {
|
|
1292
|
-
|
|
1278
|
+
var isLazyEntity = isMachine(entity) || isFunction(entity);
|
|
1279
|
+
warn(!!service || isLazyEntity, "Attempted to spawn an Actor (ID: \"" + (isMachine(entity) ? entity.id : 'undefined') + "\") outside of a service. This will have no effect.");
|
|
1293
1280
|
}
|
|
1294
1281
|
|
|
1295
1282
|
if (service) {
|
|
1296
1283
|
return service.spawn(entity, resolvedOptions.name, resolvedOptions);
|
|
1297
1284
|
} else {
|
|
1298
|
-
return
|
|
1285
|
+
return createDeferredActor(entity, resolvedOptions.name);
|
|
1299
1286
|
}
|
|
1300
1287
|
});
|
|
1301
1288
|
}
|
|
@@ -1312,4 +1299,4 @@ function interpret(machine, options) {
|
|
|
1312
1299
|
return interpreter;
|
|
1313
1300
|
}
|
|
1314
1301
|
|
|
1315
|
-
export { Interpreter, interpret, spawn };
|
|
1302
|
+
export { Interpreter, InterpreterStatus, interpret, spawn };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EventObject, InvokeConfig, InvokeDefinition, InvokeSourceDefinition } from './types';
|
|
2
|
+
export declare function toInvokeSource(src: string | InvokeSourceDefinition): InvokeSourceDefinition;
|
|
3
|
+
export declare function toInvokeDefinition<TContext, TEvent extends EventObject>(invokeConfig: InvokeConfig<TContext, TEvent> & {
|
|
4
|
+
src: string | InvokeSourceDefinition;
|
|
5
|
+
id: string;
|
|
6
|
+
}): InvokeDefinition<TContext, TEvent>;
|
|
7
|
+
//# sourceMappingURL=invokeUtils.d.ts.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { __assign, __rest } from './_virtual/_tslib.js';
|
|
2
|
+
import { invoke } from './actionTypes.js';
|
|
3
|
+
import './actions.js';
|
|
4
|
+
|
|
5
|
+
function toInvokeSource(src) {
|
|
6
|
+
if (typeof src === 'string') {
|
|
7
|
+
var simpleSrc = {
|
|
8
|
+
type: src
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
simpleSrc.toString = function () {
|
|
12
|
+
return src;
|
|
13
|
+
}; // v4 compat - TODO: remove in v5
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
return simpleSrc;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return src;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function toInvokeDefinition(invokeConfig) {
|
|
23
|
+
return __assign(__assign({
|
|
24
|
+
type: invoke
|
|
25
|
+
}, invokeConfig), {
|
|
26
|
+
toJSON: function () {
|
|
27
|
+
var onDone = invokeConfig.onDone,
|
|
28
|
+
onError = invokeConfig.onError,
|
|
29
|
+
invokeDef = __rest(invokeConfig, ["onDone", "onError"]);
|
|
30
|
+
|
|
31
|
+
return __assign(__assign({}, invokeDef), {
|
|
32
|
+
type: invoke,
|
|
33
|
+
src: toInvokeSource(invokeConfig.src)
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { toInvokeDefinition, toInvokeSource };
|
package/es/match.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { State } from './State';
|
|
2
2
|
import { StateValue, EventObject } from './types';
|
|
3
3
|
export declare type ValueFromStateGetter<T, TContext, TEvent extends EventObject> = (state: State<TContext, TEvent>) => T;
|
|
4
|
-
export declare type StatePatternTuple<T, TContext, TEvent extends EventObject> = [
|
|
4
|
+
export declare type StatePatternTuple<T, TContext, TEvent extends EventObject> = [
|
|
5
|
+
StateValue,
|
|
6
|
+
ValueFromStateGetter<T, TContext, TEvent>
|
|
7
|
+
];
|
|
5
8
|
export declare function matchState<T, TContext, TEvent extends EventObject>(state: State<TContext, TEvent> | StateValue, patterns: Array<StatePatternTuple<T, TContext, TEvent>>, defaultValue: ValueFromStateGetter<T, TContext, TEvent>): T;
|
|
6
9
|
//# sourceMappingURL=match.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Interpreter } from './interpreter';
|
|
2
|
+
export declare const provide: <T, TService extends Interpreter<any, any, any, {
|
|
3
|
+
value: any;
|
|
4
|
+
context: any;
|
|
5
|
+
}>>(service: TService | undefined, fn: (service: TService | undefined) => T) => T;
|
|
6
|
+
export declare const consume: <T, TService extends Interpreter<any, any, any, {
|
|
7
|
+
value: any;
|
|
8
|
+
context: any;
|
|
9
|
+
}>>(fn: (service: TService) => T) => T;
|
|
10
|
+
//# sourceMappingURL=serviceScope.d.ts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maintains a stack of the current service in scope.
|
|
3
|
+
* This is used to provide the correct service to spawn().
|
|
4
|
+
*/
|
|
5
|
+
var serviceStack = [];
|
|
6
|
+
|
|
7
|
+
var provide = function (service, fn) {
|
|
8
|
+
serviceStack.push(service);
|
|
9
|
+
var result = fn(service);
|
|
10
|
+
serviceStack.pop();
|
|
11
|
+
return result;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
var consume = function (fn) {
|
|
15
|
+
return fn(serviceStack[serviceStack.length - 1]);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { consume, provide };
|
package/es/stateUtils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EventObject, StateNode, StateValue } from '.';
|
|
2
2
|
declare type Configuration<TC, TE extends EventObject> = Iterable<StateNode<TC, any, TE>>;
|
|
3
3
|
declare type AdjList<TC, TE extends EventObject> = Map<StateNode<TC, any, TE>, Array<StateNode<TC, any, TE>>>;
|
|
4
|
-
export declare const isLeafNode: (stateNode: StateNode<any, any, any
|
|
4
|
+
export declare const isLeafNode: (stateNode: StateNode<any, any, any>) => boolean;
|
|
5
5
|
export declare function getChildren<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE>): Array<StateNode<TC, any, TE>>;
|
|
6
6
|
export declare function getAllStateNodes<TC, TE extends EventObject>(stateNode: StateNode<TC, any, TE>): Array<StateNode<TC, any, TE>>;
|
|
7
7
|
export declare function getConfiguration<TC, TE extends EventObject>(prevStateNodes: Iterable<StateNode<TC, any, TE>>, stateNodes: Iterable<StateNode<TC, any, TE>>): Iterable<StateNode<TC, any, TE>>;
|