xstate 4.9.1 → 4.13.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 +206 -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/Machine.d.ts +4 -1
- package/es/State.d.ts +13 -5
- package/es/StateNode.d.ts +8 -5
- package/es/StateNode.js +68 -47
- package/es/actions.d.ts +12 -10
- package/es/actions.js +7 -9
- package/es/interpreter.d.ts +29 -20
- package/es/interpreter.js +31 -61
- package/es/invokeUtils.d.ts +7 -0
- package/es/invokeUtils.js +39 -0
- 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 +59 -37
- 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.d.ts +4 -1
- package/lib/Machine.js +1 -0
- package/lib/SimulatedClock.js +1 -0
- package/lib/State.d.ts +13 -5
- package/lib/State.js +1 -0
- package/lib/StateNode.d.ts +8 -5
- package/lib/StateNode.js +73 -47
- package/lib/actionTypes.js +1 -0
- package/lib/actions.d.ts +12 -10
- package/lib/actions.js +6 -17
- 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.js +28 -20
- package/lib/interpreter.d.ts +29 -20
- package/lib/interpreter.js +32 -51
- 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.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 +59 -37
- package/lib/types.js +1 -0
- package/lib/utils.d.ts +4 -3
- package/lib/utils.js +9 -2
- package/package.json +4 -4
package/lib/Actor.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isActor = exports.createDeferredActor = exports.createInvocableActor = exports.createNullActor = void 0;
|
|
4
|
+
var utils_1 = require("./utils");
|
|
5
|
+
var serviceScope = require("./serviceScope");
|
|
3
6
|
function createNullActor(id) {
|
|
4
7
|
return {
|
|
5
8
|
id: id,
|
|
@@ -14,17 +17,35 @@ function createNullActor(id) {
|
|
|
14
17
|
}
|
|
15
18
|
exports.createNullActor = createNullActor;
|
|
16
19
|
/**
|
|
17
|
-
* Creates a
|
|
20
|
+
* Creates a deferred actor that is able to be invoked given the provided
|
|
18
21
|
* invocation information in its `.meta` value.
|
|
19
22
|
*
|
|
20
23
|
* @param invokeDefinition The meta information needed to invoke the actor.
|
|
21
24
|
*/
|
|
22
|
-
function createInvocableActor(invokeDefinition) {
|
|
23
|
-
var
|
|
25
|
+
function createInvocableActor(invokeDefinition, machine, context, _event) {
|
|
26
|
+
var _a;
|
|
27
|
+
var invokeSrc = utils_1.toInvokeSource(invokeDefinition.src);
|
|
28
|
+
var serviceCreator = (_a = machine === null || machine === void 0 ? void 0 : machine.options.services) === null || _a === void 0 ? void 0 : _a[invokeSrc.type];
|
|
29
|
+
var resolvedData = invokeDefinition.data
|
|
30
|
+
? utils_1.mapContext(invokeDefinition.data, context, _event)
|
|
31
|
+
: undefined;
|
|
32
|
+
var tempActor = serviceCreator
|
|
33
|
+
? createDeferredActor(serviceCreator, invokeDefinition.id, resolvedData)
|
|
34
|
+
: createNullActor(invokeDefinition.id);
|
|
24
35
|
tempActor.meta = invokeDefinition;
|
|
25
36
|
return tempActor;
|
|
26
37
|
}
|
|
27
38
|
exports.createInvocableActor = createInvocableActor;
|
|
39
|
+
function createDeferredActor(entity, id, data) {
|
|
40
|
+
var tempActor = createNullActor(id);
|
|
41
|
+
tempActor.deferred = true;
|
|
42
|
+
if (utils_1.isMachine(entity)) {
|
|
43
|
+
// "mute" the existing service scope so potential spawned actors within the `.initialState` stay deferred here
|
|
44
|
+
tempActor.state = serviceScope.provide(undefined, function () { return (data ? entity.withContext(data) : entity).initialState; });
|
|
45
|
+
}
|
|
46
|
+
return tempActor;
|
|
47
|
+
}
|
|
48
|
+
exports.createDeferredActor = createDeferredActor;
|
|
28
49
|
function isActor(item) {
|
|
29
50
|
try {
|
|
30
51
|
return typeof item.send === 'function';
|
package/lib/Machine.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { StateMachine, MachineOptions, DefaultContext, MachineConfig, StateSchema, EventObject, AnyEventObject, Typestate } from './types';
|
|
2
2
|
export declare function Machine<TContext = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, any, TEvent>;
|
|
3
3
|
export declare function Machine<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, TStateSchema, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, TStateSchema, TEvent>;
|
|
4
|
-
export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> =
|
|
4
|
+
export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = {
|
|
5
|
+
value: any;
|
|
6
|
+
context: TContext;
|
|
7
|
+
}>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
|
|
5
8
|
//# sourceMappingURL=Machine.d.ts.map
|
package/lib/Machine.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMachine = exports.Machine = void 0;
|
|
3
4
|
var StateNode_1 = require("./StateNode");
|
|
4
5
|
function Machine(config, options, initialContext) {
|
|
5
6
|
if (initialContext === void 0) { initialContext = config.context; }
|
package/lib/SimulatedClock.js
CHANGED
|
@@ -20,6 +20,7 @@ var __spread = (this && this.__spread) || function () {
|
|
|
20
20
|
return ar;
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.SimulatedClock = void 0;
|
|
23
24
|
var SimulatedClock = /** @class */ (function () {
|
|
24
25
|
function SimulatedClock() {
|
|
25
26
|
this.timeouts = new Map();
|
package/lib/State.d.ts
CHANGED
|
@@ -2,13 +2,19 @@ import { StateValue, ActivityMap, EventObject, HistoryValue, ActionObject, Event
|
|
|
2
2
|
import { StateNode } from './StateNode';
|
|
3
3
|
import { Actor } from './Actor';
|
|
4
4
|
export declare function stateValuesEqual(a: StateValue | undefined, b: StateValue | undefined): boolean;
|
|
5
|
-
export declare function isState<TContext, TEvent extends EventObject>
|
|
5
|
+
export declare function isState<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
|
|
6
|
+
value: any;
|
|
7
|
+
context: TContext;
|
|
8
|
+
}>(state: object | string): state is State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
6
9
|
export declare function bindActionToState<TC, TE extends EventObject>(action: ActionObject<TC, TE>, state: State<TC, TE>): ActionObject<TC, TE>;
|
|
7
|
-
export declare class State<TContext, TEvent extends EventObject = EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> =
|
|
10
|
+
export declare class State<TContext, TEvent extends EventObject = EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
|
|
11
|
+
value: any;
|
|
12
|
+
context: TContext;
|
|
13
|
+
}> {
|
|
8
14
|
value: StateValue;
|
|
9
15
|
context: TContext;
|
|
10
16
|
historyValue?: HistoryValue | undefined;
|
|
11
|
-
history?: State<TContext, TEvent, TStateSchema>;
|
|
17
|
+
history?: State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
12
18
|
actions: Array<ActionObject<TContext, TEvent>>;
|
|
13
19
|
activities: ActivityMap;
|
|
14
20
|
meta: any;
|
|
@@ -86,8 +92,10 @@ export declare class State<TContext, TEvent extends EventObject = EventObject, T
|
|
|
86
92
|
* Whether the current state value is a subset of the given parent state value.
|
|
87
93
|
* @param parentStateValue
|
|
88
94
|
*/
|
|
89
|
-
matches<TSV extends TTypestate['value']>(parentStateValue: TSV): this is TTypestate extends {
|
|
95
|
+
matches<TSV extends TTypestate['value']>(parentStateValue: TSV): this is State<(TTypestate extends {
|
|
96
|
+
value: TSV;
|
|
97
|
+
} ? TTypestate : never)['context'], TEvent, TStateSchema, TTypestate> & {
|
|
90
98
|
value: TSV;
|
|
91
|
-
}
|
|
99
|
+
};
|
|
92
100
|
}
|
|
93
101
|
//# sourceMappingURL=State.d.ts.map
|
package/lib/State.js
CHANGED
|
@@ -42,6 +42,7 @@ var __spread = (this && this.__spread) || function () {
|
|
|
42
42
|
return ar;
|
|
43
43
|
};
|
|
44
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.State = exports.bindActionToState = exports.isState = exports.stateValuesEqual = void 0;
|
|
45
46
|
var constants_1 = require("./constants");
|
|
46
47
|
var utils_1 = require("./utils");
|
|
47
48
|
var stateUtils_1 = require("./stateUtils");
|
package/lib/StateNode.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { Event, StateValue, StateValueMap, MachineOptions, EventObject, HistoryValue, StateNodeDefinition, TransitionDefinition, DelayedTransitionDefinition, ActivityDefinition, StateNodeConfig, StateSchema, StateNodesConfig, InvokeDefinition, ActionObject, Mapper, PropertyMapper, SCXML, Typestate, TransitionDefinitionMap } from './types';
|
|
2
2
|
import { State } from './State';
|
|
3
|
-
declare class StateNode<TContext = any, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> =
|
|
3
|
+
declare class StateNode<TContext = any, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
|
|
4
|
+
value: any;
|
|
5
|
+
context: TContext;
|
|
6
|
+
}> {
|
|
4
7
|
/**
|
|
5
8
|
* The raw config used to create the machine.
|
|
6
9
|
*/
|
|
@@ -92,7 +95,7 @@ declare class StateNode<TContext = any, TStateSchema extends StateSchema = any,
|
|
|
92
95
|
/**
|
|
93
96
|
* The data sent with the "done.state._id_" event if this is a final state node.
|
|
94
97
|
*/
|
|
95
|
-
|
|
98
|
+
doneData?: Mapper<TContext, TEvent, any> | PropertyMapper<TContext, TEvent, any>;
|
|
96
99
|
/**
|
|
97
100
|
* The string delimiter for serializing the path to a string. The default is "."
|
|
98
101
|
*/
|
|
@@ -156,7 +159,7 @@ declare class StateNode<TContext = any, TStateSchema extends StateSchema = any,
|
|
|
156
159
|
*
|
|
157
160
|
* @param state The state value or State instance
|
|
158
161
|
*/
|
|
159
|
-
getStateNodes(state: StateValue | State<TContext, TEvent>): Array<StateNode<TContext, any, TEvent>>;
|
|
162
|
+
getStateNodes(state: StateValue | State<TContext, TEvent, any, TTypestate>): Array<StateNode<TContext, any, TEvent>>;
|
|
160
163
|
/**
|
|
161
164
|
* Returns `true` if this state node explicitly handles the given event.
|
|
162
165
|
*
|
|
@@ -170,7 +173,7 @@ declare class StateNode<TContext = any, TStateSchema extends StateSchema = any,
|
|
|
170
173
|
*
|
|
171
174
|
* @param state The state to resolve
|
|
172
175
|
*/
|
|
173
|
-
resolveState(state: State<TContext, TEvent>): State<TContext, TEvent>;
|
|
176
|
+
resolveState(state: State<TContext, TEvent>): State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
174
177
|
private transitionLeafNode;
|
|
175
178
|
private transitionCompoundNode;
|
|
176
179
|
private transitionParallelNode;
|
|
@@ -190,7 +193,7 @@ declare class StateNode<TContext = any, TStateSchema extends StateSchema = any,
|
|
|
190
193
|
* @param event The event that was sent at the current state
|
|
191
194
|
* @param context The current context (extended state) of the current state
|
|
192
195
|
*/
|
|
193
|
-
transition(state: string | StateValueMap | State<TContext, TEvent, any,
|
|
196
|
+
transition(state: string | StateValueMap | State<TContext, TEvent, any, TTypestate> | undefined, event: Event<TEvent> | SCXML.Event<TEvent>, context?: TContext): State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
194
197
|
private resolveRaisedTransition;
|
|
195
198
|
private resolveTransition;
|
|
196
199
|
/**
|
package/lib/StateNode.js
CHANGED
|
@@ -53,6 +53,7 @@ var __values = (this && this.__values) || function(o) {
|
|
|
53
53
|
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
54
54
|
};
|
|
55
55
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
56
|
+
exports.StateNode = void 0;
|
|
56
57
|
var utils_1 = require("./utils");
|
|
57
58
|
var types_1 = require("./types");
|
|
58
59
|
var utils_2 = require("./utils");
|
|
@@ -63,6 +64,7 @@ var environment_1 = require("./environment");
|
|
|
63
64
|
var constants_1 = require("./constants");
|
|
64
65
|
var stateUtils_1 = require("./stateUtils");
|
|
65
66
|
var Actor_1 = require("./Actor");
|
|
67
|
+
var invokeUtils_1 = require("./invokeUtils");
|
|
66
68
|
var NULL_EVENT = '';
|
|
67
69
|
var STATE_IDENTIFIER = '#';
|
|
68
70
|
var WILDCARD = '*';
|
|
@@ -179,21 +181,23 @@ var StateNode = /** @class */ (function () {
|
|
|
179
181
|
// History config
|
|
180
182
|
this.history =
|
|
181
183
|
this.config.history === true ? 'shallow' : this.config.history || false;
|
|
182
|
-
this._transient =
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
184
|
+
this._transient =
|
|
185
|
+
!!this.config.always ||
|
|
186
|
+
(!this.config.on
|
|
187
|
+
? false
|
|
188
|
+
: Array.isArray(this.config.on)
|
|
189
|
+
? this.config.on.some(function (_a) {
|
|
190
|
+
var event = _a.event;
|
|
191
|
+
return event === NULL_EVENT;
|
|
192
|
+
})
|
|
193
|
+
: NULL_EVENT in this.config.on);
|
|
190
194
|
this.strict = !!this.config.strict;
|
|
191
195
|
// TODO: deprecate (entry)
|
|
192
196
|
this.onEntry = utils_1.toArray(this.config.entry || this.config.onEntry).map(function (action) { return actions_1.toActionObject(action); });
|
|
193
197
|
// TODO: deprecate (exit)
|
|
194
198
|
this.onExit = utils_1.toArray(this.config.exit || this.config.onExit).map(function (action) { return actions_1.toActionObject(action); });
|
|
195
199
|
this.meta = this.config.meta;
|
|
196
|
-
this.
|
|
200
|
+
this.doneData =
|
|
197
201
|
this.type === 'final'
|
|
198
202
|
? this.config.data
|
|
199
203
|
: undefined;
|
|
@@ -201,25 +205,33 @@ var StateNode = /** @class */ (function () {
|
|
|
201
205
|
var _a, _b;
|
|
202
206
|
if (utils_1.isMachine(invokeConfig)) {
|
|
203
207
|
_this.machine.options.services = __assign((_a = {}, _a[invokeConfig.id] = invokeConfig, _a), _this.machine.options.services);
|
|
204
|
-
return {
|
|
205
|
-
type: actionTypes.invoke,
|
|
208
|
+
return invokeUtils_1.toInvokeDefinition({
|
|
206
209
|
src: invokeConfig.id,
|
|
207
210
|
id: invokeConfig.id
|
|
208
|
-
};
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
else if (utils_1.isString(invokeConfig.src)) {
|
|
214
|
+
return invokeUtils_1.toInvokeDefinition(__assign(__assign({}, invokeConfig), { id: invokeConfig.id || invokeConfig.src, src: invokeConfig.src }));
|
|
209
215
|
}
|
|
210
|
-
else if (
|
|
216
|
+
else if (utils_1.isMachine(invokeConfig.src) || utils_1.isFunction(invokeConfig.src)) {
|
|
211
217
|
var invokeSrc = _this.id + ":invocation[" + i + "]"; // TODO: util function
|
|
212
218
|
_this.machine.options.services = __assign((_b = {}, _b[invokeSrc] = invokeConfig.src, _b), _this.machine.options.services);
|
|
213
|
-
return __assign(__assign({
|
|
219
|
+
return invokeUtils_1.toInvokeDefinition(__assign(__assign({ id: invokeSrc }, invokeConfig), { src: invokeSrc }));
|
|
214
220
|
}
|
|
215
221
|
else {
|
|
216
|
-
|
|
222
|
+
var invokeSource = invokeConfig.src;
|
|
223
|
+
return invokeUtils_1.toInvokeDefinition(__assign(__assign({ id: invokeSource.type }, invokeConfig), { src: invokeSource }));
|
|
217
224
|
}
|
|
218
225
|
});
|
|
219
226
|
this.activities = utils_1.toArray(this.config.activities)
|
|
220
227
|
.concat(this.invoke)
|
|
221
228
|
.map(function (activity) { return actions_1.toActivityDefinition(activity); });
|
|
222
229
|
this.transition = this.transition.bind(this);
|
|
230
|
+
// TODO: this is the real fix for initialization once
|
|
231
|
+
// state node getters are deprecated
|
|
232
|
+
// if (!this.parent) {
|
|
233
|
+
// this._init();
|
|
234
|
+
// }
|
|
223
235
|
}
|
|
224
236
|
StateNode.prototype._init = function () {
|
|
225
237
|
if (this.__cache.transitions) {
|
|
@@ -273,11 +285,11 @@ var StateNode = /** @class */ (function () {
|
|
|
273
285
|
activities: this.activities || [],
|
|
274
286
|
meta: this.meta,
|
|
275
287
|
order: this.order || -1,
|
|
276
|
-
data: this.
|
|
288
|
+
data: this.doneData,
|
|
277
289
|
invoke: this.invoke
|
|
278
290
|
};
|
|
279
291
|
},
|
|
280
|
-
enumerable:
|
|
292
|
+
enumerable: false,
|
|
281
293
|
configurable: true
|
|
282
294
|
});
|
|
283
295
|
StateNode.prototype.toJSON = function () {
|
|
@@ -298,7 +310,7 @@ var StateNode = /** @class */ (function () {
|
|
|
298
310
|
return map;
|
|
299
311
|
}, {}));
|
|
300
312
|
},
|
|
301
|
-
enumerable:
|
|
313
|
+
enumerable: false,
|
|
302
314
|
configurable: true
|
|
303
315
|
});
|
|
304
316
|
Object.defineProperty(StateNode.prototype, "after", {
|
|
@@ -307,7 +319,7 @@ var StateNode = /** @class */ (function () {
|
|
|
307
319
|
((this.__cache.delayedTransitions = this.getDelayedTransitions()),
|
|
308
320
|
this.__cache.delayedTransitions));
|
|
309
321
|
},
|
|
310
|
-
enumerable:
|
|
322
|
+
enumerable: false,
|
|
311
323
|
configurable: true
|
|
312
324
|
});
|
|
313
325
|
Object.defineProperty(StateNode.prototype, "transitions", {
|
|
@@ -319,7 +331,7 @@ var StateNode = /** @class */ (function () {
|
|
|
319
331
|
((this.__cache.transitions = this.formatTransitions()),
|
|
320
332
|
this.__cache.transitions));
|
|
321
333
|
},
|
|
322
|
-
enumerable:
|
|
334
|
+
enumerable: false,
|
|
323
335
|
configurable: true
|
|
324
336
|
});
|
|
325
337
|
StateNode.prototype.getCandidates = function (eventName) {
|
|
@@ -645,14 +657,16 @@ var StateNode = /** @class */ (function () {
|
|
|
645
657
|
if (!parent.parent) {
|
|
646
658
|
return events;
|
|
647
659
|
}
|
|
648
|
-
events.push(actions_1.done(sn.id, sn.
|
|
649
|
-
actions_1.done(parent.id, sn.
|
|
660
|
+
events.push(actions_1.done(sn.id, sn.doneData), // TODO: deprecate - final states should not emit done events for their own state.
|
|
661
|
+
actions_1.done(parent.id, sn.doneData
|
|
662
|
+
? utils_1.mapContext(sn.doneData, currentContext, _event)
|
|
663
|
+
: undefined));
|
|
650
664
|
var grandparent = parent.parent;
|
|
651
665
|
if (grandparent.type === 'parallel') {
|
|
652
666
|
if (stateUtils_1.getChildren(grandparent).every(function (parentNode) {
|
|
653
667
|
return stateUtils_1.isInFinalState(transition.configuration, parentNode);
|
|
654
668
|
})) {
|
|
655
|
-
events.push(actions_1.done(grandparent.id
|
|
669
|
+
events.push(actions_1.done(grandparent.id));
|
|
656
670
|
}
|
|
657
671
|
}
|
|
658
672
|
return events;
|
|
@@ -678,7 +692,8 @@ var StateNode = /** @class */ (function () {
|
|
|
678
692
|
* @param context The current context (extended state) of the current state
|
|
679
693
|
*/
|
|
680
694
|
StateNode.prototype.transition = function (state, event, context) {
|
|
681
|
-
if (state === void 0) { state = this
|
|
695
|
+
if (state === void 0) { state = this
|
|
696
|
+
.initialState; }
|
|
682
697
|
var _event = utils_1.toSCXMLEvent(event);
|
|
683
698
|
var currentState;
|
|
684
699
|
if (state instanceof State_1.State) {
|
|
@@ -722,6 +737,7 @@ var StateNode = /** @class */ (function () {
|
|
|
722
737
|
var currentActions = state.actions;
|
|
723
738
|
state = this.transition(state, _event);
|
|
724
739
|
// Save original event to state
|
|
740
|
+
// TODO: this should be the raised event! Delete in V5 (breaking)
|
|
725
741
|
state._event = originalEvent;
|
|
726
742
|
state.event = originalEvent.data;
|
|
727
743
|
(_a = state.actions).unshift.apply(_a, __spread(currentActions));
|
|
@@ -729,6 +745,7 @@ var StateNode = /** @class */ (function () {
|
|
|
729
745
|
};
|
|
730
746
|
StateNode.prototype.resolveTransition = function (stateTransition, currentState, _event, context) {
|
|
731
747
|
var e_6, _a;
|
|
748
|
+
var _this = this;
|
|
732
749
|
if (_event === void 0) { _event = actions_1.initEvent; }
|
|
733
750
|
if (context === void 0) { context = this.machine.context; }
|
|
734
751
|
var configuration = stateTransition.configuration;
|
|
@@ -753,10 +770,10 @@ var StateNode = /** @class */ (function () {
|
|
|
753
770
|
for (var actions_2 = __values(actions), actions_2_1 = actions_2.next(); !actions_2_1.done; actions_2_1 = actions_2.next()) {
|
|
754
771
|
var action = actions_2_1.value;
|
|
755
772
|
if (action.type === actionTypes.start) {
|
|
756
|
-
activities[action.activity.type] = action;
|
|
773
|
+
activities[action.activity.id || action.activity.type] = action;
|
|
757
774
|
}
|
|
758
775
|
else if (action.type === actionTypes.stop) {
|
|
759
|
-
activities[action.activity.type] = false;
|
|
776
|
+
activities[action.activity.id || action.activity.type] = false;
|
|
760
777
|
}
|
|
761
778
|
}
|
|
762
779
|
}
|
|
@@ -780,7 +797,7 @@ var StateNode = /** @class */ (function () {
|
|
|
780
797
|
actionTypes.invoke);
|
|
781
798
|
});
|
|
782
799
|
var children = invokeActions.reduce(function (acc, action) {
|
|
783
|
-
acc[action.activity.id] = Actor_1.createInvocableActor(action.activity);
|
|
800
|
+
acc[action.activity.id] = Actor_1.createInvocableActor(action.activity, _this.machine, updatedContext, _event);
|
|
784
801
|
return acc;
|
|
785
802
|
}, currentState
|
|
786
803
|
? __assign({}, currentState.children) : {});
|
|
@@ -842,7 +859,9 @@ var StateNode = /** @class */ (function () {
|
|
|
842
859
|
var maybeNextState = nextState;
|
|
843
860
|
if (!isDone) {
|
|
844
861
|
var isTransient = this._transient ||
|
|
845
|
-
configuration.some(function (stateNode) {
|
|
862
|
+
configuration.some(function (stateNode) {
|
|
863
|
+
return stateNode._transient;
|
|
864
|
+
});
|
|
846
865
|
if (isTransient) {
|
|
847
866
|
maybeNextState = this.resolveRaisedTransition(maybeNextState, {
|
|
848
867
|
type: actionTypes.nullEvent
|
|
@@ -999,7 +1018,7 @@ var StateNode = /** @class */ (function () {
|
|
|
999
1018
|
this.__cache.initialStateValue = initialStateValue;
|
|
1000
1019
|
return this.__cache.initialStateValue;
|
|
1001
1020
|
},
|
|
1002
|
-
enumerable:
|
|
1021
|
+
enumerable: false,
|
|
1003
1022
|
configurable: true
|
|
1004
1023
|
});
|
|
1005
1024
|
StateNode.prototype.getInitialState = function (stateValue, context) {
|
|
@@ -1019,14 +1038,14 @@ var StateNode = /** @class */ (function () {
|
|
|
1019
1038
|
* entering the initial state.
|
|
1020
1039
|
*/
|
|
1021
1040
|
get: function () {
|
|
1022
|
-
this._init();
|
|
1041
|
+
this._init(); // TODO: this should be in the constructor (see note in constructor)
|
|
1023
1042
|
var initialStateValue = this.initialStateValue;
|
|
1024
1043
|
if (!initialStateValue) {
|
|
1025
1044
|
throw new Error("Cannot retrieve initial state from simple state '" + this.id + "'.");
|
|
1026
1045
|
}
|
|
1027
1046
|
return this.getInitialState(initialStateValue);
|
|
1028
1047
|
},
|
|
1029
|
-
enumerable:
|
|
1048
|
+
enumerable: false,
|
|
1030
1049
|
configurable: true
|
|
1031
1050
|
});
|
|
1032
1051
|
Object.defineProperty(StateNode.prototype, "target", {
|
|
@@ -1051,7 +1070,7 @@ var StateNode = /** @class */ (function () {
|
|
|
1051
1070
|
}
|
|
1052
1071
|
return target;
|
|
1053
1072
|
},
|
|
1054
|
-
enumerable:
|
|
1073
|
+
enumerable: false,
|
|
1055
1074
|
configurable: true
|
|
1056
1075
|
});
|
|
1057
1076
|
/**
|
|
@@ -1087,7 +1106,7 @@ var StateNode = /** @class */ (function () {
|
|
|
1087
1106
|
return _this.getFromRelativePath(initialPath);
|
|
1088
1107
|
}));
|
|
1089
1108
|
},
|
|
1090
|
-
enumerable:
|
|
1109
|
+
enumerable: false,
|
|
1091
1110
|
configurable: true
|
|
1092
1111
|
});
|
|
1093
1112
|
/**
|
|
@@ -1171,7 +1190,7 @@ var StateNode = /** @class */ (function () {
|
|
|
1171
1190
|
}));
|
|
1172
1191
|
return [this.id].concat(childStateIds);
|
|
1173
1192
|
},
|
|
1174
|
-
enumerable:
|
|
1193
|
+
enumerable: false,
|
|
1175
1194
|
configurable: true
|
|
1176
1195
|
});
|
|
1177
1196
|
Object.defineProperty(StateNode.prototype, "events", {
|
|
@@ -1217,7 +1236,7 @@ var StateNode = /** @class */ (function () {
|
|
|
1217
1236
|
}
|
|
1218
1237
|
return (this.__cache.events = Array.from(events));
|
|
1219
1238
|
},
|
|
1220
|
-
enumerable:
|
|
1239
|
+
enumerable: false,
|
|
1221
1240
|
configurable: true
|
|
1222
1241
|
});
|
|
1223
1242
|
Object.defineProperty(StateNode.prototype, "ownEvents", {
|
|
@@ -1236,7 +1255,7 @@ var StateNode = /** @class */ (function () {
|
|
|
1236
1255
|
.map(function (transition) { return transition.eventType; }));
|
|
1237
1256
|
return Array.from(events);
|
|
1238
1257
|
},
|
|
1239
|
-
enumerable:
|
|
1258
|
+
enumerable: false,
|
|
1240
1259
|
configurable: true
|
|
1241
1260
|
});
|
|
1242
1261
|
StateNode.prototype.resolveTarget = function (_target) {
|
|
@@ -1280,12 +1299,9 @@ var StateNode = /** @class */ (function () {
|
|
|
1280
1299
|
: true;
|
|
1281
1300
|
var guards = this.machine.options.guards;
|
|
1282
1301
|
var target = this.resolveTarget(normalizedTarget);
|
|
1283
|
-
var transition = __assign(__assign({}, transitionConfig), { actions: actions_1.toActionObjects(utils_1.toArray(transitionConfig.actions)), cond: utils_1.toGuard(transitionConfig.cond, guards), target: target, source: this, internal: internal, eventType: transitionConfig.event })
|
|
1284
|
-
Object.defineProperty(transition, 'toJSON', {
|
|
1285
|
-
value: function () { return (__assign(__assign({}, transition), { target: transition.target
|
|
1302
|
+
var transition = __assign(__assign({}, transitionConfig), { actions: actions_1.toActionObjects(utils_1.toArray(transitionConfig.actions)), cond: utils_1.toGuard(transitionConfig.cond, guards), target: target, source: this, internal: internal, eventType: transitionConfig.event, toJSON: function () { return (__assign(__assign({}, transition), { target: transition.target
|
|
1286
1303
|
? transition.target.map(function (t) { return "#" + t.id; })
|
|
1287
|
-
: undefined, source: "#
|
|
1288
|
-
});
|
|
1304
|
+
: undefined, source: "#" + _this.id })); } });
|
|
1289
1305
|
return transition;
|
|
1290
1306
|
};
|
|
1291
1307
|
StateNode.prototype.formatTransitions = function () {
|
|
@@ -1299,20 +1315,30 @@ var StateNode = /** @class */ (function () {
|
|
|
1299
1315
|
onConfig = this.config.on;
|
|
1300
1316
|
}
|
|
1301
1317
|
else {
|
|
1302
|
-
var _b = this.config.on, _c = WILDCARD, _d = _b[_c], wildcardConfigs = _d === void 0 ? [] : _d,
|
|
1303
|
-
onConfig = utils_1.flatten(utils_1.keys(
|
|
1318
|
+
var _b = this.config.on, _c = WILDCARD, _d = _b[_c], wildcardConfigs = _d === void 0 ? [] : _d, strictTransitionConfigs_1 = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
|
|
1319
|
+
onConfig = utils_1.flatten(utils_1.keys(strictTransitionConfigs_1)
|
|
1304
1320
|
.map(function (key) {
|
|
1305
|
-
|
|
1321
|
+
if (!environment_1.IS_PRODUCTION && key === NULL_EVENT) {
|
|
1322
|
+
utils_1.warn(false, "Empty string transition configs (e.g., `{ on: { '': ... }}`) for transient transitions are deprecated. Specify the transition in the `{ always: ... }` property instead. " +
|
|
1323
|
+
("Please check the `on` configuration for \"#" + _this.id + "\"."));
|
|
1324
|
+
}
|
|
1325
|
+
var transitionConfigArray = utils_1.toTransitionConfigArray(key, strictTransitionConfigs_1[key]);
|
|
1306
1326
|
if (!environment_1.IS_PRODUCTION) {
|
|
1307
|
-
validateArrayifiedTransitions(_this, key,
|
|
1327
|
+
validateArrayifiedTransitions(_this, key, transitionConfigArray);
|
|
1308
1328
|
}
|
|
1309
|
-
return
|
|
1329
|
+
return transitionConfigArray;
|
|
1310
1330
|
})
|
|
1311
1331
|
.concat(utils_1.toTransitionConfigArray(WILDCARD, wildcardConfigs)));
|
|
1312
1332
|
}
|
|
1333
|
+
var eventlessConfig = this.config.always
|
|
1334
|
+
? utils_1.toTransitionConfigArray('', this.config.always)
|
|
1335
|
+
: [];
|
|
1313
1336
|
var doneConfig = this.config.onDone
|
|
1314
1337
|
? utils_1.toTransitionConfigArray(String(actions_1.done(this.id)), this.config.onDone)
|
|
1315
1338
|
: [];
|
|
1339
|
+
if (!environment_1.IS_PRODUCTION) {
|
|
1340
|
+
utils_1.warn(!(this.config.onDone && !this.parent), "Root nodes cannot have an \".onDone\" transition. Please check the config of \"" + this.id + "\".");
|
|
1341
|
+
}
|
|
1316
1342
|
var invokeConfig = utils_1.flatten(this.invoke.map(function (invokeDef) {
|
|
1317
1343
|
var settleTransitions = [];
|
|
1318
1344
|
if (invokeDef.onDone) {
|
|
@@ -1324,7 +1350,7 @@ var StateNode = /** @class */ (function () {
|
|
|
1324
1350
|
return settleTransitions;
|
|
1325
1351
|
}));
|
|
1326
1352
|
var delayedTransitions = this.after;
|
|
1327
|
-
var formattedTransitions = utils_1.flatten(__spread(doneConfig, invokeConfig, onConfig).map(function (transitionConfig) {
|
|
1353
|
+
var formattedTransitions = utils_1.flatten(__spread(doneConfig, invokeConfig, onConfig, eventlessConfig).map(function (transitionConfig) {
|
|
1328
1354
|
return utils_1.toArray(transitionConfig).map(function (transition) {
|
|
1329
1355
|
return _this.formatTransition(transition);
|
|
1330
1356
|
});
|
package/lib/actionTypes.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pure = exports.choose = exports.update = exports.error = exports.errorPlatform = exports.errorExecution = exports.invoke = exports.init = exports.log = exports.doneState = exports.after = exports.assign = exports.nullEvent = exports.cancel = exports.send = exports.raise = exports.stop = exports.start = void 0;
|
|
3
4
|
var types_1 = require("./types");
|
|
4
5
|
// xstate-specific action types
|
|
5
6
|
exports.start = types_1.ActionTypes.Start;
|
package/lib/actions.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseConditon, ChooseAction } from './types';
|
|
1
|
+
import { Action, Event, EventObject, SingleOrArray, SendAction, SendActionOptions, CancelAction, ActionObject, ActionType, Assigner, PropertyAssigner, AssignAction, ActionFunction, ActionFunctionMap, ActivityActionObject, ActionTypes, ActivityDefinition, RaiseAction, RaiseActionObject, DoneEvent, ErrorPlatformEvent, DoneEventObject, SendExpr, SendActionObject, PureAction, LogExpr, LogAction, LogActionObject, DelayFunctionMap, SCXML, ExprWithMeta, ChooseConditon, ChooseAction, AnyEventObject } from './types';
|
|
2
2
|
import * as actionTypes from './actionTypes';
|
|
3
3
|
import { State } from './State';
|
|
4
4
|
import { StateNode } from './StateNode';
|
|
@@ -8,7 +8,7 @@ export declare const initEvent: SCXML.Event<{
|
|
|
8
8
|
}>;
|
|
9
9
|
export declare function getActionFunction<TContext, TEvent extends EventObject>(actionType: ActionType, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | undefined;
|
|
10
10
|
export declare function toActionObject<TContext, TEvent extends EventObject>(action: Action<TContext, TEvent>, actionFunctionMap?: ActionFunctionMap<TContext, TEvent>): ActionObject<TContext, TEvent>;
|
|
11
|
-
export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: string |
|
|
11
|
+
export declare const toActionObjects: <TContext, TEvent extends EventObject>(action?: string | ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent> | Action<TContext, TEvent>[] | undefined, actionFunctionMap?: Record<string, ActionObject<TContext, TEvent> | ActionFunction<TContext, TEvent>> | undefined) => ActionObject<TContext, TEvent>[];
|
|
12
12
|
export declare function toActivityDefinition<TContext, TEvent extends EventObject>(action: string | ActivityDefinition<TContext, TEvent>): ActivityDefinition<TContext, TEvent>;
|
|
13
13
|
/**
|
|
14
14
|
* Raises an event. This places the event in the internal event queue, so that
|
|
@@ -16,7 +16,7 @@ export declare function toActivityDefinition<TContext, TEvent extends EventObjec
|
|
|
16
16
|
*
|
|
17
17
|
* @param eventType The event to raise.
|
|
18
18
|
*/
|
|
19
|
-
export declare function raise<TContext, TEvent extends EventObject>(event: Event<TEvent>): RaiseAction<TEvent> | SendAction<TContext, TEvent>;
|
|
19
|
+
export declare function raise<TContext, TEvent extends EventObject>(event: Event<TEvent>): RaiseAction<TEvent> | SendAction<TContext, AnyEventObject, TEvent>;
|
|
20
20
|
export declare function resolveRaise<TEvent extends EventObject>(action: RaiseAction<TEvent>): RaiseActionObject<TEvent>;
|
|
21
21
|
/**
|
|
22
22
|
* Sends an event. This returns an action that will be read by an interpreter to
|
|
@@ -28,26 +28,28 @@ export declare function resolveRaise<TEvent extends EventObject>(action: RaiseAc
|
|
|
28
28
|
* - `delay` - The number of milliseconds to delay the sending of the event.
|
|
29
29
|
* - `to` - The target of this event (by default, the machine the event was sent from).
|
|
30
30
|
*/
|
|
31
|
-
export declare function send<TContext, TEvent extends EventObject>(event: Event<
|
|
32
|
-
export declare function resolveSend<TContext, TEvent extends EventObject>(action: SendAction<TContext, TEvent>, ctx: TContext, _event: SCXML.Event<TEvent>, delaysMap?: DelayFunctionMap<TContext, TEvent>): SendActionObject<TContext, TEvent>;
|
|
31
|
+
export declare function send<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<TSentEvent> | SendExpr<TContext, TEvent, TSentEvent>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, TSentEvent>;
|
|
32
|
+
export declare function resolveSend<TContext, TEvent extends EventObject, TSentEvent extends EventObject>(action: SendAction<TContext, TEvent, TSentEvent>, ctx: TContext, _event: SCXML.Event<TEvent>, delaysMap?: DelayFunctionMap<TContext, TEvent>): SendActionObject<TContext, TEvent, TSentEvent>;
|
|
33
33
|
/**
|
|
34
34
|
* Sends an event to this machine's parent.
|
|
35
35
|
*
|
|
36
36
|
* @param event The event to send to the parent machine.
|
|
37
37
|
* @param options Options to pass into the send event.
|
|
38
38
|
*/
|
|
39
|
-
export declare function sendParent<TContext, TEvent extends EventObject>(event: Event<
|
|
39
|
+
export declare function sendParent<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<TSentEvent> | SendExpr<TContext, TEvent, TSentEvent>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, TSentEvent>;
|
|
40
40
|
/**
|
|
41
41
|
* Sends an update event to this machine's parent.
|
|
42
42
|
*/
|
|
43
|
-
export declare function sendUpdate<TContext, TEvent extends EventObject>(): SendAction<TContext, TEvent
|
|
43
|
+
export declare function sendUpdate<TContext, TEvent extends EventObject>(): SendAction<TContext, TEvent, {
|
|
44
|
+
type: ActionTypes.Update;
|
|
45
|
+
}>;
|
|
44
46
|
/**
|
|
45
47
|
* Sends an event back to the sender of the original event.
|
|
46
48
|
*
|
|
47
49
|
* @param event The event to send back to the sender
|
|
48
50
|
* @param options Options to pass into the send event
|
|
49
51
|
*/
|
|
50
|
-
export declare function respond<TContext, TEvent extends EventObject>(event: Event<TEvent> | SendExpr<TContext, TEvent>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent>;
|
|
52
|
+
export declare function respond<TContext, TEvent extends EventObject, TSentEvent extends EventObject = AnyEventObject>(event: Event<TEvent> | SendExpr<TContext, TEvent, TSentEvent>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, AnyEventObject>;
|
|
51
53
|
/**
|
|
52
54
|
*
|
|
53
55
|
* @param expr The expression function to evaluate which will be logged.
|
|
@@ -119,7 +121,7 @@ export declare function pure<TContext, TEvent extends EventObject>(getActions: (
|
|
|
119
121
|
* @param target The target service to forward the event to.
|
|
120
122
|
* @param options Options to pass into the send action creator.
|
|
121
123
|
*/
|
|
122
|
-
export declare function forwardTo<TContext, TEvent extends EventObject>(target: Required<SendActionOptions<TContext, TEvent>>['to'], options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent>;
|
|
124
|
+
export declare function forwardTo<TContext, TEvent extends EventObject>(target: Required<SendActionOptions<TContext, TEvent>>['to'], options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, AnyEventObject>;
|
|
123
125
|
/**
|
|
124
126
|
* Escalates an error by sending it as an event to this machine's parent.
|
|
125
127
|
*
|
|
@@ -127,7 +129,7 @@ export declare function forwardTo<TContext, TEvent extends EventObject>(target:
|
|
|
127
129
|
* takes in the `context`, `event`, and `meta`, and returns the error data to send.
|
|
128
130
|
* @param options Options to pass into the send action creator.
|
|
129
131
|
*/
|
|
130
|
-
export declare function escalate<TContext, TEvent extends EventObject, TErrorData = any>(errorData: TErrorData | ExprWithMeta<TContext, TEvent, TErrorData>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent>;
|
|
132
|
+
export declare function escalate<TContext, TEvent extends EventObject, TErrorData = any>(errorData: TErrorData | ExprWithMeta<TContext, TEvent, TErrorData>, options?: SendActionOptions<TContext, TEvent>): SendAction<TContext, TEvent, AnyEventObject>;
|
|
131
133
|
export declare function choose<TContext, TEvent extends EventObject>(conds: Array<ChooseConditon<TContext, TEvent>>): ChooseAction<TContext, TEvent>;
|
|
132
134
|
export declare function resolveActions<TContext, TEvent extends EventObject>(machine: StateNode<TContext, any, TEvent>, currentState: State<TContext, TEvent> | undefined, currentContext: TContext, _event: SCXML.Event<TEvent>, actions: Array<ActionObject<TContext, TEvent>>): [Array<ActionObject<TContext, TEvent>>, TContext];
|
|
133
135
|
//# sourceMappingURL=actions.d.ts.map
|