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/es/StateNode.js
CHANGED
|
@@ -8,6 +8,7 @@ import { start as start$1, stop as stop$1, invoke, update, nullEvent, raise as r
|
|
|
8
8
|
import { done, start, raise, stop, toActionObjects, resolveActions, doneInvoke, error, toActionObject, toActivityDefinition, after, send, cancel, initEvent } from './actions.js';
|
|
9
9
|
import { State, stateValuesEqual } from './State.js';
|
|
10
10
|
import { createInvocableActor } from './Actor.js';
|
|
11
|
+
import { toInvokeDefinition } from './invokeUtils.js';
|
|
11
12
|
var NULL_EVENT = '';
|
|
12
13
|
var STATE_IDENTIFIER = '#';
|
|
13
14
|
var WILDCARD = '*';
|
|
@@ -124,10 +125,10 @@ function () {
|
|
|
124
125
|
dfs(this); // History config
|
|
125
126
|
|
|
126
127
|
this.history = this.config.history === true ? 'shallow' : this.config.history || false;
|
|
127
|
-
this._transient = !this.config.on ? false : Array.isArray(this.config.on) ? this.config.on.some(function (_a) {
|
|
128
|
+
this._transient = !!this.config.always || (!this.config.on ? false : Array.isArray(this.config.on) ? this.config.on.some(function (_a) {
|
|
128
129
|
var event = _a.event;
|
|
129
130
|
return event === NULL_EVENT;
|
|
130
|
-
}) : NULL_EVENT in this.config.on;
|
|
131
|
+
}) : NULL_EVENT in this.config.on);
|
|
131
132
|
this.strict = !!this.config.strict; // TODO: deprecate (entry)
|
|
132
133
|
|
|
133
134
|
this.onEntry = toArray(this.config.entry || this.config.onEntry).map(function (action) {
|
|
@@ -138,39 +139,47 @@ function () {
|
|
|
138
139
|
return toActionObject(action);
|
|
139
140
|
});
|
|
140
141
|
this.meta = this.config.meta;
|
|
141
|
-
this.
|
|
142
|
+
this.doneData = this.type === 'final' ? this.config.data : undefined;
|
|
142
143
|
this.invoke = toArray(this.config.invoke).map(function (invokeConfig, i) {
|
|
143
144
|
var _a, _b;
|
|
144
145
|
|
|
145
146
|
if (isMachine(invokeConfig)) {
|
|
146
147
|
_this.machine.options.services = __assign((_a = {}, _a[invokeConfig.id] = invokeConfig, _a), _this.machine.options.services);
|
|
147
|
-
return {
|
|
148
|
-
type: invoke,
|
|
148
|
+
return toInvokeDefinition({
|
|
149
149
|
src: invokeConfig.id,
|
|
150
150
|
id: invokeConfig.id
|
|
151
|
-
};
|
|
152
|
-
} else if (
|
|
151
|
+
});
|
|
152
|
+
} else if (isString(invokeConfig.src)) {
|
|
153
|
+
return toInvokeDefinition(__assign(__assign({}, invokeConfig), {
|
|
154
|
+
id: invokeConfig.id || invokeConfig.src,
|
|
155
|
+
src: invokeConfig.src
|
|
156
|
+
}));
|
|
157
|
+
} else if (isMachine(invokeConfig.src) || isFunction(invokeConfig.src)) {
|
|
153
158
|
var invokeSrc = _this.id + ":invocation[" + i + "]"; // TODO: util function
|
|
154
159
|
|
|
155
160
|
_this.machine.options.services = __assign((_b = {}, _b[invokeSrc] = invokeConfig.src, _b), _this.machine.options.services);
|
|
156
|
-
return __assign(__assign({
|
|
157
|
-
type: invoke,
|
|
161
|
+
return toInvokeDefinition(__assign(__assign({
|
|
158
162
|
id: invokeSrc
|
|
159
163
|
}, invokeConfig), {
|
|
160
164
|
src: invokeSrc
|
|
161
|
-
});
|
|
165
|
+
}));
|
|
162
166
|
} else {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
id:
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
var invokeSource = invokeConfig.src;
|
|
168
|
+
return toInvokeDefinition(__assign(__assign({
|
|
169
|
+
id: invokeSource.type
|
|
170
|
+
}, invokeConfig), {
|
|
171
|
+
src: invokeSource
|
|
172
|
+
}));
|
|
168
173
|
}
|
|
169
174
|
});
|
|
170
175
|
this.activities = toArray(this.config.activities).concat(this.invoke).map(function (activity) {
|
|
171
176
|
return toActivityDefinition(activity);
|
|
172
177
|
});
|
|
173
|
-
this.transition = this.transition.bind(this);
|
|
178
|
+
this.transition = this.transition.bind(this); // TODO: this is the real fix for initialization once
|
|
179
|
+
// state node getters are deprecated
|
|
180
|
+
// if (!this.parent) {
|
|
181
|
+
// this._init();
|
|
182
|
+
// }
|
|
174
183
|
}
|
|
175
184
|
|
|
176
185
|
StateNode.prototype._init = function () {
|
|
@@ -243,11 +252,11 @@ function () {
|
|
|
243
252
|
activities: this.activities || [],
|
|
244
253
|
meta: this.meta,
|
|
245
254
|
order: this.order || -1,
|
|
246
|
-
data: this.
|
|
255
|
+
data: this.doneData,
|
|
247
256
|
invoke: this.invoke
|
|
248
257
|
};
|
|
249
258
|
},
|
|
250
|
-
enumerable:
|
|
259
|
+
enumerable: false,
|
|
251
260
|
configurable: true
|
|
252
261
|
});
|
|
253
262
|
|
|
@@ -271,14 +280,14 @@ function () {
|
|
|
271
280
|
return map;
|
|
272
281
|
}, {});
|
|
273
282
|
},
|
|
274
|
-
enumerable:
|
|
283
|
+
enumerable: false,
|
|
275
284
|
configurable: true
|
|
276
285
|
});
|
|
277
286
|
Object.defineProperty(StateNode.prototype, "after", {
|
|
278
287
|
get: function () {
|
|
279
288
|
return this.__cache.delayedTransitions || (this.__cache.delayedTransitions = this.getDelayedTransitions(), this.__cache.delayedTransitions);
|
|
280
289
|
},
|
|
281
|
-
enumerable:
|
|
290
|
+
enumerable: false,
|
|
282
291
|
configurable: true
|
|
283
292
|
});
|
|
284
293
|
Object.defineProperty(StateNode.prototype, "transitions", {
|
|
@@ -288,7 +297,7 @@ function () {
|
|
|
288
297
|
get: function () {
|
|
289
298
|
return this.__cache.transitions || (this.__cache.transitions = this.formatTransitions(), this.__cache.transitions);
|
|
290
299
|
},
|
|
291
|
-
enumerable:
|
|
300
|
+
enumerable: false,
|
|
292
301
|
configurable: true
|
|
293
302
|
});
|
|
294
303
|
|
|
@@ -714,15 +723,15 @@ function () {
|
|
|
714
723
|
return events;
|
|
715
724
|
}
|
|
716
725
|
|
|
717
|
-
events.push(done(sn.id, sn.
|
|
718
|
-
done(parent.id, sn.
|
|
726
|
+
events.push(done(sn.id, sn.doneData), // TODO: deprecate - final states should not emit done events for their own state.
|
|
727
|
+
done(parent.id, sn.doneData ? mapContext(sn.doneData, currentContext, _event) : undefined));
|
|
719
728
|
var grandparent = parent.parent;
|
|
720
729
|
|
|
721
730
|
if (grandparent.type === 'parallel') {
|
|
722
731
|
if (getChildren(grandparent).every(function (parentNode) {
|
|
723
732
|
return isInFinalState(transition.configuration, parentNode);
|
|
724
733
|
})) {
|
|
725
|
-
events.push(done(grandparent.id
|
|
734
|
+
events.push(done(grandparent.id));
|
|
726
735
|
}
|
|
727
736
|
}
|
|
728
737
|
|
|
@@ -807,6 +816,7 @@ function () {
|
|
|
807
816
|
|
|
808
817
|
var currentActions = state.actions;
|
|
809
818
|
state = this.transition(state, _event); // Save original event to state
|
|
819
|
+
// TODO: this should be the raised event! Delete in V5 (breaking)
|
|
810
820
|
|
|
811
821
|
state._event = originalEvent;
|
|
812
822
|
state.event = originalEvent.data;
|
|
@@ -819,6 +829,8 @@ function () {
|
|
|
819
829
|
StateNode.prototype.resolveTransition = function (stateTransition, currentState, _event, context) {
|
|
820
830
|
var e_6, _a;
|
|
821
831
|
|
|
832
|
+
var _this = this;
|
|
833
|
+
|
|
822
834
|
if (_event === void 0) {
|
|
823
835
|
_event = initEvent;
|
|
824
836
|
}
|
|
@@ -843,9 +855,9 @@ function () {
|
|
|
843
855
|
var action = actions_1_1.value;
|
|
844
856
|
|
|
845
857
|
if (action.type === start$1) {
|
|
846
|
-
activities[action.activity.type] = action;
|
|
858
|
+
activities[action.activity.id || action.activity.type] = action;
|
|
847
859
|
} else if (action.type === stop$1) {
|
|
848
|
-
activities[action.activity.type] = false;
|
|
860
|
+
activities[action.activity.id || action.activity.type] = false;
|
|
849
861
|
}
|
|
850
862
|
}
|
|
851
863
|
} catch (e_6_1) {
|
|
@@ -874,7 +886,7 @@ function () {
|
|
|
874
886
|
return action.type === start$1 && action.activity.type === invoke;
|
|
875
887
|
});
|
|
876
888
|
var children = invokeActions.reduce(function (acc, action) {
|
|
877
|
-
acc[action.activity.id] = createInvocableActor(action.activity);
|
|
889
|
+
acc[action.activity.id] = createInvocableActor(action.activity, _this.machine, updatedContext, _event);
|
|
878
890
|
return acc;
|
|
879
891
|
}, currentState ? __assign({}, currentState.children) : {});
|
|
880
892
|
var resolvedConfiguration = resolvedStateValue ? stateTransition.configuration : currentState ? currentState.configuration : [];
|
|
@@ -1105,7 +1117,7 @@ function () {
|
|
|
1105
1117
|
this.__cache.initialStateValue = initialStateValue;
|
|
1106
1118
|
return this.__cache.initialStateValue;
|
|
1107
1119
|
},
|
|
1108
|
-
enumerable:
|
|
1120
|
+
enumerable: false,
|
|
1109
1121
|
configurable: true
|
|
1110
1122
|
});
|
|
1111
1123
|
|
|
@@ -1127,7 +1139,8 @@ function () {
|
|
|
1127
1139
|
* entering the initial state.
|
|
1128
1140
|
*/
|
|
1129
1141
|
get: function () {
|
|
1130
|
-
this._init();
|
|
1142
|
+
this._init(); // TODO: this should be in the constructor (see note in constructor)
|
|
1143
|
+
|
|
1131
1144
|
|
|
1132
1145
|
var initialStateValue = this.initialStateValue;
|
|
1133
1146
|
|
|
@@ -1137,7 +1150,7 @@ function () {
|
|
|
1137
1150
|
|
|
1138
1151
|
return this.getInitialState(initialStateValue);
|
|
1139
1152
|
},
|
|
1140
|
-
enumerable:
|
|
1153
|
+
enumerable: false,
|
|
1141
1154
|
configurable: true
|
|
1142
1155
|
});
|
|
1143
1156
|
Object.defineProperty(StateNode.prototype, "target", {
|
|
@@ -1160,7 +1173,7 @@ function () {
|
|
|
1160
1173
|
|
|
1161
1174
|
return target;
|
|
1162
1175
|
},
|
|
1163
|
-
enumerable:
|
|
1176
|
+
enumerable: false,
|
|
1164
1177
|
configurable: true
|
|
1165
1178
|
});
|
|
1166
1179
|
/**
|
|
@@ -1201,7 +1214,7 @@ function () {
|
|
|
1201
1214
|
return _this.getFromRelativePath(initialPath);
|
|
1202
1215
|
}));
|
|
1203
1216
|
},
|
|
1204
|
-
enumerable:
|
|
1217
|
+
enumerable: false,
|
|
1205
1218
|
configurable: true
|
|
1206
1219
|
});
|
|
1207
1220
|
/**
|
|
@@ -1303,7 +1316,7 @@ function () {
|
|
|
1303
1316
|
}));
|
|
1304
1317
|
return [this.id].concat(childStateIds);
|
|
1305
1318
|
},
|
|
1306
|
-
enumerable:
|
|
1319
|
+
enumerable: false,
|
|
1307
1320
|
configurable: true
|
|
1308
1321
|
});
|
|
1309
1322
|
Object.defineProperty(StateNode.prototype, "events", {
|
|
@@ -1360,7 +1373,7 @@ function () {
|
|
|
1360
1373
|
|
|
1361
1374
|
return this.__cache.events = Array.from(events);
|
|
1362
1375
|
},
|
|
1363
|
-
enumerable:
|
|
1376
|
+
enumerable: false,
|
|
1364
1377
|
configurable: true
|
|
1365
1378
|
});
|
|
1366
1379
|
Object.defineProperty(StateNode.prototype, "ownEvents", {
|
|
@@ -1377,7 +1390,7 @@ function () {
|
|
|
1377
1390
|
}));
|
|
1378
1391
|
return Array.from(events);
|
|
1379
1392
|
},
|
|
1380
|
-
enumerable:
|
|
1393
|
+
enumerable: false,
|
|
1381
1394
|
configurable: true
|
|
1382
1395
|
});
|
|
1383
1396
|
|
|
@@ -1433,19 +1446,17 @@ function () {
|
|
|
1433
1446
|
target: target,
|
|
1434
1447
|
source: this,
|
|
1435
1448
|
internal: internal,
|
|
1436
|
-
eventType: transitionConfig.event
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
Object.defineProperty(transition, 'toJSON', {
|
|
1440
|
-
value: function () {
|
|
1449
|
+
eventType: transitionConfig.event,
|
|
1450
|
+
toJSON: function () {
|
|
1441
1451
|
return __assign(__assign({}, transition), {
|
|
1442
1452
|
target: transition.target ? transition.target.map(function (t) {
|
|
1443
1453
|
return "#" + t.id;
|
|
1444
1454
|
}) : undefined,
|
|
1445
|
-
source: "#
|
|
1455
|
+
source: "#" + _this.id
|
|
1446
1456
|
});
|
|
1447
1457
|
}
|
|
1448
1458
|
});
|
|
1459
|
+
|
|
1449
1460
|
return transition;
|
|
1450
1461
|
};
|
|
1451
1462
|
|
|
@@ -1465,20 +1476,30 @@ function () {
|
|
|
1465
1476
|
_c = WILDCARD,
|
|
1466
1477
|
_d = _b[_c],
|
|
1467
1478
|
wildcardConfigs = _d === void 0 ? [] : _d,
|
|
1468
|
-
|
|
1479
|
+
strictTransitionConfigs_1 = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
|
|
1480
|
+
|
|
1481
|
+
onConfig = flatten(keys(strictTransitionConfigs_1).map(function (key) {
|
|
1482
|
+
if (!IS_PRODUCTION && key === NULL_EVENT) {
|
|
1483
|
+
warn(false, "Empty string transition configs (e.g., `{ on: { '': ... }}`) for transient transitions are deprecated. Specify the transition in the `{ always: ... }` property instead. " + ("Please check the `on` configuration for \"#" + _this.id + "\"."));
|
|
1484
|
+
}
|
|
1469
1485
|
|
|
1470
|
-
|
|
1471
|
-
var arrayified = toTransitionConfigArray(key, strictOnConfigs_1[key]);
|
|
1486
|
+
var transitionConfigArray = toTransitionConfigArray(key, strictTransitionConfigs_1[key]);
|
|
1472
1487
|
|
|
1473
1488
|
if (!IS_PRODUCTION) {
|
|
1474
|
-
validateArrayifiedTransitions(_this, key,
|
|
1489
|
+
validateArrayifiedTransitions(_this, key, transitionConfigArray);
|
|
1475
1490
|
}
|
|
1476
1491
|
|
|
1477
|
-
return
|
|
1492
|
+
return transitionConfigArray;
|
|
1478
1493
|
}).concat(toTransitionConfigArray(WILDCARD, wildcardConfigs)));
|
|
1479
1494
|
}
|
|
1480
1495
|
|
|
1496
|
+
var eventlessConfig = this.config.always ? toTransitionConfigArray('', this.config.always) : [];
|
|
1481
1497
|
var doneConfig = this.config.onDone ? toTransitionConfigArray(String(done(this.id)), this.config.onDone) : [];
|
|
1498
|
+
|
|
1499
|
+
if (!IS_PRODUCTION) {
|
|
1500
|
+
warn(!(this.config.onDone && !this.parent), "Root nodes cannot have an \".onDone\" transition. Please check the config of \"" + this.id + "\".");
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1482
1503
|
var invokeConfig = flatten(this.invoke.map(function (invokeDef) {
|
|
1483
1504
|
var settleTransitions = [];
|
|
1484
1505
|
|
|
@@ -1493,7 +1514,7 @@ function () {
|
|
|
1493
1514
|
return settleTransitions;
|
|
1494
1515
|
}));
|
|
1495
1516
|
var delayedTransitions = this.after;
|
|
1496
|
-
var formattedTransitions = flatten(__spread(doneConfig, invokeConfig, onConfig).map(function (transitionConfig) {
|
|
1517
|
+
var formattedTransitions = flatten(__spread(doneConfig, invokeConfig, onConfig, eventlessConfig).map(function (transitionConfig) {
|
|
1497
1518
|
return toArray(transitionConfig).map(function (transition) {
|
|
1498
1519
|
return _this.formatTransition(transition);
|
|
1499
1520
|
});
|
package/es/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
|
package/es/actions.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { __assign, __read
|
|
1
|
+
import { __assign, __read } from './_virtual/_tslib.js';
|
|
2
2
|
import { IS_PRODUCTION } from './environment.js';
|
|
3
3
|
import { toSCXMLEvent, isFunction, toEventObject, getEventType, isString, partition, updateContext, flatten, toArray, toGuard, evaluateGuard, warn, isArray } from './utils.js';
|
|
4
4
|
import { SpecialTargets, ActionTypes } from './types.js';
|
|
@@ -44,12 +44,10 @@ function toActionObject(action, actionFunctionMap) {
|
|
|
44
44
|
exec: exec
|
|
45
45
|
});
|
|
46
46
|
} else if (exec) {
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
type: type
|
|
52
|
-
}, exec), other);
|
|
47
|
+
var actionType = exec.type || action.type;
|
|
48
|
+
actionObject = __assign(__assign(__assign({}, exec), action), {
|
|
49
|
+
type: actionType
|
|
50
|
+
});
|
|
53
51
|
} else {
|
|
54
52
|
actionObject = action;
|
|
55
53
|
}
|
|
@@ -450,7 +448,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
450
448
|
return [];
|
|
451
449
|
}
|
|
452
450
|
|
|
453
|
-
var resolved = resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions)));
|
|
451
|
+
var resolved = resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions));
|
|
454
452
|
updatedContext = resolved[1];
|
|
455
453
|
return resolved[0];
|
|
456
454
|
}
|
|
@@ -463,7 +461,7 @@ function resolveActions(machine, currentState, currentContext, _event, actions)
|
|
|
463
461
|
return [];
|
|
464
462
|
}
|
|
465
463
|
|
|
466
|
-
var resolved = resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions)));
|
|
464
|
+
var resolved = resolveActions(machine, currentState, updatedContext, _event, toActionObjects(toArray(matchedActions), machine.options.actions));
|
|
467
465
|
updatedContext = resolved[1];
|
|
468
466
|
return resolved[0];
|
|
469
467
|
}
|
package/es/interpreter.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
|
|
1
|
+
import { StateMachine, Event, EventObject, DefaultContext, StateSchema, StateValue, InterpreterOptions, SingleOrArray, DoneEvent, Unsubscribable, MachineOptions, SCXML, EventData, Observer, Spawnable, Typestate } from './types';
|
|
2
2
|
import { State } from './State';
|
|
3
3
|
import { Actor } from './Actor';
|
|
4
|
-
export declare type StateListener<TContext, TEvent extends EventObject,
|
|
4
|
+
export declare type StateListener<TContext, TEvent extends EventObject, TStateSchema extends StateSchema<TContext> = any, TTypestate extends Typestate<TContext> = {
|
|
5
|
+
value: any;
|
|
6
|
+
context: TContext;
|
|
7
|
+
}> = (state: State<TContext, TEvent, TStateSchema, TTypestate>, event: TEvent) => void;
|
|
5
8
|
export declare type ContextListener<TContext = DefaultContext> = (context: TContext, prevContext: TContext | undefined) => void;
|
|
6
9
|
export declare type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
|
|
7
10
|
export declare type Listener = () => void;
|
|
@@ -14,7 +17,10 @@ interface SpawnOptions {
|
|
|
14
17
|
autoForward?: boolean;
|
|
15
18
|
sync?: boolean;
|
|
16
19
|
}
|
|
17
|
-
export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> =
|
|
20
|
+
export declare class Interpreter<TContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
|
|
21
|
+
value: any;
|
|
22
|
+
context: TContext;
|
|
23
|
+
}> implements Actor<State<TContext, TEvent, TStateSchema, TTypestate>, TEvent> {
|
|
18
24
|
machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>;
|
|
19
25
|
/**
|
|
20
26
|
* The default interpreter options:
|
|
@@ -63,8 +69,8 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
63
69
|
* @param options Interpreter options
|
|
64
70
|
*/
|
|
65
71
|
constructor(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>);
|
|
66
|
-
get initialState(): State<TContext, TEvent>;
|
|
67
|
-
get state(): State<TContext, TEvent,
|
|
72
|
+
get initialState(): State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
73
|
+
get state(): State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
68
74
|
static interpret: typeof interpret;
|
|
69
75
|
/**
|
|
70
76
|
* Executes the actions of the given state, with that state's `context` and `event`.
|
|
@@ -72,56 +78,56 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
72
78
|
* @param state The state whose actions will be executed
|
|
73
79
|
* @param actionsConfig The action implementations to use
|
|
74
80
|
*/
|
|
75
|
-
execute(state: State<TContext, TEvent>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
|
|
81
|
+
execute(state: State<TContext, TEvent, TStateSchema, TTypestate>, actionsConfig?: MachineOptions<TContext, TEvent>['actions']): void;
|
|
76
82
|
private update;
|
|
77
|
-
onTransition(listener: StateListener<TContext, TEvent, TTypestate>): this;
|
|
83
|
+
onTransition(listener: StateListener<TContext, TEvent, TStateSchema, TTypestate>): this;
|
|
78
84
|
subscribe(observer: Observer<State<TContext, TEvent, any, TTypestate>>): Unsubscribable;
|
|
79
85
|
subscribe(nextListener?: (state: State<TContext, TEvent, any, TTypestate>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Unsubscribable;
|
|
80
86
|
/**
|
|
81
87
|
* Adds an event listener that is notified whenever an event is sent to the running interpreter.
|
|
82
88
|
* @param listener The event listener
|
|
83
89
|
*/
|
|
84
|
-
onEvent(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent>;
|
|
90
|
+
onEvent(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
85
91
|
/**
|
|
86
92
|
* Adds an event listener that is notified whenever a `send` event occurs.
|
|
87
93
|
* @param listener The event listener
|
|
88
94
|
*/
|
|
89
|
-
onSend(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent>;
|
|
95
|
+
onSend(listener: EventListener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
90
96
|
/**
|
|
91
97
|
* Adds a context listener that is notified whenever the state context changes.
|
|
92
98
|
* @param listener The context listener
|
|
93
99
|
*/
|
|
94
|
-
onChange(listener: ContextListener<TContext>): Interpreter<TContext, TStateSchema, TEvent>;
|
|
100
|
+
onChange(listener: ContextListener<TContext>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
95
101
|
/**
|
|
96
102
|
* Adds a listener that is notified when the machine is stopped.
|
|
97
103
|
* @param listener The listener
|
|
98
104
|
*/
|
|
99
|
-
onStop(listener: Listener): Interpreter<TContext, TStateSchema, TEvent>;
|
|
105
|
+
onStop(listener: Listener): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
100
106
|
/**
|
|
101
107
|
* Adds a state listener that is notified when the statechart has reached its final state.
|
|
102
108
|
* @param listener The state listener
|
|
103
109
|
*/
|
|
104
|
-
onDone(listener: EventListener<DoneEvent>): Interpreter<TContext, TStateSchema, TEvent>;
|
|
110
|
+
onDone(listener: EventListener<DoneEvent>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
105
111
|
/**
|
|
106
112
|
* Removes a listener.
|
|
107
113
|
* @param listener The listener to remove
|
|
108
114
|
*/
|
|
109
|
-
off(listener: (...args: any[]) => void): Interpreter<TContext, TStateSchema, TEvent>;
|
|
115
|
+
off(listener: (...args: any[]) => void): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
110
116
|
/**
|
|
111
117
|
* Alias for Interpreter.prototype.start
|
|
112
118
|
*/
|
|
113
|
-
init: (initialState?: string | State<TContext, TEvent,
|
|
119
|
+
init: (initialState?: string | import("./types").StateValueMap | State<TContext, TEvent, TStateSchema, TTypestate> | undefined) => Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
114
120
|
/**
|
|
115
121
|
* Starts the interpreter from the given state, or the initial state.
|
|
116
122
|
* @param initialState The state to start the statechart from
|
|
117
123
|
*/
|
|
118
|
-
start(initialState?: State<TContext, TEvent> | StateValue): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
124
|
+
start(initialState?: State<TContext, TEvent, TStateSchema, TTypestate> | StateValue): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
119
125
|
/**
|
|
120
126
|
* Stops the interpreter and unsubscribe all listeners.
|
|
121
127
|
*
|
|
122
128
|
* This will also notify the `onStop` listeners.
|
|
123
129
|
*/
|
|
124
|
-
stop(): Interpreter<TContext, TStateSchema, TEvent>;
|
|
130
|
+
stop(): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
125
131
|
/**
|
|
126
132
|
* Sends an event to the running interpreter to trigger a transition.
|
|
127
133
|
*
|
|
@@ -131,14 +137,14 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
131
137
|
*
|
|
132
138
|
* @param event The event(s) to send
|
|
133
139
|
*/
|
|
134
|
-
send: (event:
|
|
140
|
+
send: (event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>, payload?: EventData | undefined) => State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
135
141
|
private batch;
|
|
136
142
|
/**
|
|
137
143
|
* Returns a send function bound to this interpreter instance.
|
|
138
144
|
*
|
|
139
145
|
* @param event The event to be sent by the sender.
|
|
140
146
|
*/
|
|
141
|
-
sender(event: Event<TEvent>): () => State<TContext, TEvent>;
|
|
147
|
+
sender(event: Event<TEvent>): () => State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
142
148
|
private sendTo;
|
|
143
149
|
/**
|
|
144
150
|
* Returns the next state given the interpreter's current state and the event.
|
|
@@ -147,7 +153,7 @@ export declare class Interpreter<TContext, TStateSchema extends StateSchema = an
|
|
|
147
153
|
*
|
|
148
154
|
* @param event The event to determine the next state
|
|
149
155
|
*/
|
|
150
|
-
nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent>;
|
|
156
|
+
nextState(event: Event<TEvent> | SCXML.Event<TEvent>): State<TContext, TEvent, TStateSchema, TTypestate>;
|
|
151
157
|
private forward;
|
|
152
158
|
private defer;
|
|
153
159
|
private cancel;
|
|
@@ -179,6 +185,9 @@ export declare function spawn(entity: Spawnable, nameOrOptions?: string | SpawnO
|
|
|
179
185
|
* @param machine The machine to interpret
|
|
180
186
|
* @param options Interpreter options
|
|
181
187
|
*/
|
|
182
|
-
export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> =
|
|
188
|
+
export declare function interpret<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
|
|
189
|
+
value: any;
|
|
190
|
+
context: TContext;
|
|
191
|
+
}>(machine: StateMachine<TContext, TStateSchema, TEvent, TTypestate>, options?: Partial<InterpreterOptions>): Interpreter<TContext, TStateSchema, TEvent, TTypestate>;
|
|
183
192
|
export {};
|
|
184
193
|
//# sourceMappingURL=interpreter.d.ts.map
|