xstate 4.13.0 → 4.15.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 +76 -0
- package/LICENSE +22 -0
- 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 +3 -1
- package/es/Actor.js +5 -1
- package/es/State.d.ts +2 -3
- package/es/StateNode.js +3 -2
- package/es/actions.d.ts +7 -3
- package/es/actions.js +23 -6
- package/es/index.d.ts +2 -2
- package/es/index.js +1 -1
- package/es/interpreter.d.ts +15 -9
- package/es/interpreter.js +65 -42
- package/es/match.d.ts +4 -1
- package/es/types.d.ts +58 -14
- package/es/utils.d.ts +2 -1
- package/es/utils.js +21 -1
- package/lib/Actor.d.ts +3 -1
- package/lib/Actor.js +5 -1
- package/lib/State.d.ts +2 -3
- package/lib/StateNode.js +2 -2
- package/lib/actions.d.ts +7 -3
- package/lib/actions.js +32 -9
- package/lib/index.d.ts +2 -2
- package/lib/index.js +3 -2
- package/lib/interpreter.d.ts +15 -9
- package/lib/interpreter.js +57 -39
- package/lib/match.d.ts +4 -1
- package/lib/serviceScope.js +4 -2
- package/lib/stateUtils.js +2 -1
- package/lib/types.d.ts +58 -14
- package/lib/utils.d.ts +2 -1
- package/lib/utils.js +15 -2
- package/package.json +4 -4
package/lib/actions.js
CHANGED
|
@@ -27,7 +27,7 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
27
27
|
return ar;
|
|
28
28
|
};
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.resolveActions = exports.choose = exports.escalate = exports.forwardTo = exports.pure = exports.error = exports.doneInvoke = exports.done = exports.after = exports.isActionObject = exports.assign = exports.stop = exports.start = exports.cancel = exports.resolveLog = exports.log = exports.respond = exports.sendUpdate = exports.sendParent = exports.resolveSend = exports.send = exports.resolveRaise = exports.raise = exports.toActivityDefinition = exports.toActionObjects = exports.toActionObject = exports.getActionFunction = exports.initEvent = exports.actionTypes = void 0;
|
|
30
|
+
exports.resolveActions = exports.choose = exports.escalate = exports.forwardTo = exports.pure = exports.error = exports.doneInvoke = exports.done = exports.after = exports.isActionObject = exports.assign = exports.resolveStop = exports.stop = exports.start = exports.cancel = exports.resolveLog = exports.log = exports.respond = exports.sendUpdate = exports.sendParent = exports.resolveSend = exports.send = exports.resolveRaise = exports.raise = exports.toActivityDefinition = exports.toActionObjects = exports.toActionObject = exports.getActionFunction = exports.initEvent = exports.actionTypes = void 0;
|
|
31
31
|
var types_1 = require("./types");
|
|
32
32
|
var actionTypes = require("./actionTypes");
|
|
33
33
|
exports.actionTypes = actionTypes;
|
|
@@ -85,7 +85,7 @@ function toActionObject(action, actionFunctionMap) {
|
|
|
85
85
|
return actionObject;
|
|
86
86
|
}
|
|
87
87
|
exports.toActionObject = toActionObject;
|
|
88
|
-
|
|
88
|
+
var toActionObjects = function (action, actionFunctionMap) {
|
|
89
89
|
if (!action) {
|
|
90
90
|
return [];
|
|
91
91
|
}
|
|
@@ -94,6 +94,7 @@ exports.toActionObjects = function (action, actionFunctionMap) {
|
|
|
94
94
|
return toActionObject(subAction, actionFunctionMap);
|
|
95
95
|
});
|
|
96
96
|
};
|
|
97
|
+
exports.toActionObjects = toActionObjects;
|
|
97
98
|
function toActivityDefinition(action) {
|
|
98
99
|
var actionObject = toActionObject(action);
|
|
99
100
|
return __assign(__assign({ id: utils_1.isString(action) ? action : actionObject.id }, actionObject), { type: actionObject.type });
|
|
@@ -223,11 +224,12 @@ function log(expr, label) {
|
|
|
223
224
|
};
|
|
224
225
|
}
|
|
225
226
|
exports.log = log;
|
|
226
|
-
|
|
227
|
+
var resolveLog = function (action, ctx, _event) { return (__assign(__assign({}, action), { value: utils_1.isString(action.expr)
|
|
227
228
|
? action.expr
|
|
228
229
|
: action.expr(ctx, _event.data, {
|
|
229
230
|
_event: _event
|
|
230
231
|
}) })); };
|
|
232
|
+
exports.resolveLog = resolveLog;
|
|
231
233
|
/**
|
|
232
234
|
* Cancels an in-flight `send(...)` action. A canceled sent action will not
|
|
233
235
|
* be executed, nor will its event be sent, unless it has already been sent
|
|
@@ -235,12 +237,13 @@ exports.resolveLog = function (action, ctx, _event) { return (__assign(__assign(
|
|
|
235
237
|
*
|
|
236
238
|
* @param sendId The `id` of the `send(...)` action to cancel.
|
|
237
239
|
*/
|
|
238
|
-
|
|
240
|
+
var cancel = function (sendId) {
|
|
239
241
|
return {
|
|
240
242
|
type: actionTypes.cancel,
|
|
241
243
|
sendId: sendId
|
|
242
244
|
};
|
|
243
245
|
};
|
|
246
|
+
exports.cancel = cancel;
|
|
244
247
|
/**
|
|
245
248
|
* Starts an activity.
|
|
246
249
|
*
|
|
@@ -258,28 +261,45 @@ exports.start = start;
|
|
|
258
261
|
/**
|
|
259
262
|
* Stops an activity.
|
|
260
263
|
*
|
|
261
|
-
* @param
|
|
264
|
+
* @param actorRef The activity to stop.
|
|
262
265
|
*/
|
|
263
|
-
function stop(
|
|
264
|
-
var
|
|
266
|
+
function stop(actorRef) {
|
|
267
|
+
var activity = utils_1.isFunction(actorRef)
|
|
268
|
+
? actorRef
|
|
269
|
+
: toActivityDefinition(actorRef);
|
|
265
270
|
return {
|
|
266
271
|
type: types_1.ActionTypes.Stop,
|
|
267
|
-
activity:
|
|
272
|
+
activity: activity,
|
|
268
273
|
exec: undefined
|
|
269
274
|
};
|
|
270
275
|
}
|
|
271
276
|
exports.stop = stop;
|
|
277
|
+
function resolveStop(action, context, _event) {
|
|
278
|
+
var actorRefOrString = utils_1.isFunction(action.activity)
|
|
279
|
+
? action.activity(context, _event.data)
|
|
280
|
+
: action.activity;
|
|
281
|
+
var resolvedActorRef = typeof actorRefOrString === 'string'
|
|
282
|
+
? { id: actorRefOrString }
|
|
283
|
+
: actorRefOrString;
|
|
284
|
+
var actionObject = {
|
|
285
|
+
type: types_1.ActionTypes.Stop,
|
|
286
|
+
activity: resolvedActorRef
|
|
287
|
+
};
|
|
288
|
+
return actionObject;
|
|
289
|
+
}
|
|
290
|
+
exports.resolveStop = resolveStop;
|
|
272
291
|
/**
|
|
273
292
|
* Updates the current context of the machine.
|
|
274
293
|
*
|
|
275
294
|
* @param assignment An object that represents the partial context to update.
|
|
276
295
|
*/
|
|
277
|
-
|
|
296
|
+
var assign = function (assignment) {
|
|
278
297
|
return {
|
|
279
298
|
type: actionTypes.assign,
|
|
280
299
|
assignment: assignment
|
|
281
300
|
};
|
|
282
301
|
};
|
|
302
|
+
exports.assign = assign;
|
|
283
303
|
function isActionObject(action) {
|
|
284
304
|
return typeof action === 'object' && 'type' in action;
|
|
285
305
|
}
|
|
@@ -428,6 +448,9 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
428
448
|
updatedContext = resolved[1];
|
|
429
449
|
return resolved[0];
|
|
430
450
|
}
|
|
451
|
+
case actionTypes.stop: {
|
|
452
|
+
return resolveStop(actionObject, updatedContext, _event);
|
|
453
|
+
}
|
|
431
454
|
default:
|
|
432
455
|
return toActionObject(actionObject, machine.options.actions);
|
|
433
456
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { State } from './State';
|
|
|
5
5
|
import { Machine, createMachine } from './Machine';
|
|
6
6
|
import { Actor } from './Actor';
|
|
7
7
|
import { raise, send, sendParent, sendUpdate, log, start, stop, assign, after, done, respond, doneInvoke, forwardTo, escalate, choose, pure } from './actions';
|
|
8
|
-
import { interpret, Interpreter, spawn } from './interpreter';
|
|
8
|
+
import { interpret, Interpreter, spawn, InterpreterStatus } from './interpreter';
|
|
9
9
|
import { matchState } from './match';
|
|
10
10
|
declare const actions: {
|
|
11
11
|
raise: typeof raise;
|
|
@@ -25,6 +25,6 @@ declare const actions: {
|
|
|
25
25
|
choose: typeof choose;
|
|
26
26
|
pure: typeof pure;
|
|
27
27
|
};
|
|
28
|
-
export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, matchState, spawn, doneInvoke, createMachine };
|
|
28
|
+
export { Actor, Machine, StateNode, State, matchesState, mapState, actions, assign, send, sendParent, sendUpdate, forwardTo, interpret, Interpreter, InterpreterStatus, matchState, spawn, doneInvoke, createMachine };
|
|
29
29
|
export * from './types';
|
|
30
30
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -7,10 +7,10 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
9
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.createMachine = exports.doneInvoke = exports.spawn = exports.matchState = exports.Interpreter = exports.interpret = exports.forwardTo = exports.sendUpdate = exports.sendParent = exports.send = exports.assign = exports.actions = exports.mapState = exports.matchesState = exports.State = exports.StateNode = exports.Machine = void 0;
|
|
13
|
+
exports.createMachine = exports.doneInvoke = exports.spawn = exports.matchState = exports.InterpreterStatus = exports.Interpreter = exports.interpret = exports.forwardTo = exports.sendUpdate = exports.sendParent = exports.send = exports.assign = exports.actions = exports.mapState = exports.matchesState = exports.State = exports.StateNode = exports.Machine = void 0;
|
|
14
14
|
var utils_1 = require("./utils");
|
|
15
15
|
Object.defineProperty(exports, "matchesState", { enumerable: true, get: function () { return utils_1.matchesState; } });
|
|
16
16
|
var mapState_1 = require("./mapState");
|
|
@@ -33,6 +33,7 @@ var interpreter_1 = require("./interpreter");
|
|
|
33
33
|
Object.defineProperty(exports, "interpret", { enumerable: true, get: function () { return interpreter_1.interpret; } });
|
|
34
34
|
Object.defineProperty(exports, "Interpreter", { enumerable: true, get: function () { return interpreter_1.Interpreter; } });
|
|
35
35
|
Object.defineProperty(exports, "spawn", { enumerable: true, get: function () { return interpreter_1.spawn; } });
|
|
36
|
+
Object.defineProperty(exports, "InterpreterStatus", { enumerable: true, get: function () { return interpreter_1.InterpreterStatus; } });
|
|
36
37
|
var match_1 = require("./match");
|
|
37
38
|
Object.defineProperty(exports, "matchState", { enumerable: true, get: function () { return match_1.matchState; } });
|
|
38
39
|
var actions = {
|
package/lib/interpreter.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent,
|
|
1
|
+
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
|
|
2
2
|
import { State } from './State';
|
|
3
3
|
import { Actor } from './Actor';
|
|
4
|
+
import { ActorRefFrom, SpawnedActorRef, Subscription } from '.';
|
|
4
5
|
export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
|
|
5
6
|
value: any;
|
|
6
7
|
context: TContext;
|
|
@@ -17,6 +18,11 @@ interface SpawnOptions {
|
|
|
17
18
|
autoForward?: boolean;
|
|
18
19
|
sync?: boolean;
|
|
19
20
|
}
|
|
21
|
+
export declare enum InterpreterStatus {
|
|
22
|
+
NotStarted = 0,
|
|
23
|
+
Running = 1,
|
|
24
|
+
Stopped = 2
|
|
25
|
+
}
|
|
20
26
|
export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
|
|
21
27
|
value: any;
|
|
22
28
|
context: TContext;
|
|
@@ -52,14 +58,14 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
52
58
|
* Whether the service is started.
|
|
53
59
|
*/
|
|
54
60
|
initialized: boolean;
|
|
55
|
-
|
|
61
|
+
status: InterpreterStatus;
|
|
56
62
|
parent?: Interpreter<any>;
|
|
57
63
|
id: string;
|
|
58
64
|
/**
|
|
59
65
|
* The globally unique process ID for this invocation.
|
|
60
66
|
*/
|
|
61
67
|
sessionId: string;
|
|
62
|
-
children: Map<string | number,
|
|
68
|
+
children: Map<string | number, SpawnedActorRef<any>>;
|
|
63
69
|
private forwardTo;
|
|
64
70
|
private devTools?;
|
|
65
71
|
/**
|
|
@@ -81,8 +87,8 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
81
87
|
execute(state: State<TContext, TEvent, TStateSchema, TTypestate>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
|
|
82
88
|
private update;
|
|
83
89
|
onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate>): this;
|
|
84
|
-
subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>):
|
|
85
|
-
subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void):
|
|
90
|
+
subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Subscription;
|
|
91
|
+
subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
|
|
86
92
|
/**
|
|
87
93
|
* Adds an event listener that is notified whenever an event is sent to the running interpreter.
|
|
88
94
|
* @param listener The event listener
|
|
@@ -160,12 +166,12 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
160
166
|
private exec;
|
|
161
167
|
private removeChild;
|
|
162
168
|
private stopChild;
|
|
163
|
-
spawn(entity: Spawnable, name: string, options?: SpawnOptions):
|
|
169
|
+
spawn(entity: Spawnable, name: string, options?: SpawnOptions): SpawnedActorRef<any>;
|
|
164
170
|
spawnMachine<TChildContext, TChildStateSchema, TChildEvent extends EventObject>(machine: StateMachine<TChildContext, TChildStateSchema, TChildEvent>, options?: {
|
|
165
171
|
id?: string;
|
|
166
172
|
autoForward?: boolean;
|
|
167
173
|
sync?: boolean;
|
|
168
|
-
}):
|
|
174
|
+
}): SpawnedActorRef<TChildEvent, State<TChildContext, TChildEvent>>;
|
|
169
175
|
private spawnPromise;
|
|
170
176
|
private spawnCallback;
|
|
171
177
|
private spawnObservable;
|
|
@@ -177,8 +183,8 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
177
183
|
id: string;
|
|
178
184
|
};
|
|
179
185
|
}
|
|
180
|
-
export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions):
|
|
181
|
-
export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions):
|
|
186
|
+
export declare function spawn<TC, TE extends EventObject>(entity: StateMachine<TC, any, TE>, nameOrOptions?: string | SpawnOptions): ActorRefFrom<StateMachine<TC, any, TE>>;
|
|
187
|
+
export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnOptions): SpawnedActorRef<any>;
|
|
182
188
|
/**
|
|
183
189
|
* Creates a new Interpreter instance for the given machine with the provided options, if any.
|
|
184
190
|
*
|
package/lib/interpreter.js
CHANGED
|
@@ -42,7 +42,7 @@ var __spread = (this && this.__spread) || function () {
|
|
|
42
42
|
return ar;
|
|
43
43
|
};
|
|
44
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
-
exports.interpret = exports.spawn = exports.Interpreter = void 0;
|
|
45
|
+
exports.interpret = exports.spawn = exports.Interpreter = exports.InterpreterStatus = void 0;
|
|
46
46
|
var types_1 = require("./types");
|
|
47
47
|
var State_1 = require("./State");
|
|
48
48
|
var actionTypes = require("./actionTypes");
|
|
@@ -61,7 +61,7 @@ var InterpreterStatus;
|
|
|
61
61
|
InterpreterStatus[InterpreterStatus["NotStarted"] = 0] = "NotStarted";
|
|
62
62
|
InterpreterStatus[InterpreterStatus["Running"] = 1] = "Running";
|
|
63
63
|
InterpreterStatus[InterpreterStatus["Stopped"] = 2] = "Stopped";
|
|
64
|
-
})(InterpreterStatus || (InterpreterStatus = {}));
|
|
64
|
+
})(InterpreterStatus = exports.InterpreterStatus || (exports.InterpreterStatus = {}));
|
|
65
65
|
var Interpreter = /** @class */ (function () {
|
|
66
66
|
/**
|
|
67
67
|
* Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
|
|
@@ -85,7 +85,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
85
85
|
* Whether the service is started.
|
|
86
86
|
*/
|
|
87
87
|
this.initialized = false;
|
|
88
|
-
this.
|
|
88
|
+
this.status = InterpreterStatus.NotStarted;
|
|
89
89
|
this.children = new Map();
|
|
90
90
|
this.forwardTo = new Set();
|
|
91
91
|
/**
|
|
@@ -107,21 +107,15 @@ var Interpreter = /** @class */ (function () {
|
|
|
107
107
|
return _this.state;
|
|
108
108
|
}
|
|
109
109
|
var _event = utils_1.toSCXMLEvent(utils_1.toEventObject(event, payload));
|
|
110
|
-
if (_this.
|
|
110
|
+
if (_this.status === InterpreterStatus.Stopped) {
|
|
111
111
|
// do nothing
|
|
112
112
|
if (!environment_1.IS_PRODUCTION) {
|
|
113
113
|
utils_1.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));
|
|
114
114
|
}
|
|
115
115
|
return _this.state;
|
|
116
116
|
}
|
|
117
|
-
if (_this.
|
|
118
|
-
_this.options.deferEvents) {
|
|
119
|
-
// tslint:disable-next-line:no-console
|
|
120
|
-
if (!environment_1.IS_PRODUCTION) {
|
|
121
|
-
utils_1.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));
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
else if (_this._status !== InterpreterStatus.Running) {
|
|
117
|
+
if (_this.status !== InterpreterStatus.Running &&
|
|
118
|
+
!_this.options.deferEvents) {
|
|
125
119
|
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));
|
|
126
120
|
}
|
|
127
121
|
_this.scheduler.schedule(function () {
|
|
@@ -137,9 +131,11 @@ var Interpreter = /** @class */ (function () {
|
|
|
137
131
|
var isParent = _this.parent && (to === types_1.SpecialTargets.Parent || _this.parent.id === to);
|
|
138
132
|
var target = isParent
|
|
139
133
|
? _this.parent
|
|
140
|
-
:
|
|
141
|
-
? to
|
|
142
|
-
:
|
|
134
|
+
: utils_1.isString(to)
|
|
135
|
+
? _this.children.get(to) || registry_1.registry.get(to)
|
|
136
|
+
: utils_1.isActor(to)
|
|
137
|
+
? to
|
|
138
|
+
: undefined;
|
|
143
139
|
if (!target) {
|
|
144
140
|
if (!isParent) {
|
|
145
141
|
throw new Error("Unable to send event to child '" + to + "' from service '" + _this.id + "'.");
|
|
@@ -189,7 +185,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
189
185
|
Object.defineProperty(Interpreter.prototype, "state", {
|
|
190
186
|
get: function () {
|
|
191
187
|
if (!environment_1.IS_PRODUCTION) {
|
|
192
|
-
utils_1.warn(this.
|
|
188
|
+
utils_1.warn(this.status !== InterpreterStatus.NotStarted, "Attempted to read state from uninitialized service '" + this.id + "'. Make sure the service is started first.");
|
|
193
189
|
}
|
|
194
190
|
return this._state;
|
|
195
191
|
},
|
|
@@ -229,6 +225,10 @@ var Interpreter = /** @class */ (function () {
|
|
|
229
225
|
if (this.options.execute) {
|
|
230
226
|
this.execute(this.state);
|
|
231
227
|
}
|
|
228
|
+
// Update children
|
|
229
|
+
this.children.forEach(function (child) {
|
|
230
|
+
_this.state.children[child.id] = child;
|
|
231
|
+
});
|
|
232
232
|
// Dev tools
|
|
233
233
|
if (this.devTools) {
|
|
234
234
|
this.devTools.send(_event.data, state);
|
|
@@ -307,7 +307,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
307
307
|
Interpreter.prototype.onTransition = function (listener) {
|
|
308
308
|
this.listeners.add(listener);
|
|
309
309
|
// Send current state to listener
|
|
310
|
-
if (this.
|
|
310
|
+
if (this.status === InterpreterStatus.Running) {
|
|
311
311
|
listener(this.state, this.state.event);
|
|
312
312
|
}
|
|
313
313
|
return this;
|
|
@@ -329,7 +329,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
329
329
|
}
|
|
330
330
|
this.listeners.add(listener);
|
|
331
331
|
// Send current state to listener
|
|
332
|
-
if (this.
|
|
332
|
+
if (this.status === InterpreterStatus.Running) {
|
|
333
333
|
listener(this.state);
|
|
334
334
|
}
|
|
335
335
|
if (resolvedCompleteListener) {
|
|
@@ -402,13 +402,13 @@ var Interpreter = /** @class */ (function () {
|
|
|
402
402
|
*/
|
|
403
403
|
Interpreter.prototype.start = function (initialState) {
|
|
404
404
|
var _this = this;
|
|
405
|
-
if (this.
|
|
405
|
+
if (this.status === InterpreterStatus.Running) {
|
|
406
406
|
// Do not restart the service if it is already started
|
|
407
407
|
return this;
|
|
408
408
|
}
|
|
409
409
|
registry_1.registry.register(this.sessionId, this);
|
|
410
410
|
this.initialized = true;
|
|
411
|
-
this.
|
|
411
|
+
this.status = InterpreterStatus.Running;
|
|
412
412
|
var resolvedState = initialState === undefined
|
|
413
413
|
? this.initialState
|
|
414
414
|
: serviceScope.provide(this, function () {
|
|
@@ -431,6 +431,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
431
431
|
*/
|
|
432
432
|
Interpreter.prototype.stop = function () {
|
|
433
433
|
var e_6, _a, e_7, _b, e_8, _c, e_9, _d, e_10, _e;
|
|
434
|
+
var _this = this;
|
|
434
435
|
try {
|
|
435
436
|
for (var _f = __values(this.listeners), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
436
437
|
var listener = _g.value;
|
|
@@ -485,6 +486,22 @@ var Interpreter = /** @class */ (function () {
|
|
|
485
486
|
}
|
|
486
487
|
finally { if (e_9) throw e_9.error; }
|
|
487
488
|
}
|
|
489
|
+
this.state.configuration.forEach(function (stateNode) {
|
|
490
|
+
var e_11, _a;
|
|
491
|
+
try {
|
|
492
|
+
for (var _b = __values(stateNode.definition.exit), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
493
|
+
var action = _c.value;
|
|
494
|
+
_this.exec(action, _this.state);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
498
|
+
finally {
|
|
499
|
+
try {
|
|
500
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
501
|
+
}
|
|
502
|
+
finally { if (e_11) throw e_11.error; }
|
|
503
|
+
}
|
|
504
|
+
});
|
|
488
505
|
// Stop all children
|
|
489
506
|
this.children.forEach(function (child) {
|
|
490
507
|
if (utils_1.isFunction(child.stop)) {
|
|
@@ -507,26 +524,26 @@ var Interpreter = /** @class */ (function () {
|
|
|
507
524
|
}
|
|
508
525
|
this.scheduler.clear();
|
|
509
526
|
this.initialized = false;
|
|
510
|
-
this.
|
|
527
|
+
this.status = InterpreterStatus.Stopped;
|
|
511
528
|
registry_1.registry.free(this.sessionId);
|
|
512
529
|
return this;
|
|
513
530
|
};
|
|
514
531
|
Interpreter.prototype.batch = function (events) {
|
|
515
532
|
var _this = this;
|
|
516
|
-
if (this.
|
|
533
|
+
if (this.status === InterpreterStatus.NotStarted &&
|
|
517
534
|
this.options.deferEvents) {
|
|
518
535
|
// tslint:disable-next-line:no-console
|
|
519
536
|
if (!environment_1.IS_PRODUCTION) {
|
|
520
537
|
utils_1.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));
|
|
521
538
|
}
|
|
522
539
|
}
|
|
523
|
-
else if (this.
|
|
540
|
+
else if (this.status !== InterpreterStatus.Running) {
|
|
524
541
|
throw new Error(
|
|
525
542
|
// tslint:disable-next-line:max-line-length
|
|
526
543
|
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.");
|
|
527
544
|
}
|
|
528
545
|
this.scheduler.schedule(function () {
|
|
529
|
-
var
|
|
546
|
+
var e_12, _a;
|
|
530
547
|
var nextState = _this.state;
|
|
531
548
|
var batchChanged = false;
|
|
532
549
|
var batchedActions = [];
|
|
@@ -547,12 +564,12 @@ var Interpreter = /** @class */ (function () {
|
|
|
547
564
|
_loop_1(event_1);
|
|
548
565
|
}
|
|
549
566
|
}
|
|
550
|
-
catch (
|
|
567
|
+
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
551
568
|
finally {
|
|
552
569
|
try {
|
|
553
570
|
if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
|
|
554
571
|
}
|
|
555
|
-
finally { if (
|
|
572
|
+
finally { if (e_12) throw e_12.error; }
|
|
556
573
|
}
|
|
557
574
|
nextState.changed = batchChanged;
|
|
558
575
|
nextState.actions = batchedActions;
|
|
@@ -587,7 +604,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
587
604
|
return nextState;
|
|
588
605
|
};
|
|
589
606
|
Interpreter.prototype.forward = function (event) {
|
|
590
|
-
var
|
|
607
|
+
var e_13, _a;
|
|
591
608
|
try {
|
|
592
609
|
for (var _b = __values(this.forwardTo), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
593
610
|
var id = _c.value;
|
|
@@ -598,12 +615,12 @@ var Interpreter = /** @class */ (function () {
|
|
|
598
615
|
child.send(event);
|
|
599
616
|
}
|
|
600
617
|
}
|
|
601
|
-
catch (
|
|
618
|
+
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
602
619
|
finally {
|
|
603
620
|
try {
|
|
604
621
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
605
622
|
}
|
|
606
|
-
finally { if (
|
|
623
|
+
finally { if (e_13) throw e_13.error; }
|
|
607
624
|
}
|
|
608
625
|
};
|
|
609
626
|
Interpreter.prototype.defer = function (sendAction) {
|
|
@@ -710,17 +727,17 @@ var Interpreter = /** @class */ (function () {
|
|
|
710
727
|
})
|
|
711
728
|
: serviceCreator;
|
|
712
729
|
if (utils_1.isPromiseLike(source)) {
|
|
713
|
-
this.
|
|
730
|
+
this.spawnPromise(Promise.resolve(source), id);
|
|
714
731
|
}
|
|
715
732
|
else if (utils_1.isFunction(source)) {
|
|
716
|
-
this.
|
|
733
|
+
this.spawnCallback(source, id);
|
|
717
734
|
}
|
|
718
735
|
else if (utils_1.isObservable(source)) {
|
|
719
|
-
this.
|
|
736
|
+
this.spawnObservable(source, id);
|
|
720
737
|
}
|
|
721
738
|
else if (utils_1.isMachine(source)) {
|
|
722
739
|
// TODO: try/catch here
|
|
723
|
-
this.
|
|
740
|
+
this.spawnMachine(resolvedData ? source.withContext(resolvedData) : source, {
|
|
724
741
|
id: id,
|
|
725
742
|
autoForward: autoForward
|
|
726
743
|
});
|
|
@@ -777,7 +794,7 @@ var Interpreter = /** @class */ (function () {
|
|
|
777
794
|
else if (utils_1.isFunction(entity)) {
|
|
778
795
|
return this.spawnCallback(entity, name);
|
|
779
796
|
}
|
|
780
|
-
else if (Actor_1.
|
|
797
|
+
else if (Actor_1.isSpawnedActor(entity)) {
|
|
781
798
|
return this.spawnActor(entity);
|
|
782
799
|
}
|
|
783
800
|
else if (utils_1.isObservable(entity)) {
|
|
@@ -851,21 +868,22 @@ var Interpreter = /** @class */ (function () {
|
|
|
851
868
|
id: id,
|
|
852
869
|
send: function () { return void 0; },
|
|
853
870
|
subscribe: function (next, handleError, complete) {
|
|
871
|
+
var observer = utils_1.toObserver(next, handleError, complete);
|
|
854
872
|
var unsubscribed = false;
|
|
855
873
|
promise.then(function (response) {
|
|
856
874
|
if (unsubscribed) {
|
|
857
875
|
return;
|
|
858
876
|
}
|
|
859
|
-
next
|
|
877
|
+
observer.next(response);
|
|
860
878
|
if (unsubscribed) {
|
|
861
879
|
return;
|
|
862
880
|
}
|
|
863
|
-
complete
|
|
881
|
+
observer.complete();
|
|
864
882
|
}, function (err) {
|
|
865
883
|
if (unsubscribed) {
|
|
866
884
|
return;
|
|
867
885
|
}
|
|
868
|
-
|
|
886
|
+
observer.error(err);
|
|
869
887
|
});
|
|
870
888
|
return {
|
|
871
889
|
unsubscribe: function () { return (unsubscribed = true); }
|
|
@@ -1028,10 +1046,10 @@ var Interpreter = /** @class */ (function () {
|
|
|
1028
1046
|
deferEvents: true,
|
|
1029
1047
|
clock: {
|
|
1030
1048
|
setTimeout: function (fn, ms) {
|
|
1031
|
-
return
|
|
1049
|
+
return setTimeout(fn, ms);
|
|
1032
1050
|
},
|
|
1033
1051
|
clearTimeout: function (id) {
|
|
1034
|
-
return
|
|
1052
|
+
return clearTimeout(id);
|
|
1035
1053
|
}
|
|
1036
1054
|
},
|
|
1037
1055
|
logger: global.console.log.bind(console),
|
package/lib/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
|
package/lib/serviceScope.js
CHANGED
|
@@ -6,10 +6,12 @@ exports.consume = exports.provide = void 0;
|
|
|
6
6
|
* This is used to provide the correct service to spawn().
|
|
7
7
|
*/
|
|
8
8
|
var serviceStack = [];
|
|
9
|
-
|
|
9
|
+
var provide = function (service, fn) {
|
|
10
10
|
serviceStack.push(service);
|
|
11
11
|
var result = fn(service);
|
|
12
12
|
serviceStack.pop();
|
|
13
13
|
return result;
|
|
14
14
|
};
|
|
15
|
-
exports.
|
|
15
|
+
exports.provide = provide;
|
|
16
|
+
var consume = function (fn) { return fn(serviceStack[serviceStack.length - 1]); };
|
|
17
|
+
exports.consume = consume;
|
package/lib/stateUtils.js
CHANGED
|
@@ -33,9 +33,10 @@ var __spread = (this && this.__spread) || function () {
|
|
|
33
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
34
|
exports.isInFinalState = exports.nextEvents = exports.has = exports.getValue = exports.getAdjList = exports.getConfiguration = exports.getAllStateNodes = exports.getChildren = exports.isLeafNode = void 0;
|
|
35
35
|
var utils_1 = require("./utils");
|
|
36
|
-
|
|
36
|
+
var isLeafNode = function (stateNode) {
|
|
37
37
|
return stateNode.type === 'atomic' || stateNode.type === 'final';
|
|
38
38
|
};
|
|
39
|
+
exports.isLeafNode = isLeafNode;
|
|
39
40
|
function getChildren(stateNode) {
|
|
40
41
|
return utils_1.keys(stateNode.states).map(function (key) { return stateNode.states[key]; });
|
|
41
42
|
}
|