xstate 4.31.0 → 5.0.0-alpha.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 +526 -0
- package/actions/ExecutableAction.ts +58 -0
- package/actions/dist/xstate-actions.cjs.d.ts +1 -0
- package/actions/dist/xstate-actions.cjs.dev.js +32 -0
- package/actions/dist/xstate-actions.cjs.js +7 -0
- package/actions/dist/xstate-actions.cjs.prod.js +32 -0
- package/actions/dist/xstate-actions.esm.js +2 -0
- package/actions/dynamicAction.ts +34 -0
- package/actions/package.json +4 -0
- package/actors/dist/xstate-actors.cjs.d.ts +1 -0
- package/actors/dist/xstate-actors.cjs.dev.js +21 -0
- package/actors/dist/xstate-actors.cjs.js +7 -0
- package/actors/dist/xstate-actors.cjs.prod.js +21 -0
- package/actors/dist/xstate-actors.esm.js +2 -0
- package/actors/package.json +4 -0
- package/dev/dist/xstate-dev.cjs.d.ts +1 -0
- package/dev/dist/xstate-dev.cjs.dev.js +11 -0
- package/dev/dist/xstate-dev.cjs.js +7 -0
- package/{lib/devTools.js → dev/dist/xstate-dev.cjs.prod.js} +17 -10
- package/dev/dist/xstate-dev.esm.js +1 -0
- package/dev/package.json +4 -0
- package/dist/Machine-9ce74c11.cjs.dev.js +755 -0
- package/dist/Machine-dca041dc.cjs.prod.js +749 -0
- package/dist/Machine-fb2f47f5.esm.js +751 -0
- package/dist/actions-6b86876d.cjs.dev.js +4815 -0
- package/dist/actions-70094c93.cjs.prod.js +4765 -0
- package/dist/actions-ff512f16.esm.js +4757 -0
- package/dist/declarations/actions/ExecutableAction.d.ts +23 -0
- package/dist/declarations/actions/dynamicAction.d.ts +3 -0
- package/dist/declarations/src/Machine.d.ts +4 -0
- package/dist/declarations/src/Mailbox.d.ts +12 -0
- package/dist/declarations/src/ObservableActorRef.d.ts +25 -0
- package/{lib → dist/declarations/src}/SimulatedClock.d.ts +0 -1
- package/dist/declarations/src/State.d.ts +104 -0
- package/dist/declarations/src/StateMachine.d.ts +112 -0
- package/dist/declarations/src/StateNode.d.ts +127 -0
- package/{es → dist/declarations/src}/actionTypes.d.ts +0 -3
- package/dist/declarations/src/actions/assign.d.ts +7 -0
- package/dist/declarations/src/actions/cancel.d.ts +10 -0
- package/dist/declarations/src/actions/choose.d.ts +3 -0
- package/dist/declarations/src/actions/invoke.d.ts +3 -0
- package/dist/declarations/src/actions/log.d.ts +11 -0
- package/dist/declarations/src/actions/pure.d.ts +3 -0
- package/dist/declarations/src/actions/raise.d.ts +9 -0
- package/dist/declarations/src/actions/send.d.ts +55 -0
- package/dist/declarations/src/actions/stop.d.ts +8 -0
- package/dist/declarations/src/actions.d.ts +44 -0
- package/dist/declarations/src/actors.d.ts +38 -0
- package/dist/declarations/src/constants.d.ts +5 -0
- package/{lib/devTools.d.ts → dist/declarations/src/dev/index.d.ts} +2 -2
- package/{es → dist/declarations/src}/environment.d.ts +0 -1
- package/dist/declarations/src/guards.d.ts +9 -0
- package/dist/declarations/src/index.d.ts +47 -0
- package/dist/declarations/src/interpreter.d.ts +147 -0
- package/{es → dist/declarations/src}/mapState.d.ts +0 -1
- package/dist/declarations/src/match.d.ts +5 -0
- package/dist/declarations/src/memo.d.ts +1 -0
- package/dist/declarations/src/model.d.ts +5 -0
- package/{lib → dist/declarations/src}/model.types.d.ts +10 -9
- package/dist/declarations/src/registry.d.ts +8 -0
- package/{es → dist/declarations/src}/schema.d.ts +0 -1
- package/dist/declarations/src/spawn.d.ts +2 -0
- package/dist/declarations/src/stateUtils.d.ts +85 -0
- package/{lib → dist/declarations/src}/typegenTypes.d.ts +28 -26
- package/dist/declarations/src/types.d.ts +1026 -0
- package/dist/declarations/src/utils.d.ts +56 -0
- package/{es/devTools.js → dist/index-20b364c4.esm.js} +18 -6
- package/dist/index-2a564d03.cjs.dev.js +64 -0
- package/dist/xstate.cjs.d.ts +1 -0
- package/dist/xstate.cjs.dev.js +217 -0
- package/dist/xstate.cjs.js +7 -0
- package/dist/xstate.cjs.prod.js +217 -0
- package/dist/xstate.esm.js +172 -0
- package/guards/dist/xstate-guards.cjs.d.ts +1 -0
- package/guards/dist/xstate-guards.cjs.dev.js +15 -0
- package/guards/dist/xstate-guards.cjs.js +7 -0
- package/guards/dist/xstate-guards.cjs.prod.js +15 -0
- package/guards/dist/xstate-guards.esm.js +2 -0
- package/guards/package.json +4 -0
- package/invoke/package.json +4 -0
- package/model/dist/xstate-model.cjs.d.ts +1 -0
- package/model/dist/xstate-model.cjs.dev.js +41 -0
- package/model/dist/xstate-model.cjs.js +7 -0
- package/model/dist/xstate-model.cjs.prod.js +41 -0
- package/model/dist/xstate-model.esm.js +37 -0
- package/model/package.json +4 -0
- package/package.json +21 -30
- package/dist/xstate.interpreter.js +0 -15
- package/dist/xstate.js +0 -15
- package/dist/xstate.web.js +0 -15
- package/es/Actor.d.ts +0 -25
- package/es/Actor.js +0 -99
- package/es/Machine.d.ts +0 -12
- package/es/Machine.js +0 -14
- package/es/SimulatedClock.d.ts +0 -17
- package/es/SimulatedClock.js +0 -81
- package/es/State.d.ts +0 -123
- package/es/State.js +0 -274
- package/es/StateNode.d.ts +0 -287
- package/es/StateNode.js +0 -1566
- package/es/_virtual/_tslib.js +0 -78
- package/es/actionTypes.js +0 -22
- package/es/actions.d.ts +0 -154
- package/es/actions.js +0 -520
- package/es/behaviors.d.ts +0 -37
- package/es/behaviors.js +0 -131
- package/es/constants.d.ts +0 -6
- package/es/constants.js +0 -6
- package/es/devTools.d.ts +0 -14
- package/es/each.d.ts +0 -4
- package/es/each.js +0 -13
- package/es/environment.js +0 -3
- package/es/index.d.ts +0 -22
- package/es/index.js +0 -23
- package/es/interpreter.d.ts +0 -209
- package/es/interpreter.js +0 -1402
- package/es/invoke.d.ts +0 -11
- package/es/invokeUtils.d.ts +0 -7
- package/es/invokeUtils.js +0 -40
- package/es/json.d.ts +0 -31
- package/es/json.js +0 -86
- package/es/mapState.js +0 -32
- package/es/match.d.ts +0 -9
- package/es/match.js +0 -34
- package/es/model.d.ts +0 -5
- package/es/model.js +0 -50
- package/es/model.types.d.ts +0 -52
- package/es/patterns.d.ts +0 -14
- package/es/patterns.js +0 -48
- package/es/registry.d.ts +0 -9
- package/es/registry.js +0 -19
- package/es/scheduler.d.ts +0 -17
- package/es/scheduler.js +0 -79
- package/es/schema.js +0 -6
- package/es/scxml.d.ts +0 -6
- package/es/serviceScope.d.ts +0 -4
- package/es/serviceScope.js +0 -16
- package/es/stateUtils.d.ts +0 -17
- package/es/stateUtils.js +0 -259
- package/es/typegenTypes.d.ts +0 -130
- package/es/types.d.ts +0 -1037
- package/es/types.js +0 -33
- package/es/utils.d.ts +0 -72
- package/es/utils.js +0 -626
- package/es/waitFor.d.ts +0 -32
- package/es/waitFor.js +0 -66
- package/lib/Actor.d.ts +0 -25
- package/lib/Actor.js +0 -108
- package/lib/Machine.d.ts +0 -12
- package/lib/Machine.js +0 -19
- package/lib/SimulatedClock.js +0 -85
- package/lib/State.d.ts +0 -123
- package/lib/State.js +0 -282
- package/lib/StateNode.d.ts +0 -287
- package/lib/StateNode.js +0 -1570
- package/lib/_virtual/_tslib.js +0 -85
- package/lib/actionTypes.d.ts +0 -20
- package/lib/actionTypes.js +0 -43
- package/lib/actions.d.ts +0 -154
- package/lib/actions.js +0 -552
- package/lib/behaviors.d.ts +0 -37
- package/lib/behaviors.js +0 -137
- package/lib/constants.d.ts +0 -6
- package/lib/constants.js +0 -13
- package/lib/each.d.ts +0 -4
- package/lib/each.js +0 -17
- package/lib/environment.d.ts +0 -2
- package/lib/environment.js +0 -7
- package/lib/index.d.ts +0 -22
- package/lib/index.js +0 -60
- package/lib/interpreter.d.ts +0 -209
- package/lib/interpreter.js +0 -1408
- package/lib/invoke.d.ts +0 -11
- package/lib/invoke.js +0 -20
- package/lib/invokeUtils.d.ts +0 -7
- package/lib/invokeUtils.js +0 -45
- package/lib/json.d.ts +0 -31
- package/lib/json.js +0 -94
- package/lib/mapState.d.ts +0 -4
- package/lib/mapState.js +0 -36
- package/lib/match.d.ts +0 -9
- package/lib/match.js +0 -38
- package/lib/model.d.ts +0 -5
- package/lib/model.js +0 -54
- package/lib/model.types.js +0 -2
- package/lib/patterns.d.ts +0 -14
- package/lib/patterns.js +0 -53
- package/lib/registry.d.ts +0 -9
- package/lib/registry.js +0 -23
- package/lib/scheduler.d.ts +0 -17
- package/lib/scheduler.js +0 -83
- package/lib/schema.d.ts +0 -3
- package/lib/schema.js +0 -11
- package/lib/scxml.d.ts +0 -6
- package/lib/scxml.js +0 -350
- package/lib/serviceScope.d.ts +0 -4
- package/lib/serviceScope.js +0 -21
- package/lib/stateUtils.d.ts +0 -17
- package/lib/stateUtils.js +0 -273
- package/lib/typegenTypes.js +0 -2
- package/lib/types.d.ts +0 -1037
- package/lib/types.js +0 -35
- package/lib/utils.d.ts +0 -72
- package/lib/utils.js +0 -673
- package/lib/waitFor.d.ts +0 -32
- package/lib/waitFor.js +0 -70
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
import { b as _createClass, J as formatTransitions, K as toTransitionConfigArray, N as NULL_EVENT, L as formatTransition, M as mapValues, P as memo, Q as getEventType, _ as _createForOfIteratorHelper, R as evaluateGuard, d as _toConsumableArray, T as flatten, e as _classCallCheck, f as _defineProperty, t as toArray, U as toActionObject, V as createInvokeId, W as toInvokeConfig, X as _objectSpread2, Y as invoke, Z as _objectWithoutProperties, $ as getDelayedTransitions, a0 as formatInitialTransition, a1 as getCandidates, a2 as toInvokeSource, a3 as isString, a4 as createSpawner, a5 as getConfiguration, B as getStateNodes, a6 as resolveStateValue, a7 as toState, a8 as macrostep, F as toSCXMLEvent, a9 as isBuiltInEvent, aa as resolveMicroTransition, ab as transitionNode, a as _slicedToArray, S as State, ac as getStateValue, ad as isStateId, ae as isFunction, af as STATE_DELIMITER } from './actions-ff512f16.esm.js';
|
|
2
|
+
import { I as IS_PRODUCTION } from './index-20b364c4.esm.js';
|
|
3
|
+
|
|
4
|
+
var _excluded = ["onDone", "onError"];
|
|
5
|
+
var EMPTY_OBJECT = {};
|
|
6
|
+
var StateNode = /*#__PURE__*/function () {
|
|
7
|
+
/**
|
|
8
|
+
* The relative key of the state node, which represents its location in the overall state value.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The unique ID of the state node.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The type of this state node:
|
|
17
|
+
*
|
|
18
|
+
* - `'atomic'` - no child state nodes
|
|
19
|
+
* - `'compound'` - nested child state nodes (XOR)
|
|
20
|
+
* - `'parallel'` - orthogonal nested child state nodes (AND)
|
|
21
|
+
* - `'history'` - history state node
|
|
22
|
+
* - `'final'` - final state node
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The string path from the root machine node to this node.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The child state nodes.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The type of history on this state node. Can be:
|
|
35
|
+
*
|
|
36
|
+
* - `'shallow'` - recalls only top-level historical state value
|
|
37
|
+
* - `'deep'` - recalls historical state value at all levels
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The action(s) to be executed upon entering the state node.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The action(s) to be executed upon exiting the state node.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The parent state node.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The root machine node.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The meta data associated with this state node, which will be returned in State instances.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The data sent with the "done.state._id_" event if this is a final state node.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The order this state node appears. Corresponds to the implicit SCXML document order.
|
|
66
|
+
*/
|
|
67
|
+
function StateNode(
|
|
68
|
+
/**
|
|
69
|
+
* The raw config used to create the machine.
|
|
70
|
+
*/
|
|
71
|
+
config, options) {
|
|
72
|
+
var _this = this;
|
|
73
|
+
|
|
74
|
+
_classCallCheck(this, StateNode);
|
|
75
|
+
|
|
76
|
+
this.config = config;
|
|
77
|
+
|
|
78
|
+
_defineProperty(this, "key", void 0);
|
|
79
|
+
|
|
80
|
+
_defineProperty(this, "id", void 0);
|
|
81
|
+
|
|
82
|
+
_defineProperty(this, "type", void 0);
|
|
83
|
+
|
|
84
|
+
_defineProperty(this, "path", void 0);
|
|
85
|
+
|
|
86
|
+
_defineProperty(this, "states", void 0);
|
|
87
|
+
|
|
88
|
+
_defineProperty(this, "history", void 0);
|
|
89
|
+
|
|
90
|
+
_defineProperty(this, "entry", void 0);
|
|
91
|
+
|
|
92
|
+
_defineProperty(this, "exit", void 0);
|
|
93
|
+
|
|
94
|
+
_defineProperty(this, "parent", void 0);
|
|
95
|
+
|
|
96
|
+
_defineProperty(this, "machine", void 0);
|
|
97
|
+
|
|
98
|
+
_defineProperty(this, "meta", void 0);
|
|
99
|
+
|
|
100
|
+
_defineProperty(this, "doneData", void 0);
|
|
101
|
+
|
|
102
|
+
_defineProperty(this, "order", -1);
|
|
103
|
+
|
|
104
|
+
_defineProperty(this, "description", void 0);
|
|
105
|
+
|
|
106
|
+
_defineProperty(this, "tags", []);
|
|
107
|
+
|
|
108
|
+
_defineProperty(this, "transitions", void 0);
|
|
109
|
+
|
|
110
|
+
_defineProperty(this, "always", void 0);
|
|
111
|
+
|
|
112
|
+
this.parent = options._parent;
|
|
113
|
+
this.key = this.config.key || options._key;
|
|
114
|
+
this.machine = options._machine;
|
|
115
|
+
this.path = this.parent ? this.parent.path.concat(this.key) : [];
|
|
116
|
+
this.id = this.config.id || [this.machine.key].concat(_toConsumableArray(this.path)).join(this.machine.delimiter);
|
|
117
|
+
this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
|
|
118
|
+
this.description = this.config.description;
|
|
119
|
+
this.order = this.machine.idMap.size;
|
|
120
|
+
this.machine.idMap.set(this.id, this);
|
|
121
|
+
this.states = this.config.states ? mapValues(this.config.states, function (stateConfig, key) {
|
|
122
|
+
var stateNode = new StateNode(stateConfig, {
|
|
123
|
+
_parent: _this,
|
|
124
|
+
_key: key,
|
|
125
|
+
_machine: _this.machine
|
|
126
|
+
});
|
|
127
|
+
return stateNode;
|
|
128
|
+
}) : EMPTY_OBJECT;
|
|
129
|
+
|
|
130
|
+
if (this.type === 'compound' && !this.config.initial) {
|
|
131
|
+
throw new Error("No initial state specified for compound state node \"#".concat(this.id, "\". Try adding { initial: \"").concat(Object.keys(this.states)[0], "\" } to the state config."));
|
|
132
|
+
} // History config
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
this.history = this.config.history === true ? 'shallow' : this.config.history || false;
|
|
136
|
+
this.entry = toArray(this.config.entry).map(function (action) {
|
|
137
|
+
return toActionObject(action, _this.machine.options.actions);
|
|
138
|
+
});
|
|
139
|
+
this.exit = toArray(this.config.exit).map(function (action) {
|
|
140
|
+
return toActionObject(action, _this.machine.options.actions);
|
|
141
|
+
});
|
|
142
|
+
this.meta = this.config.meta;
|
|
143
|
+
this.doneData = this.type === 'final' ? this.config.data : undefined;
|
|
144
|
+
this.tags = toArray(config.tags);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_createClass(StateNode, [{
|
|
148
|
+
key: "_initialize",
|
|
149
|
+
value: function _initialize() {
|
|
150
|
+
var _this2 = this;
|
|
151
|
+
|
|
152
|
+
this.transitions = formatTransitions(this);
|
|
153
|
+
|
|
154
|
+
if (this.config.always) {
|
|
155
|
+
this.always = toTransitionConfigArray(NULL_EVENT, this.config.always).map(function (t) {
|
|
156
|
+
return formatTransition(_this2, t);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
Object.keys(this.states).forEach(function (key) {
|
|
161
|
+
_this2.states[key]._initialize();
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The well-structured state node definition.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
}, {
|
|
169
|
+
key: "definition",
|
|
170
|
+
get: function get() {
|
|
171
|
+
var _this3 = this;
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
id: this.id,
|
|
175
|
+
key: this.key,
|
|
176
|
+
version: this.machine.version,
|
|
177
|
+
context: this.machine.context,
|
|
178
|
+
type: this.type,
|
|
179
|
+
initial: this.initial ? {
|
|
180
|
+
target: this.initial.target,
|
|
181
|
+
source: this,
|
|
182
|
+
actions: this.initial.actions,
|
|
183
|
+
eventType: null,
|
|
184
|
+
toJSON: function toJSON() {
|
|
185
|
+
return {
|
|
186
|
+
target: _this3.initial.target.map(function (t) {
|
|
187
|
+
return "#".concat(t.id);
|
|
188
|
+
}),
|
|
189
|
+
source: "#".concat(_this3.id),
|
|
190
|
+
actions: _this3.initial.actions,
|
|
191
|
+
eventType: null
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
} : undefined,
|
|
195
|
+
history: this.history,
|
|
196
|
+
states: mapValues(this.states, function (state) {
|
|
197
|
+
return state.definition;
|
|
198
|
+
}),
|
|
199
|
+
on: this.on,
|
|
200
|
+
transitions: this.transitions,
|
|
201
|
+
entry: this.entry,
|
|
202
|
+
exit: this.exit,
|
|
203
|
+
meta: this.meta,
|
|
204
|
+
order: this.order || -1,
|
|
205
|
+
data: this.doneData,
|
|
206
|
+
invoke: this.invoke,
|
|
207
|
+
description: this.description,
|
|
208
|
+
tags: this.tags
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
}, {
|
|
212
|
+
key: "toJSON",
|
|
213
|
+
value: function toJSON() {
|
|
214
|
+
return this.definition;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The behaviors invoked as actors by this state node.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
}, {
|
|
221
|
+
key: "invoke",
|
|
222
|
+
get: function get() {
|
|
223
|
+
var _this4 = this;
|
|
224
|
+
|
|
225
|
+
return memo(this, 'invoke', function () {
|
|
226
|
+
return toArray(_this4.config.invoke).map(function (invocable, i) {
|
|
227
|
+
var generatedId = createInvokeId(_this4.id, i);
|
|
228
|
+
var invokeConfig = toInvokeConfig(invocable, generatedId);
|
|
229
|
+
var resolvedId = invokeConfig.id || generatedId;
|
|
230
|
+
var src = invokeConfig.src;
|
|
231
|
+
var resolvedSrc = toInvokeSource(isString(src) ? src : !('type' in src) ? resolvedId : src);
|
|
232
|
+
|
|
233
|
+
if (!_this4.machine.options.actors[resolvedId] && typeof src !== 'string' && !('type' in src)) {
|
|
234
|
+
_this4.machine.options.actors = _objectSpread2(_objectSpread2({}, _this4.machine.options.actors), {}, _defineProperty({}, resolvedId, typeof src === 'function' ? src : function () {
|
|
235
|
+
return src;
|
|
236
|
+
}));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return _objectSpread2(_objectSpread2({
|
|
240
|
+
type: invoke
|
|
241
|
+
}, invokeConfig), {}, {
|
|
242
|
+
src: resolvedSrc,
|
|
243
|
+
id: resolvedId,
|
|
244
|
+
toJSON: function toJSON() {
|
|
245
|
+
var onDone = invokeConfig.onDone,
|
|
246
|
+
onError = invokeConfig.onError,
|
|
247
|
+
invokeDefValues = _objectWithoutProperties(invokeConfig, _excluded);
|
|
248
|
+
|
|
249
|
+
return _objectSpread2(_objectSpread2({}, invokeDefValues), {}, {
|
|
250
|
+
type: invoke,
|
|
251
|
+
src: resolvedSrc,
|
|
252
|
+
id: resolvedId
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The mapping of events to transitions.
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
}, {
|
|
264
|
+
key: "on",
|
|
265
|
+
get: function get() {
|
|
266
|
+
var _this5 = this;
|
|
267
|
+
|
|
268
|
+
return memo(this, 'on', function () {
|
|
269
|
+
var transitions = _this5.transitions;
|
|
270
|
+
return transitions.reduce(function (map, transition) {
|
|
271
|
+
map[transition.eventType] = map[transition.eventType] || [];
|
|
272
|
+
map[transition.eventType].push(transition);
|
|
273
|
+
return map;
|
|
274
|
+
}, {});
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}, {
|
|
278
|
+
key: "after",
|
|
279
|
+
get: function get() {
|
|
280
|
+
var _this6 = this;
|
|
281
|
+
|
|
282
|
+
return memo(this, 'delayedTransitions', function () {
|
|
283
|
+
return getDelayedTransitions(_this6);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}, {
|
|
287
|
+
key: "initial",
|
|
288
|
+
get: function get() {
|
|
289
|
+
var _this7 = this;
|
|
290
|
+
|
|
291
|
+
return memo(this, 'initial', function () {
|
|
292
|
+
return formatInitialTransition(_this7, _this7.config.initial || []);
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Returns `true` if this state node explicitly handles the given event.
|
|
297
|
+
*
|
|
298
|
+
* @param event The event in question
|
|
299
|
+
*/
|
|
300
|
+
|
|
301
|
+
}, {
|
|
302
|
+
key: "handles",
|
|
303
|
+
value: function handles(event) {
|
|
304
|
+
var eventType = getEventType(event);
|
|
305
|
+
return this.events.includes(eventType);
|
|
306
|
+
}
|
|
307
|
+
}, {
|
|
308
|
+
key: "next",
|
|
309
|
+
value: function next(state, _event) {
|
|
310
|
+
var _this8 = this;
|
|
311
|
+
|
|
312
|
+
var eventName = _event.name;
|
|
313
|
+
var actions = [];
|
|
314
|
+
var selectedTransition;
|
|
315
|
+
var candidates = memo(this, "candidates-".concat(eventName.toString()), function () {
|
|
316
|
+
return getCandidates(_this8, eventName, _this8.machine.config.scxml // Whether token matching should be used
|
|
317
|
+
);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
var _iterator = _createForOfIteratorHelper(candidates),
|
|
321
|
+
_step;
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
325
|
+
var candidate = _step.value;
|
|
326
|
+
var guard = candidate.guard;
|
|
327
|
+
var resolvedContext = state.context;
|
|
328
|
+
var guardPassed = false;
|
|
329
|
+
|
|
330
|
+
try {
|
|
331
|
+
guardPassed = !guard || evaluateGuard(guard, resolvedContext, _event, state, this.machine);
|
|
332
|
+
} catch (err) {
|
|
333
|
+
throw new Error("Unable to evaluate guard '".concat(guard.type, "' in transition for event '").concat(eventName, "' in state node '").concat(this.id, "':\n").concat(err.message));
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (guardPassed) {
|
|
337
|
+
actions.push.apply(actions, _toConsumableArray(candidate.actions));
|
|
338
|
+
selectedTransition = candidate;
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} catch (err) {
|
|
343
|
+
_iterator.e(err);
|
|
344
|
+
} finally {
|
|
345
|
+
_iterator.f();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return selectedTransition ? [selectedTransition] : undefined;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* The target state value of the history state node, if it exists. This represents the
|
|
352
|
+
* default state value to transition to if no history value exists yet.
|
|
353
|
+
*/
|
|
354
|
+
|
|
355
|
+
}, {
|
|
356
|
+
key: "target",
|
|
357
|
+
get: function get() {
|
|
358
|
+
if (this.type === 'history') {
|
|
359
|
+
var historyConfig = this.config;
|
|
360
|
+
return historyConfig.target;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* All the state node IDs of this state node and its descendant state nodes.
|
|
367
|
+
*/
|
|
368
|
+
|
|
369
|
+
}, {
|
|
370
|
+
key: "stateIds",
|
|
371
|
+
get: function get() {
|
|
372
|
+
var _this9 = this;
|
|
373
|
+
|
|
374
|
+
var childStateIds = flatten(Object.keys(this.states).map(function (stateKey) {
|
|
375
|
+
return _this9.states[stateKey].stateIds;
|
|
376
|
+
}));
|
|
377
|
+
return [this.id].concat(childStateIds);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* All the event types accepted by this state node and its descendants.
|
|
381
|
+
*/
|
|
382
|
+
|
|
383
|
+
}, {
|
|
384
|
+
key: "events",
|
|
385
|
+
get: function get() {
|
|
386
|
+
var _this10 = this;
|
|
387
|
+
|
|
388
|
+
return memo(this, 'events', function () {
|
|
389
|
+
var states = _this10.states;
|
|
390
|
+
var events = new Set(_this10.ownEvents);
|
|
391
|
+
|
|
392
|
+
if (states) {
|
|
393
|
+
for (var _i = 0, _Object$keys = Object.keys(states); _i < _Object$keys.length; _i++) {
|
|
394
|
+
var stateId = _Object$keys[_i];
|
|
395
|
+
var state = states[stateId];
|
|
396
|
+
|
|
397
|
+
if (state.states) {
|
|
398
|
+
var _iterator2 = _createForOfIteratorHelper(state.events),
|
|
399
|
+
_step2;
|
|
400
|
+
|
|
401
|
+
try {
|
|
402
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
403
|
+
var event = _step2.value;
|
|
404
|
+
events.add("".concat(event));
|
|
405
|
+
}
|
|
406
|
+
} catch (err) {
|
|
407
|
+
_iterator2.e(err);
|
|
408
|
+
} finally {
|
|
409
|
+
_iterator2.f();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return Array.from(events);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* All the events that have transitions directly from this state node.
|
|
420
|
+
*
|
|
421
|
+
* Excludes any inert events.
|
|
422
|
+
*/
|
|
423
|
+
|
|
424
|
+
}, {
|
|
425
|
+
key: "ownEvents",
|
|
426
|
+
get: function get() {
|
|
427
|
+
var events = new Set(this.transitions.filter(function (transition) {
|
|
428
|
+
return !(!transition.target && !transition.actions.length && transition.internal);
|
|
429
|
+
}).map(function (transition) {
|
|
430
|
+
return transition.eventType;
|
|
431
|
+
}));
|
|
432
|
+
return Array.from(events);
|
|
433
|
+
}
|
|
434
|
+
}]);
|
|
435
|
+
|
|
436
|
+
return StateNode;
|
|
437
|
+
}();
|
|
438
|
+
|
|
439
|
+
var STATE_IDENTIFIER = '#';
|
|
440
|
+
var WILDCARD = '*';
|
|
441
|
+
|
|
442
|
+
function createDefaultOptions() {
|
|
443
|
+
return {
|
|
444
|
+
actions: {},
|
|
445
|
+
actors: {},
|
|
446
|
+
delays: {},
|
|
447
|
+
guards: {},
|
|
448
|
+
context: {}
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function resolveContext(context, partialContext) {
|
|
453
|
+
if (isFunction(partialContext)) {
|
|
454
|
+
return _objectSpread2(_objectSpread2({}, context), partialContext);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return _objectSpread2(_objectSpread2({}, context), partialContext);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
var StateMachine = /*#__PURE__*/function () {
|
|
461
|
+
function StateMachine(
|
|
462
|
+
/**
|
|
463
|
+
* The raw config used to create the machine.
|
|
464
|
+
*/
|
|
465
|
+
config, options) {
|
|
466
|
+
var _this$config$schema;
|
|
467
|
+
|
|
468
|
+
_classCallCheck(this, StateMachine);
|
|
469
|
+
|
|
470
|
+
this.config = config;
|
|
471
|
+
|
|
472
|
+
_defineProperty(this, "_contextFactory", void 0);
|
|
473
|
+
|
|
474
|
+
_defineProperty(this, "version", void 0);
|
|
475
|
+
|
|
476
|
+
_defineProperty(this, "strict", void 0);
|
|
477
|
+
|
|
478
|
+
_defineProperty(this, "delimiter", void 0);
|
|
479
|
+
|
|
480
|
+
_defineProperty(this, "options", void 0);
|
|
481
|
+
|
|
482
|
+
_defineProperty(this, "schema", void 0);
|
|
483
|
+
|
|
484
|
+
_defineProperty(this, "__xstatenode", true);
|
|
485
|
+
|
|
486
|
+
_defineProperty(this, "idMap", new Map());
|
|
487
|
+
|
|
488
|
+
_defineProperty(this, "root", void 0);
|
|
489
|
+
|
|
490
|
+
_defineProperty(this, "key", void 0);
|
|
491
|
+
|
|
492
|
+
_defineProperty(this, "states", void 0);
|
|
493
|
+
|
|
494
|
+
_defineProperty(this, "events", void 0);
|
|
495
|
+
|
|
496
|
+
_defineProperty(this, "__TContext", void 0);
|
|
497
|
+
|
|
498
|
+
_defineProperty(this, "__TEvent", void 0);
|
|
499
|
+
|
|
500
|
+
_defineProperty(this, "__TAction", void 0);
|
|
501
|
+
|
|
502
|
+
_defineProperty(this, "__TActorMap", void 0);
|
|
503
|
+
|
|
504
|
+
_defineProperty(this, "__TResolvedTypesMeta", void 0);
|
|
505
|
+
|
|
506
|
+
this.key = config.key || config.id || '(machine)';
|
|
507
|
+
this.options = Object.assign(createDefaultOptions(), options);
|
|
508
|
+
this._contextFactory = isFunction(config.context) ? config.context : function (stuff) {
|
|
509
|
+
var partialContext = typeof (options === null || options === void 0 ? void 0 : options.context) === 'function' ? options.context(stuff) : options === null || options === void 0 ? void 0 : options.context;
|
|
510
|
+
return resolveContext(config.context, partialContext);
|
|
511
|
+
}; // TODO: fix types
|
|
512
|
+
// this.context = resolveContext(config.context, options?.context);
|
|
513
|
+
|
|
514
|
+
this.delimiter = this.config.delimiter || STATE_DELIMITER;
|
|
515
|
+
this.version = this.config.version;
|
|
516
|
+
this.schema = (_this$config$schema = this.config.schema) !== null && _this$config$schema !== void 0 ? _this$config$schema : {};
|
|
517
|
+
this.strict = !!this.config.strict;
|
|
518
|
+
this.transition = this.transition.bind(this);
|
|
519
|
+
this.root = new StateNode(config, {
|
|
520
|
+
_key: this.key,
|
|
521
|
+
_machine: this
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
this.root._initialize();
|
|
525
|
+
|
|
526
|
+
this.states = this.root.states; // TODO: remove!
|
|
527
|
+
|
|
528
|
+
this.events = this.root.events;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Clones this state machine with the provided implementations
|
|
532
|
+
* and merges the `context` (if provided).
|
|
533
|
+
*
|
|
534
|
+
* @param implementations Options (`actions`, `guards`, `actors`, `delays`, `context`)
|
|
535
|
+
* to recursively merge with the existing options.
|
|
536
|
+
*
|
|
537
|
+
* @returns A new `StateMachine` instance with the provided implementations.
|
|
538
|
+
*/
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
_createClass(StateMachine, [{
|
|
542
|
+
key: "context",
|
|
543
|
+
get: function get() {
|
|
544
|
+
return this.getContextAndActions()[0];
|
|
545
|
+
}
|
|
546
|
+
}, {
|
|
547
|
+
key: "getContextAndActions",
|
|
548
|
+
value: function getContextAndActions() {
|
|
549
|
+
var actions = []; // TODO: merge with this.options.context
|
|
550
|
+
|
|
551
|
+
var context = this._contextFactory({
|
|
552
|
+
spawn: createSpawner(this, null, null, actions) // TODO: fix types
|
|
553
|
+
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
return [context, actions];
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* The machine's own version.
|
|
560
|
+
*/
|
|
561
|
+
|
|
562
|
+
}, {
|
|
563
|
+
key: "provide",
|
|
564
|
+
value: function provide(implementations) {
|
|
565
|
+
var _this$options = this.options,
|
|
566
|
+
actions = _this$options.actions,
|
|
567
|
+
guards = _this$options.guards,
|
|
568
|
+
actors = _this$options.actors,
|
|
569
|
+
delays = _this$options.delays;
|
|
570
|
+
return new StateMachine(this.config, {
|
|
571
|
+
actions: _objectSpread2(_objectSpread2({}, actions), implementations.actions),
|
|
572
|
+
guards: _objectSpread2(_objectSpread2({}, guards), implementations.guards),
|
|
573
|
+
actors: _objectSpread2(_objectSpread2({}, actors), implementations.actors),
|
|
574
|
+
delays: _objectSpread2(_objectSpread2({}, delays), implementations.delays),
|
|
575
|
+
context: implementations.context
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Clones this state machine with custom `context`.
|
|
580
|
+
*
|
|
581
|
+
* The `context` provided can be partial `context`, which will be combined with the original `context`.
|
|
582
|
+
*
|
|
583
|
+
* @param context Custom context (will override predefined context, not recursive)
|
|
584
|
+
*/
|
|
585
|
+
|
|
586
|
+
}, {
|
|
587
|
+
key: "withContext",
|
|
588
|
+
value: function withContext(context) {
|
|
589
|
+
return this.provide({
|
|
590
|
+
context: context
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Resolves the given `state` to a new `State` instance relative to this machine.
|
|
595
|
+
*
|
|
596
|
+
* This ensures that `.nextEvents` represent the correct values.
|
|
597
|
+
*
|
|
598
|
+
* @param state The state to resolve
|
|
599
|
+
*/
|
|
600
|
+
|
|
601
|
+
}, {
|
|
602
|
+
key: "resolveState",
|
|
603
|
+
value: function resolveState(state) {
|
|
604
|
+
var configuration = Array.from(getConfiguration(getStateNodes(this.root, state.value)));
|
|
605
|
+
return this.createState(_objectSpread2(_objectSpread2({}, state), {}, {
|
|
606
|
+
value: resolveStateValue(this.root, state.value),
|
|
607
|
+
configuration: configuration
|
|
608
|
+
}));
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Determines the next state given the current `state` and received `event`.
|
|
612
|
+
* Calculates a full macrostep from all microsteps.
|
|
613
|
+
*
|
|
614
|
+
* @param state The current State instance or state value
|
|
615
|
+
* @param event The received event
|
|
616
|
+
*/
|
|
617
|
+
|
|
618
|
+
}, {
|
|
619
|
+
key: "transition",
|
|
620
|
+
value: function transition() {
|
|
621
|
+
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialState;
|
|
622
|
+
var event = arguments.length > 1 ? arguments[1] : undefined;
|
|
623
|
+
var currentState = toState(state, this);
|
|
624
|
+
return macrostep(currentState, event, this);
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Determines the next state given the current `state` and `event`.
|
|
628
|
+
* Calculates a microstep.
|
|
629
|
+
*
|
|
630
|
+
* @param state The current state
|
|
631
|
+
* @param event The received event
|
|
632
|
+
*/
|
|
633
|
+
|
|
634
|
+
}, {
|
|
635
|
+
key: "microstep",
|
|
636
|
+
value: function microstep() {
|
|
637
|
+
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.initialState;
|
|
638
|
+
var event = arguments.length > 1 ? arguments[1] : undefined;
|
|
639
|
+
var resolvedState = toState(state, this);
|
|
640
|
+
|
|
641
|
+
var _event = toSCXMLEvent(event);
|
|
642
|
+
|
|
643
|
+
if (!IS_PRODUCTION && _event.name === WILDCARD) {
|
|
644
|
+
throw new Error("An event cannot have the wildcard type ('".concat(WILDCARD, "')"));
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
if (this.strict) {
|
|
648
|
+
if (!this.root.events.includes(_event.name) && !isBuiltInEvent(_event.name)) {
|
|
649
|
+
throw new Error("Machine '".concat(this.key, "' does not accept event '").concat(_event.name, "'"));
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
var transitions = this.getTransitionData(resolvedState, _event);
|
|
654
|
+
return resolveMicroTransition(this, transitions, resolvedState, _event);
|
|
655
|
+
}
|
|
656
|
+
}, {
|
|
657
|
+
key: "getTransitionData",
|
|
658
|
+
value: function getTransitionData(state, _event) {
|
|
659
|
+
return transitionNode(this.root, state.value, state, _event) || [];
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* The initial state _before_ evaluating any microsteps.
|
|
663
|
+
* This "pre-initial" state is provided to initial actions executed in the initial state.
|
|
664
|
+
*/
|
|
665
|
+
|
|
666
|
+
}, {
|
|
667
|
+
key: "preInitialState",
|
|
668
|
+
get: function get() {
|
|
669
|
+
var _preInitial$actions;
|
|
670
|
+
|
|
671
|
+
var _this$getContextAndAc = this.getContextAndActions(),
|
|
672
|
+
_this$getContextAndAc2 = _slicedToArray(_this$getContextAndAc, 2),
|
|
673
|
+
context = _this$getContextAndAc2[0],
|
|
674
|
+
actions = _this$getContextAndAc2[1];
|
|
675
|
+
|
|
676
|
+
var preInitial = this.resolveState(State.from(getStateValue(this.root, getConfiguration([this.root])), context));
|
|
677
|
+
preInitial._initial = true;
|
|
678
|
+
|
|
679
|
+
(_preInitial$actions = preInitial.actions).unshift.apply(_preInitial$actions, _toConsumableArray(actions));
|
|
680
|
+
|
|
681
|
+
return preInitial;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* The initial State instance, which includes all actions to be executed from
|
|
685
|
+
* entering the initial state.
|
|
686
|
+
*/
|
|
687
|
+
|
|
688
|
+
}, {
|
|
689
|
+
key: "initialState",
|
|
690
|
+
get: function get() {
|
|
691
|
+
return this.getInitialState();
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Returns the initial `State` instance, with reference to `self` as an `ActorRef`.
|
|
695
|
+
*/
|
|
696
|
+
|
|
697
|
+
}, {
|
|
698
|
+
key: "getInitialState",
|
|
699
|
+
value: function getInitialState() {
|
|
700
|
+
var _nextState$actions;
|
|
701
|
+
|
|
702
|
+
var preInitialState = this.preInitialState;
|
|
703
|
+
var nextState = resolveMicroTransition(this, [], preInitialState, undefined);
|
|
704
|
+
|
|
705
|
+
(_nextState$actions = nextState.actions).unshift.apply(_nextState$actions, _toConsumableArray(preInitialState.actions));
|
|
706
|
+
|
|
707
|
+
var macroState = macrostep(nextState, null, this);
|
|
708
|
+
macroState.changed = undefined;
|
|
709
|
+
return macroState;
|
|
710
|
+
}
|
|
711
|
+
}, {
|
|
712
|
+
key: "getStateNodeById",
|
|
713
|
+
value: function getStateNodeById(stateId) {
|
|
714
|
+
var resolvedStateId = isStateId(stateId) ? stateId.slice(STATE_IDENTIFIER.length) : stateId;
|
|
715
|
+
var stateNode = this.idMap.get(resolvedStateId);
|
|
716
|
+
|
|
717
|
+
if (!stateNode) {
|
|
718
|
+
throw new Error("Child state node '#".concat(resolvedStateId, "' does not exist on machine '").concat(this.key, "'"));
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
return stateNode;
|
|
722
|
+
}
|
|
723
|
+
}, {
|
|
724
|
+
key: "definition",
|
|
725
|
+
get: function get() {
|
|
726
|
+
return this.root.definition;
|
|
727
|
+
}
|
|
728
|
+
}, {
|
|
729
|
+
key: "toJSON",
|
|
730
|
+
value: function toJSON() {
|
|
731
|
+
return this.definition;
|
|
732
|
+
}
|
|
733
|
+
}, {
|
|
734
|
+
key: "createState",
|
|
735
|
+
value: function createState(stateConfig) {
|
|
736
|
+
var state = stateConfig instanceof State ? stateConfig : new State(stateConfig);
|
|
737
|
+
state.machine = this;
|
|
738
|
+
return state;
|
|
739
|
+
}
|
|
740
|
+
/**@deprecated an internal property acting as a "phantom" type, not meant to be used at runtime */
|
|
741
|
+
|
|
742
|
+
}]);
|
|
743
|
+
|
|
744
|
+
return StateMachine;
|
|
745
|
+
}();
|
|
746
|
+
|
|
747
|
+
function createMachine(config, implementations) {
|
|
748
|
+
return new StateMachine(config, implementations);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
export { StateNode as S, StateMachine as a, createMachine as c };
|