xstate 4.20.1 → 4.23.1
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 +134 -0
- package/dist/xstate.interpreter.js +1 -1
- package/dist/xstate.js +1 -1
- package/dist/xstate.web.js +2 -2
- package/es/Actor.js +17 -5
- package/es/Machine.d.ts +6 -3
- package/es/Machine.js +3 -6
- package/es/State.js +1 -3
- package/es/StateNode.d.ts +4 -3
- package/es/StateNode.js +34 -20
- package/es/_virtual/_tslib.js +59 -73
- package/es/actionTypes.js +3 -2
- package/es/actions.d.ts +1 -1
- package/es/actions.js +51 -37
- package/es/behaviors.d.ts +37 -0
- package/es/behaviors.js +65 -0
- package/es/constants.js +2 -1
- package/es/devTools.js +1 -1
- package/es/environment.js +2 -1
- package/es/index.js +3 -1
- package/es/interpreter.d.ts +10 -3
- package/es/interpreter.js +44 -22
- package/es/invokeUtils.js +4 -3
- package/es/mapState.js +1 -1
- package/es/match.js +1 -1
- package/es/model.d.ts +2 -36
- package/es/model.types.d.ts +37 -0
- package/es/registry.js +2 -1
- package/es/scheduler.js +2 -1
- package/es/schema.js +1 -1
- package/es/serviceScope.js +1 -3
- package/es/stateUtils.js +1 -9
- package/es/types.d.ts +26 -5
- package/es/types.js +1 -1
- package/es/utils.d.ts +3 -2
- package/es/utils.js +4 -40
- package/lib/Actor.d.ts +25 -25
- package/lib/Actor.js +85 -66
- package/lib/Machine.d.ts +17 -14
- package/lib/Machine.js +14 -14
- package/lib/SimulatedClock.d.ts +16 -16
- package/lib/State.d.ts +108 -108
- package/lib/State.js +246 -236
- package/lib/StateNode.d.ts +279 -278
- package/lib/StateNode.js +1535 -1339
- package/lib/_virtual/_tslib.js +81 -0
- package/lib/actionTypes.d.ts +19 -19
- package/lib/actionTypes.js +43 -23
- package/lib/actions.d.ts +138 -138
- package/lib/actions.js +465 -387
- package/lib/behaviors.d.ts +37 -0
- package/lib/behaviors.js +69 -0
- package/lib/constants.d.ts +5 -5
- package/lib/constants.js +13 -7
- package/lib/devTools.d.ts +15 -15
- package/lib/devTools.js +37 -26
- package/lib/each.d.ts +3 -3
- package/lib/environment.d.ts +1 -1
- package/lib/environment.js +7 -4
- package/lib/index.d.ts +30 -30
- package/lib/index.js +67 -57
- package/lib/interpreter.d.ts +205 -198
- package/lib/interpreter.js +1306 -1054
- package/lib/invoke.d.ts +10 -10
- package/lib/invokeUtils.d.ts +6 -6
- package/lib/invokeUtils.js +40 -37
- package/lib/json.d.ts +30 -30
- package/lib/mapState.d.ts +3 -3
- package/lib/mapState.js +31 -32
- package/lib/match.d.ts +8 -8
- package/lib/match.js +33 -47
- package/lib/model.d.ts +4 -38
- package/lib/model.js +5 -1
- package/lib/model.types.d.ts +37 -0
- package/lib/model.types.js +2 -0
- package/lib/patterns.d.ts +13 -13
- package/lib/registry.d.ts +8 -8
- package/lib/registry.js +21 -18
- package/lib/scheduler.d.ts +16 -16
- package/lib/scheduler.js +79 -70
- package/lib/schema.d.ts +1 -1
- package/lib/schema.js +6 -4
- package/lib/scxml.d.ts +5 -5
- package/lib/serviceScope.d.ts +3 -3
- package/lib/serviceScope.js +16 -12
- package/lib/stateUtils.d.ts +14 -14
- package/lib/stateUtils.js +231 -199
- package/lib/types.d.ts +928 -907
- package/lib/types.js +29 -29
- package/lib/utils.d.ts +68 -67
- package/lib/utils.js +530 -529
- package/package.json +6 -6
package/es/actionTypes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ActionTypes } from './types.js';
|
|
1
|
+
import { ActionTypes } from './types.js';
|
|
2
2
|
|
|
3
3
|
var start = ActionTypes.Start;
|
|
4
4
|
var stop = ActionTypes.Stop;
|
|
@@ -18,4 +18,5 @@ var error = ActionTypes.ErrorCustom;
|
|
|
18
18
|
var update = ActionTypes.Update;
|
|
19
19
|
var choose = ActionTypes.Choose;
|
|
20
20
|
var pure = ActionTypes.Pure;
|
|
21
|
-
|
|
21
|
+
|
|
22
|
+
export { after, assign, cancel, choose, doneState, error, errorExecution, errorPlatform, init, invoke, log, nullEvent, pure, raise, send, start, stop, update };
|
package/es/actions.d.ts
CHANGED
|
@@ -135,5 +135,5 @@ export declare function forwardTo<TContext, TEvent extends EventObject>(target:
|
|
|
135
135
|
*/
|
|
136
136
|
export declare function escalate<TContext, TEvent extends EventObject, TErrorData = any>(errorData: TErrorData | ExprWithMeta<TContext, TEvent, TErrorData>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, AnyEventObject>;
|
|
137
137
|
export declare function choose<TContext, TEvent extends EventObject>(conds: Array<ChooseConditon<TContext, TEvent>>): ChooseAction<TContext, TEvent>;
|
|
138
|
-
export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any>, currentState: State<TContext, TEvent> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actions: Array<ActionObject<TContext, TEvent
|
|
138
|
+
export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent, any>, currentState: State<TContext, TEvent> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actions: Array<ActionObject<TContext, TEvent>>, preserveActionOrder?: boolean): [Array<ActionObject<TContext, TEvent>>, TContext];
|
|
139
139
|
//# sourceMappingURL=actions.d.ts.map
|
package/es/actions.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { __assign, __read } from './_virtual/_tslib.js';
|
|
1
|
+
import { __assign, __read, __spreadArray } from './_virtual/_tslib.js';
|
|
2
2
|
import { IS_PRODUCTION } from './environment.js';
|
|
3
|
-
import {
|
|
3
|
+
import { isFunction, toEventObject, getEventType, toSCXMLEvent, isString, partition, updateContext, flatten, toArray, toGuard, evaluateGuard, warn, isArray } from './utils.js';
|
|
4
4
|
import { SpecialTargets, ActionTypes } from './types.js';
|
|
5
5
|
import { send as send$1, update, assign as assign$1, init, raise as raise$1, log as log$1, cancel as cancel$1, error as error$1, stop as stop$1, pure as pure$1, choose as choose$1 } from './actionTypes.js';
|
|
6
|
+
|
|
6
7
|
var initEvent = /*#__PURE__*/toSCXMLEvent({
|
|
7
8
|
type: init
|
|
8
9
|
});
|
|
9
|
-
|
|
10
10
|
function getActionFunction(actionType, actionFunctionMap) {
|
|
11
11
|
return actionFunctionMap ? actionFunctionMap[actionType] || undefined : undefined;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
13
|
function toActionObject(action, actionFunctionMap) {
|
|
15
14
|
var actionObject;
|
|
16
15
|
|
|
@@ -62,7 +61,6 @@ function toActionObject(action, actionFunctionMap) {
|
|
|
62
61
|
});
|
|
63
62
|
return actionObject;
|
|
64
63
|
}
|
|
65
|
-
|
|
66
64
|
var toActionObjects = function (action, actionFunctionMap) {
|
|
67
65
|
if (!action) {
|
|
68
66
|
return [];
|
|
@@ -73,7 +71,6 @@ var toActionObjects = function (action, actionFunctionMap) {
|
|
|
73
71
|
return toActionObject(subAction, actionFunctionMap);
|
|
74
72
|
});
|
|
75
73
|
};
|
|
76
|
-
|
|
77
74
|
function toActivityDefinition(action) {
|
|
78
75
|
var actionObject = toActionObject(action);
|
|
79
76
|
return __assign(__assign({
|
|
@@ -89,7 +86,6 @@ function toActivityDefinition(action) {
|
|
|
89
86
|
* @param eventType The event to raise.
|
|
90
87
|
*/
|
|
91
88
|
|
|
92
|
-
|
|
93
89
|
function raise(event) {
|
|
94
90
|
if (!isString(event)) {
|
|
95
91
|
return send(event, {
|
|
@@ -102,7 +98,6 @@ function raise(event) {
|
|
|
102
98
|
event: event
|
|
103
99
|
};
|
|
104
100
|
}
|
|
105
|
-
|
|
106
101
|
function resolveRaise(action) {
|
|
107
102
|
return {
|
|
108
103
|
type: raise$1,
|
|
@@ -120,7 +115,6 @@ function resolveRaise(action) {
|
|
|
120
115
|
* - `to` - The target of this event (by default, the machine the event was sent from).
|
|
121
116
|
*/
|
|
122
117
|
|
|
123
|
-
|
|
124
118
|
function send(event, options) {
|
|
125
119
|
return {
|
|
126
120
|
to: options ? options.to : undefined,
|
|
@@ -130,7 +124,6 @@ function send(event, options) {
|
|
|
130
124
|
id: options && options.id !== undefined ? options.id : isFunction(event) ? event.name : getEventType(event)
|
|
131
125
|
};
|
|
132
126
|
}
|
|
133
|
-
|
|
134
127
|
function resolveSend(action, ctx, _event, delaysMap) {
|
|
135
128
|
var meta = {
|
|
136
129
|
_event: _event
|
|
@@ -161,7 +154,6 @@ function resolveSend(action, ctx, _event, delaysMap) {
|
|
|
161
154
|
* @param options Options to pass into the send event.
|
|
162
155
|
*/
|
|
163
156
|
|
|
164
|
-
|
|
165
157
|
function sendParent(event, options) {
|
|
166
158
|
return send(event, __assign(__assign({}, options), {
|
|
167
159
|
to: SpecialTargets.Parent
|
|
@@ -171,7 +163,6 @@ function sendParent(event, options) {
|
|
|
171
163
|
* Sends an update event to this machine's parent.
|
|
172
164
|
*/
|
|
173
165
|
|
|
174
|
-
|
|
175
166
|
function sendUpdate() {
|
|
176
167
|
return sendParent(update);
|
|
177
168
|
}
|
|
@@ -182,7 +173,6 @@ function sendUpdate() {
|
|
|
182
173
|
* @param options Options to pass into the send event
|
|
183
174
|
*/
|
|
184
175
|
|
|
185
|
-
|
|
186
176
|
function respond(event, options) {
|
|
187
177
|
return send(event, __assign(__assign({}, options), {
|
|
188
178
|
to: function (_, __, _a) {
|
|
@@ -219,7 +209,6 @@ function log(expr, label) {
|
|
|
219
209
|
expr: expr
|
|
220
210
|
};
|
|
221
211
|
}
|
|
222
|
-
|
|
223
212
|
var resolveLog = function (action, ctx, _event) {
|
|
224
213
|
return __assign(__assign({}, action), {
|
|
225
214
|
value: isString(action.expr) ? action.expr : action.expr(ctx, _event.data, {
|
|
@@ -235,7 +224,6 @@ var resolveLog = function (action, ctx, _event) {
|
|
|
235
224
|
* @param sendId The `id` of the `send(...)` action to cancel.
|
|
236
225
|
*/
|
|
237
226
|
|
|
238
|
-
|
|
239
227
|
var cancel = function (sendId) {
|
|
240
228
|
return {
|
|
241
229
|
type: cancel$1,
|
|
@@ -248,7 +236,6 @@ var cancel = function (sendId) {
|
|
|
248
236
|
* @param activity The activity to start.
|
|
249
237
|
*/
|
|
250
238
|
|
|
251
|
-
|
|
252
239
|
function start(activity) {
|
|
253
240
|
var activityDef = toActivityDefinition(activity);
|
|
254
241
|
return {
|
|
@@ -263,7 +250,6 @@ function start(activity) {
|
|
|
263
250
|
* @param actorRef The activity to stop.
|
|
264
251
|
*/
|
|
265
252
|
|
|
266
|
-
|
|
267
253
|
function stop(actorRef) {
|
|
268
254
|
var activity = isFunction(actorRef) ? actorRef : toActivityDefinition(actorRef);
|
|
269
255
|
return {
|
|
@@ -272,7 +258,6 @@ function stop(actorRef) {
|
|
|
272
258
|
exec: undefined
|
|
273
259
|
};
|
|
274
260
|
}
|
|
275
|
-
|
|
276
261
|
function resolveStop(action, context, _event) {
|
|
277
262
|
var actorRefOrString = isFunction(action.activity) ? action.activity(context, _event.data) : action.activity;
|
|
278
263
|
var resolvedActorRef = typeof actorRefOrString === 'string' ? {
|
|
@@ -290,7 +275,6 @@ function resolveStop(action, context, _event) {
|
|
|
290
275
|
* @param assignment An object that represents the partial context to update.
|
|
291
276
|
*/
|
|
292
277
|
|
|
293
|
-
|
|
294
278
|
var assign = function (assignment) {
|
|
295
279
|
return {
|
|
296
280
|
type: assign$1,
|
|
@@ -305,7 +289,6 @@ var assign = function (assignment) {
|
|
|
305
289
|
* @param id The state node ID where this event is handled
|
|
306
290
|
*/
|
|
307
291
|
|
|
308
|
-
|
|
309
292
|
function after(delayRef, id) {
|
|
310
293
|
var idSuffix = id ? "#" + id : '';
|
|
311
294
|
return ActionTypes.After + "(" + delayRef + ")" + idSuffix;
|
|
@@ -318,7 +301,6 @@ function after(delayRef, id) {
|
|
|
318
301
|
* @param data The data to pass into the event
|
|
319
302
|
*/
|
|
320
303
|
|
|
321
|
-
|
|
322
304
|
function done(id, data) {
|
|
323
305
|
var type = ActionTypes.DoneState + "." + id;
|
|
324
306
|
var eventObject = {
|
|
@@ -342,7 +324,6 @@ function done(id, data) {
|
|
|
342
324
|
* @param data The data to pass into the event
|
|
343
325
|
*/
|
|
344
326
|
|
|
345
|
-
|
|
346
327
|
function doneInvoke(id, data) {
|
|
347
328
|
var type = ActionTypes.DoneInvoke + "." + id;
|
|
348
329
|
var eventObject = {
|
|
@@ -356,7 +337,6 @@ function doneInvoke(id, data) {
|
|
|
356
337
|
|
|
357
338
|
return eventObject;
|
|
358
339
|
}
|
|
359
|
-
|
|
360
340
|
function error(id, data) {
|
|
361
341
|
var type = ActionTypes.ErrorPlatform + "." + id;
|
|
362
342
|
var eventObject = {
|
|
@@ -370,7 +350,6 @@ function error(id, data) {
|
|
|
370
350
|
|
|
371
351
|
return eventObject;
|
|
372
352
|
}
|
|
373
|
-
|
|
374
353
|
function pure(getActions) {
|
|
375
354
|
return {
|
|
376
355
|
type: ActionTypes.Pure,
|
|
@@ -384,7 +363,6 @@ function pure(getActions) {
|
|
|
384
363
|
* @param options Options to pass into the send action creator.
|
|
385
364
|
*/
|
|
386
365
|
|
|
387
|
-
|
|
388
366
|
function forwardTo(target, options) {
|
|
389
367
|
return send(function (_, event) {
|
|
390
368
|
return event;
|
|
@@ -400,7 +378,6 @@ function forwardTo(target, options) {
|
|
|
400
378
|
* @param options Options to pass into the send action creator.
|
|
401
379
|
*/
|
|
402
380
|
|
|
403
|
-
|
|
404
381
|
function escalate(errorData, options) {
|
|
405
382
|
return sendParent(function (context, event, meta) {
|
|
406
383
|
return {
|
|
@@ -411,22 +388,25 @@ function escalate(errorData, options) {
|
|
|
411
388
|
to: SpecialTargets.Parent
|
|
412
389
|
}));
|
|
413
390
|
}
|
|
414
|
-
|
|
415
391
|
function choose(conds) {
|
|
416
392
|
return {
|
|
417
393
|
type: ActionTypes.Choose,
|
|
418
394
|
conds: conds
|
|
419
395
|
};
|
|
420
396
|
}
|
|
397
|
+
function resolveActions(machine, currentState, currentContext, _event, actions, preserveActionOrder) {
|
|
398
|
+
if (preserveActionOrder === void 0) {
|
|
399
|
+
preserveActionOrder = false;
|
|
400
|
+
}
|
|
421
401
|
|
|
422
|
-
|
|
423
|
-
var _a = __read(partition(actions, function (action) {
|
|
402
|
+
var _a = __read(preserveActionOrder ? [[], actions] : partition(actions, function (action) {
|
|
424
403
|
return action.type === assign$1;
|
|
425
404
|
}), 2),
|
|
426
405
|
assignActions = _a[0],
|
|
427
406
|
otherActions = _a[1];
|
|
428
407
|
|
|
429
408
|
var updatedContext = assignActions.length ? updateContext(currentContext, _event, assignActions, currentState) : currentContext;
|
|
409
|
+
var preservedContexts = preserveActionOrder ? [currentContext] : undefined;
|
|
430
410
|
var resolvedActions = flatten(otherActions.map(function (actionObject) {
|
|
431
411
|
var _a;
|
|
432
412
|
|
|
@@ -460,9 +440,13 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
460
440
|
return [];
|
|
461
441
|
}
|
|
462
442
|
|
|
463
|
-
var
|
|
464
|
-
|
|
465
|
-
|
|
443
|
+
var _b = __read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
|
|
444
|
+
resolvedActionsFromChoose = _b[0],
|
|
445
|
+
resolvedContextFromChoose = _b[1];
|
|
446
|
+
|
|
447
|
+
updatedContext = resolvedContextFromChoose;
|
|
448
|
+
preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
|
|
449
|
+
return resolvedActionsFromChoose;
|
|
466
450
|
}
|
|
467
451
|
|
|
468
452
|
case pure$1:
|
|
@@ -473,9 +457,13 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
473
457
|
return [];
|
|
474
458
|
}
|
|
475
459
|
|
|
476
|
-
var
|
|
477
|
-
|
|
478
|
-
|
|
460
|
+
var _c = __read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
|
|
461
|
+
resolvedActionsFromPure = _c[0],
|
|
462
|
+
resolvedContext = _c[1];
|
|
463
|
+
|
|
464
|
+
updatedContext = resolvedContext;
|
|
465
|
+
preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
|
|
466
|
+
return resolvedActionsFromPure;
|
|
479
467
|
}
|
|
480
468
|
|
|
481
469
|
case stop$1:
|
|
@@ -483,11 +471,37 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
483
471
|
return resolveStop(actionObject, updatedContext, _event);
|
|
484
472
|
}
|
|
485
473
|
|
|
474
|
+
case assign$1:
|
|
475
|
+
{
|
|
476
|
+
updatedContext = updateContext(updatedContext, _event, [actionObject], currentState);
|
|
477
|
+
preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
|
|
486
481
|
default:
|
|
487
|
-
|
|
482
|
+
var resolvedActionObject = toActionObject(actionObject, machine.options.actions);
|
|
483
|
+
var exec_1 = resolvedActionObject.exec;
|
|
484
|
+
|
|
485
|
+
if (exec_1 && preservedContexts) {
|
|
486
|
+
var contextIndex_1 = preservedContexts.length - 1;
|
|
487
|
+
|
|
488
|
+
resolvedActionObject.exec = function (_ctx) {
|
|
489
|
+
var args = [];
|
|
490
|
+
|
|
491
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
492
|
+
args[_i - 1] = arguments[_i];
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
exec_1 === null || exec_1 === void 0 ? void 0 : exec_1.apply(void 0, __spreadArray([preservedContexts[contextIndex_1]], __read(args)));
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return resolvedActionObject;
|
|
488
500
|
}
|
|
501
|
+
}).filter(function (a) {
|
|
502
|
+
return !!a;
|
|
489
503
|
}));
|
|
490
504
|
return [resolvedActions, updatedContext];
|
|
491
505
|
}
|
|
492
506
|
|
|
493
|
-
export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, resolveStop, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
|
|
507
|
+
export { after, assign, cancel, choose, done, doneInvoke, error, escalate, forwardTo, getActionFunction, initEvent, log, pure, raise, resolveActions, resolveLog, resolveRaise, resolveSend, resolveStop, respond, send, sendParent, sendUpdate, start, stop, toActionObject, toActionObjects, toActivityDefinition };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ActorContext, ActorRef, Behavior, EventObject } from '.';
|
|
2
|
+
/**
|
|
3
|
+
* Returns an actor behavior from a reducer and its initial state.
|
|
4
|
+
*
|
|
5
|
+
* @param transition The pure reducer that returns the next state given the current state and event.
|
|
6
|
+
* @param initialState The initial state of the reducer.
|
|
7
|
+
* @returns An actor behavior
|
|
8
|
+
*/
|
|
9
|
+
export declare function fromReducer<TState, TEvent extends EventObject>(transition: (state: TState, event: TEvent, actorContext: ActorContext<TEvent, TState>) => TState, initialState: TState): Behavior<TEvent, TState>;
|
|
10
|
+
declare type PromiseEvents<T> = {
|
|
11
|
+
type: 'fulfill';
|
|
12
|
+
data: T;
|
|
13
|
+
} | {
|
|
14
|
+
type: 'reject';
|
|
15
|
+
error: unknown;
|
|
16
|
+
};
|
|
17
|
+
declare type PromiseState<T> = {
|
|
18
|
+
status: 'pending';
|
|
19
|
+
data: undefined;
|
|
20
|
+
error: undefined;
|
|
21
|
+
} | {
|
|
22
|
+
status: 'fulfilled';
|
|
23
|
+
data: T;
|
|
24
|
+
error: undefined;
|
|
25
|
+
} | {
|
|
26
|
+
status: 'rejected';
|
|
27
|
+
data: undefined;
|
|
28
|
+
error: any;
|
|
29
|
+
};
|
|
30
|
+
export declare function fromPromise<T>(promiseFn: () => Promise<T>): Behavior<PromiseEvents<T>, PromiseState<T>>;
|
|
31
|
+
interface SpawnBehaviorOptions {
|
|
32
|
+
id?: string;
|
|
33
|
+
parent?: ActorRef<any>;
|
|
34
|
+
}
|
|
35
|
+
export declare function spawnBehavior<TEvent extends EventObject, TEmitted>(behavior: Behavior<TEvent, TEmitted>, options?: SpawnBehaviorOptions): ActorRef<TEvent, TEmitted>;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=behaviors.d.ts.map
|
package/es/behaviors.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import './environment.js';
|
|
2
|
+
import { toObserver } from './utils.js';
|
|
3
|
+
import './types.js';
|
|
4
|
+
import './actionTypes.js';
|
|
5
|
+
import { toActorRef } from './Actor.js';
|
|
6
|
+
|
|
7
|
+
function spawnBehavior(behavior, options) {
|
|
8
|
+
if (options === void 0) {
|
|
9
|
+
options = {};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
var state = behavior.initialState;
|
|
13
|
+
var observers = new Set();
|
|
14
|
+
var mailbox = [];
|
|
15
|
+
var flushing = false;
|
|
16
|
+
|
|
17
|
+
var flush = function () {
|
|
18
|
+
if (flushing) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
flushing = true;
|
|
23
|
+
|
|
24
|
+
while (mailbox.length > 0) {
|
|
25
|
+
var event_1 = mailbox.shift();
|
|
26
|
+
state = behavior.transition(state, event_1, actorCtx);
|
|
27
|
+
observers.forEach(function (observer) {
|
|
28
|
+
return observer.next(state);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
flushing = false;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
var actor = toActorRef({
|
|
36
|
+
id: options.id,
|
|
37
|
+
send: function (event) {
|
|
38
|
+
mailbox.push(event);
|
|
39
|
+
flush();
|
|
40
|
+
},
|
|
41
|
+
getSnapshot: function () {
|
|
42
|
+
return state;
|
|
43
|
+
},
|
|
44
|
+
subscribe: function (next, handleError, complete) {
|
|
45
|
+
var observer = toObserver(next, handleError, complete);
|
|
46
|
+
observers.add(observer);
|
|
47
|
+
observer.next(state);
|
|
48
|
+
return {
|
|
49
|
+
unsubscribe: function () {
|
|
50
|
+
observers.delete(observer);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
var actorCtx = {
|
|
56
|
+
parent: options.parent,
|
|
57
|
+
self: actor,
|
|
58
|
+
id: options.id || 'anonymous',
|
|
59
|
+
observers: observers
|
|
60
|
+
};
|
|
61
|
+
state = behavior.start ? behavior.start(actorCtx) : state;
|
|
62
|
+
return actor;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { spawnBehavior };
|
package/es/constants.js
CHANGED
|
@@ -2,4 +2,5 @@ var STATE_DELIMITER = '.';
|
|
|
2
2
|
var EMPTY_ACTIVITY_MAP = {};
|
|
3
3
|
var DEFAULT_GUARD_TYPE = 'xstate.guard';
|
|
4
4
|
var TARGETLESS_KEY = '';
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
export { DEFAULT_GUARD_TYPE, EMPTY_ACTIVITY_MAP, STATE_DELIMITER, TARGETLESS_KEY };
|
package/es/devTools.js
CHANGED
package/es/environment.js
CHANGED
package/es/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { Machine, createMachine } from './Machine.js';
|
|
|
9
9
|
export { Interpreter, InterpreterStatus, interpret, spawn } from './interpreter.js';
|
|
10
10
|
export { matchState } from './match.js';
|
|
11
11
|
export { createSchema } from './schema.js';
|
|
12
|
+
|
|
12
13
|
var actions = {
|
|
13
14
|
raise: raise,
|
|
14
15
|
send: send,
|
|
@@ -27,4 +28,5 @@ var actions = {
|
|
|
27
28
|
choose: choose,
|
|
28
29
|
pure: pure
|
|
29
30
|
};
|
|
30
|
-
|
|
31
|
+
|
|
32
|
+
export { actions };
|
package/es/interpreter.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
|
|
1
|
+
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, Subscribable, DoneEvent, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate, ActorRef, ActorRefFrom, Behavior, Subscription } from './types';
|
|
2
2
|
import { State } from './State';
|
|
3
|
-
import { ActorRef, ActorRefFrom, Subscription } from '.';
|
|
4
3
|
export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
|
|
5
4
|
value: any;
|
|
6
5
|
context: TContext;
|
|
@@ -22,6 +21,11 @@ export declare enum InterpreterStatus {
|
|
|
22
21
|
Running = 1,
|
|
23
22
|
Stopped = 2
|
|
24
23
|
}
|
|
24
|
+
declare global {
|
|
25
|
+
interface SymbolConstructor {
|
|
26
|
+
readonly observable: symbol;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
25
29
|
export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
|
|
26
30
|
value: any;
|
|
27
31
|
context: TContext;
|
|
@@ -86,8 +90,8 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
86
90
|
execute(state: State<TContext, TEvent, TStateSchema, TTypestate>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
|
|
87
91
|
private update;
|
|
88
92
|
onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate>): this;
|
|
89
|
-
subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Subscription;
|
|
90
93
|
subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
|
|
94
|
+
subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Subscription;
|
|
91
95
|
/**
|
|
92
96
|
* Adds an event listener that is notified whenever an event is sent to the running interpreter.
|
|
93
97
|
* @param listener The event listener
|
|
@@ -171,6 +175,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
171
175
|
autoForward?: boolean;
|
|
172
176
|
sync?: boolean;
|
|
173
177
|
}): ActorRef<TChildEvent, State<TChildContext, TChildEvent>>;
|
|
178
|
+
private spawnBehavior;
|
|
174
179
|
private spawnPromise;
|
|
175
180
|
private spawnCallback;
|
|
176
181
|
private spawnObservable;
|
|
@@ -181,8 +186,10 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
181
186
|
toJSON(): {
|
|
182
187
|
id: string;
|
|
183
188
|
};
|
|
189
|
+
[Symbol.observable](): Subscribable<State<TContext, TEvent, TStateSchema, TTypestate>>;
|
|
184
190
|
getSnapshot(): State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
185
191
|
}
|
|
192
|
+
export declare function spawn<T extends Behavior<any, any>>(entity: T, nameOrOptions?: string | SpawnOptions): ActorRefFrom<T>;
|
|
186
193
|
export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions): ActorRefFrom<StateMachine<TC, any, TE>>;
|
|
187
194
|
export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions): ActorRef<any>;
|
|
188
195
|
/**
|
package/es/interpreter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { __values, __assign, __spreadArray, __read } from './_virtual/_tslib.js';
|
|
2
2
|
import { IS_PRODUCTION } from './environment.js';
|
|
3
|
-
import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isPromiseLike, isObservable,
|
|
3
|
+
import { warn, mapContext, isFunction, keys, toSCXMLEvent, toInvokeSource, isMachine, isPromiseLike, isObservable, isBehavior, reportUnhandledExceptionOnInvocation, symbolObservable, isArray, toEventObject, isString, isActor, uniqueId, toObserver } 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';
|
|
@@ -11,6 +11,8 @@ import { isSpawnedActor, createDeferredActor } from './Actor.js';
|
|
|
11
11
|
import { Scheduler } from './scheduler.js';
|
|
12
12
|
import { registry } from './registry.js';
|
|
13
13
|
import { registerService, getGlobal } from './devTools.js';
|
|
14
|
+
import { spawnBehavior } from './behaviors.js';
|
|
15
|
+
|
|
14
16
|
var DEFAULT_SPAWN_OPTIONS = {
|
|
15
17
|
sync: false,
|
|
16
18
|
autoForward: false
|
|
@@ -830,24 +832,32 @@ function () {
|
|
|
830
832
|
}
|
|
831
833
|
|
|
832
834
|
var resolvedData = data ? mapContext(data, context, _event) : undefined;
|
|
835
|
+
|
|
836
|
+
if (typeof serviceCreator === 'string') {
|
|
837
|
+
// TODO: warn
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
|
|
833
841
|
var source = isFunction(serviceCreator) ? serviceCreator(context, _event.data, {
|
|
834
842
|
data: resolvedData,
|
|
835
843
|
src: invokeSource
|
|
836
844
|
}) : serviceCreator;
|
|
837
845
|
|
|
838
|
-
if (
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
846
|
+
if (!source) {
|
|
847
|
+
// TODO: warn?
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
var options = void 0;
|
|
852
|
+
|
|
853
|
+
if (isMachine(source)) {
|
|
854
|
+
source = resolvedData ? source.withContext(resolvedData) : source;
|
|
855
|
+
options = {
|
|
848
856
|
autoForward: autoForward
|
|
849
|
-
}
|
|
850
|
-
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
this.spawn(source, id, options);
|
|
851
861
|
} else {
|
|
852
862
|
this.spawnActivity(activity);
|
|
853
863
|
}
|
|
@@ -910,13 +920,15 @@ function () {
|
|
|
910
920
|
} else if (isFunction(entity)) {
|
|
911
921
|
return this.spawnCallback(entity, name);
|
|
912
922
|
} else if (isSpawnedActor(entity)) {
|
|
913
|
-
return this.spawnActor(entity);
|
|
923
|
+
return this.spawnActor(entity, name);
|
|
914
924
|
} else if (isObservable(entity)) {
|
|
915
925
|
return this.spawnObservable(entity, name);
|
|
916
926
|
} else if (isMachine(entity)) {
|
|
917
927
|
return this.spawnMachine(entity, __assign(__assign({}, options), {
|
|
918
928
|
id: name
|
|
919
929
|
}));
|
|
930
|
+
} else if (isBehavior(entity)) {
|
|
931
|
+
return this.spawnBehavior(entity, name);
|
|
920
932
|
} else {
|
|
921
933
|
throw new Error("Unable to spawn entity \"" + name + "\" of type \"" + typeof entity + "\".");
|
|
922
934
|
}
|
|
@@ -962,11 +974,20 @@ function () {
|
|
|
962
974
|
return actor;
|
|
963
975
|
};
|
|
964
976
|
|
|
977
|
+
Interpreter.prototype.spawnBehavior = function (behavior, id) {
|
|
978
|
+
var actorRef = spawnBehavior(behavior, {
|
|
979
|
+
id: id,
|
|
980
|
+
parent: this
|
|
981
|
+
});
|
|
982
|
+
this.children.set(id, actorRef);
|
|
983
|
+
return actorRef;
|
|
984
|
+
};
|
|
985
|
+
|
|
965
986
|
Interpreter.prototype.spawnPromise = function (promise, id) {
|
|
966
987
|
var _this = this;
|
|
967
988
|
|
|
968
989
|
var canceled = false;
|
|
969
|
-
var resolvedData
|
|
990
|
+
var resolvedData;
|
|
970
991
|
promise.then(function (response) {
|
|
971
992
|
if (!canceled) {
|
|
972
993
|
resolvedData = response;
|
|
@@ -1060,7 +1081,7 @@ function () {
|
|
|
1060
1081
|
var canceled = false;
|
|
1061
1082
|
var receivers = new Set();
|
|
1062
1083
|
var listeners = new Set();
|
|
1063
|
-
var emitted
|
|
1084
|
+
var emitted;
|
|
1064
1085
|
|
|
1065
1086
|
var receive = function (e) {
|
|
1066
1087
|
emitted = e;
|
|
@@ -1131,7 +1152,7 @@ function () {
|
|
|
1131
1152
|
Interpreter.prototype.spawnObservable = function (source, id) {
|
|
1132
1153
|
var _this = this;
|
|
1133
1154
|
|
|
1134
|
-
var emitted
|
|
1155
|
+
var emitted;
|
|
1135
1156
|
var subscription = source.subscribe(function (value) {
|
|
1136
1157
|
emitted = value;
|
|
1137
1158
|
|
|
@@ -1175,8 +1196,8 @@ function () {
|
|
|
1175
1196
|
return actor;
|
|
1176
1197
|
};
|
|
1177
1198
|
|
|
1178
|
-
Interpreter.prototype.spawnActor = function (actor) {
|
|
1179
|
-
this.children.set(
|
|
1199
|
+
Interpreter.prototype.spawnActor = function (actor, name) {
|
|
1200
|
+
this.children.set(name, actor);
|
|
1180
1201
|
return actor;
|
|
1181
1202
|
};
|
|
1182
1203
|
|
|
@@ -1260,7 +1281,9 @@ function () {
|
|
|
1260
1281
|
|
|
1261
1282
|
Interpreter.prototype[symbolObservable] = function () {
|
|
1262
1283
|
return this;
|
|
1263
|
-
};
|
|
1284
|
+
}; // this gets stripped by Babel to avoid having "undefined" property in environments without this non-standard Symbol
|
|
1285
|
+
// it has to be here to be included in the generated .d.ts
|
|
1286
|
+
|
|
1264
1287
|
|
|
1265
1288
|
Interpreter.prototype.getSnapshot = function () {
|
|
1266
1289
|
if (this.status === InterpreterStatus.NotStarted) {
|
|
@@ -1332,10 +1355,9 @@ function spawn(entity, nameOrOptions) {
|
|
|
1332
1355
|
* @param options Interpreter options
|
|
1333
1356
|
*/
|
|
1334
1357
|
|
|
1335
|
-
|
|
1336
1358
|
function interpret(machine, options) {
|
|
1337
1359
|
var interpreter = new Interpreter(machine, options);
|
|
1338
1360
|
return interpreter;
|
|
1339
1361
|
}
|
|
1340
1362
|
|
|
1341
|
-
export { Interpreter, InterpreterStatus, interpret, spawn };
|
|
1363
|
+
export { Interpreter, InterpreterStatus, interpret, spawn };
|
package/es/invokeUtils.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { __assign, __rest } from './_virtual/_tslib.js';
|
|
2
|
+
import './environment.js';
|
|
3
|
+
import './utils.js';
|
|
4
|
+
import './types.js';
|
|
2
5
|
import { invoke } from './actionTypes.js';
|
|
3
|
-
import './actions.js';
|
|
4
6
|
|
|
5
7
|
function toInvokeSource(src) {
|
|
6
8
|
if (typeof src === 'string') {
|
|
@@ -18,7 +20,6 @@ function toInvokeSource(src) {
|
|
|
18
20
|
|
|
19
21
|
return src;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
23
|
function toInvokeDefinition(invokeConfig) {
|
|
23
24
|
return __assign(__assign({
|
|
24
25
|
type: invoke
|
|
@@ -36,4 +37,4 @@ function toInvokeDefinition(invokeConfig) {
|
|
|
36
37
|
});
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
export { toInvokeDefinition, toInvokeSource };
|
|
40
|
+
export { toInvokeDefinition, toInvokeSource };
|
package/es/mapState.js
CHANGED