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.
Files changed (206) hide show
  1. package/CHANGELOG.md +526 -0
  2. package/actions/ExecutableAction.ts +58 -0
  3. package/actions/dist/xstate-actions.cjs.d.ts +1 -0
  4. package/actions/dist/xstate-actions.cjs.dev.js +32 -0
  5. package/actions/dist/xstate-actions.cjs.js +7 -0
  6. package/actions/dist/xstate-actions.cjs.prod.js +32 -0
  7. package/actions/dist/xstate-actions.esm.js +2 -0
  8. package/actions/dynamicAction.ts +34 -0
  9. package/actions/package.json +4 -0
  10. package/actors/dist/xstate-actors.cjs.d.ts +1 -0
  11. package/actors/dist/xstate-actors.cjs.dev.js +21 -0
  12. package/actors/dist/xstate-actors.cjs.js +7 -0
  13. package/actors/dist/xstate-actors.cjs.prod.js +21 -0
  14. package/actors/dist/xstate-actors.esm.js +2 -0
  15. package/actors/package.json +4 -0
  16. package/dev/dist/xstate-dev.cjs.d.ts +1 -0
  17. package/dev/dist/xstate-dev.cjs.dev.js +11 -0
  18. package/dev/dist/xstate-dev.cjs.js +7 -0
  19. package/{lib/devTools.js → dev/dist/xstate-dev.cjs.prod.js} +17 -10
  20. package/dev/dist/xstate-dev.esm.js +1 -0
  21. package/dev/package.json +4 -0
  22. package/dist/Machine-9ce74c11.cjs.dev.js +755 -0
  23. package/dist/Machine-dca041dc.cjs.prod.js +749 -0
  24. package/dist/Machine-fb2f47f5.esm.js +751 -0
  25. package/dist/actions-6b86876d.cjs.dev.js +4815 -0
  26. package/dist/actions-70094c93.cjs.prod.js +4765 -0
  27. package/dist/actions-ff512f16.esm.js +4757 -0
  28. package/dist/declarations/actions/ExecutableAction.d.ts +23 -0
  29. package/dist/declarations/actions/dynamicAction.d.ts +3 -0
  30. package/dist/declarations/src/Machine.d.ts +4 -0
  31. package/dist/declarations/src/Mailbox.d.ts +12 -0
  32. package/dist/declarations/src/ObservableActorRef.d.ts +25 -0
  33. package/{lib → dist/declarations/src}/SimulatedClock.d.ts +0 -1
  34. package/dist/declarations/src/State.d.ts +104 -0
  35. package/dist/declarations/src/StateMachine.d.ts +112 -0
  36. package/dist/declarations/src/StateNode.d.ts +127 -0
  37. package/{es → dist/declarations/src}/actionTypes.d.ts +0 -3
  38. package/dist/declarations/src/actions/assign.d.ts +7 -0
  39. package/dist/declarations/src/actions/cancel.d.ts +10 -0
  40. package/dist/declarations/src/actions/choose.d.ts +3 -0
  41. package/dist/declarations/src/actions/invoke.d.ts +3 -0
  42. package/dist/declarations/src/actions/log.d.ts +11 -0
  43. package/dist/declarations/src/actions/pure.d.ts +3 -0
  44. package/dist/declarations/src/actions/raise.d.ts +9 -0
  45. package/dist/declarations/src/actions/send.d.ts +55 -0
  46. package/dist/declarations/src/actions/stop.d.ts +8 -0
  47. package/dist/declarations/src/actions.d.ts +44 -0
  48. package/dist/declarations/src/actors.d.ts +38 -0
  49. package/dist/declarations/src/constants.d.ts +5 -0
  50. package/{lib/devTools.d.ts → dist/declarations/src/dev/index.d.ts} +2 -2
  51. package/{es → dist/declarations/src}/environment.d.ts +0 -1
  52. package/dist/declarations/src/guards.d.ts +9 -0
  53. package/dist/declarations/src/index.d.ts +47 -0
  54. package/dist/declarations/src/interpreter.d.ts +147 -0
  55. package/{es → dist/declarations/src}/mapState.d.ts +0 -1
  56. package/dist/declarations/src/match.d.ts +5 -0
  57. package/dist/declarations/src/memo.d.ts +1 -0
  58. package/dist/declarations/src/model.d.ts +5 -0
  59. package/{lib → dist/declarations/src}/model.types.d.ts +10 -9
  60. package/dist/declarations/src/registry.d.ts +8 -0
  61. package/{es → dist/declarations/src}/schema.d.ts +0 -1
  62. package/dist/declarations/src/spawn.d.ts +2 -0
  63. package/dist/declarations/src/stateUtils.d.ts +85 -0
  64. package/{lib → dist/declarations/src}/typegenTypes.d.ts +28 -26
  65. package/dist/declarations/src/types.d.ts +1026 -0
  66. package/dist/declarations/src/utils.d.ts +56 -0
  67. package/{es/devTools.js → dist/index-20b364c4.esm.js} +18 -6
  68. package/dist/index-2a564d03.cjs.dev.js +64 -0
  69. package/dist/xstate.cjs.d.ts +1 -0
  70. package/dist/xstate.cjs.dev.js +217 -0
  71. package/dist/xstate.cjs.js +7 -0
  72. package/dist/xstate.cjs.prod.js +217 -0
  73. package/dist/xstate.esm.js +172 -0
  74. package/guards/dist/xstate-guards.cjs.d.ts +1 -0
  75. package/guards/dist/xstate-guards.cjs.dev.js +15 -0
  76. package/guards/dist/xstate-guards.cjs.js +7 -0
  77. package/guards/dist/xstate-guards.cjs.prod.js +15 -0
  78. package/guards/dist/xstate-guards.esm.js +2 -0
  79. package/guards/package.json +4 -0
  80. package/invoke/package.json +4 -0
  81. package/model/dist/xstate-model.cjs.d.ts +1 -0
  82. package/model/dist/xstate-model.cjs.dev.js +41 -0
  83. package/model/dist/xstate-model.cjs.js +7 -0
  84. package/model/dist/xstate-model.cjs.prod.js +41 -0
  85. package/model/dist/xstate-model.esm.js +37 -0
  86. package/model/package.json +4 -0
  87. package/package.json +21 -30
  88. package/dist/xstate.interpreter.js +0 -15
  89. package/dist/xstate.js +0 -15
  90. package/dist/xstate.web.js +0 -15
  91. package/es/Actor.d.ts +0 -25
  92. package/es/Actor.js +0 -99
  93. package/es/Machine.d.ts +0 -12
  94. package/es/Machine.js +0 -14
  95. package/es/SimulatedClock.d.ts +0 -17
  96. package/es/SimulatedClock.js +0 -81
  97. package/es/State.d.ts +0 -123
  98. package/es/State.js +0 -274
  99. package/es/StateNode.d.ts +0 -287
  100. package/es/StateNode.js +0 -1566
  101. package/es/_virtual/_tslib.js +0 -78
  102. package/es/actionTypes.js +0 -22
  103. package/es/actions.d.ts +0 -154
  104. package/es/actions.js +0 -520
  105. package/es/behaviors.d.ts +0 -37
  106. package/es/behaviors.js +0 -131
  107. package/es/constants.d.ts +0 -6
  108. package/es/constants.js +0 -6
  109. package/es/devTools.d.ts +0 -14
  110. package/es/each.d.ts +0 -4
  111. package/es/each.js +0 -13
  112. package/es/environment.js +0 -3
  113. package/es/index.d.ts +0 -22
  114. package/es/index.js +0 -23
  115. package/es/interpreter.d.ts +0 -209
  116. package/es/interpreter.js +0 -1402
  117. package/es/invoke.d.ts +0 -11
  118. package/es/invokeUtils.d.ts +0 -7
  119. package/es/invokeUtils.js +0 -40
  120. package/es/json.d.ts +0 -31
  121. package/es/json.js +0 -86
  122. package/es/mapState.js +0 -32
  123. package/es/match.d.ts +0 -9
  124. package/es/match.js +0 -34
  125. package/es/model.d.ts +0 -5
  126. package/es/model.js +0 -50
  127. package/es/model.types.d.ts +0 -52
  128. package/es/patterns.d.ts +0 -14
  129. package/es/patterns.js +0 -48
  130. package/es/registry.d.ts +0 -9
  131. package/es/registry.js +0 -19
  132. package/es/scheduler.d.ts +0 -17
  133. package/es/scheduler.js +0 -79
  134. package/es/schema.js +0 -6
  135. package/es/scxml.d.ts +0 -6
  136. package/es/serviceScope.d.ts +0 -4
  137. package/es/serviceScope.js +0 -16
  138. package/es/stateUtils.d.ts +0 -17
  139. package/es/stateUtils.js +0 -259
  140. package/es/typegenTypes.d.ts +0 -130
  141. package/es/types.d.ts +0 -1037
  142. package/es/types.js +0 -33
  143. package/es/utils.d.ts +0 -72
  144. package/es/utils.js +0 -626
  145. package/es/waitFor.d.ts +0 -32
  146. package/es/waitFor.js +0 -66
  147. package/lib/Actor.d.ts +0 -25
  148. package/lib/Actor.js +0 -108
  149. package/lib/Machine.d.ts +0 -12
  150. package/lib/Machine.js +0 -19
  151. package/lib/SimulatedClock.js +0 -85
  152. package/lib/State.d.ts +0 -123
  153. package/lib/State.js +0 -282
  154. package/lib/StateNode.d.ts +0 -287
  155. package/lib/StateNode.js +0 -1570
  156. package/lib/_virtual/_tslib.js +0 -85
  157. package/lib/actionTypes.d.ts +0 -20
  158. package/lib/actionTypes.js +0 -43
  159. package/lib/actions.d.ts +0 -154
  160. package/lib/actions.js +0 -552
  161. package/lib/behaviors.d.ts +0 -37
  162. package/lib/behaviors.js +0 -137
  163. package/lib/constants.d.ts +0 -6
  164. package/lib/constants.js +0 -13
  165. package/lib/each.d.ts +0 -4
  166. package/lib/each.js +0 -17
  167. package/lib/environment.d.ts +0 -2
  168. package/lib/environment.js +0 -7
  169. package/lib/index.d.ts +0 -22
  170. package/lib/index.js +0 -60
  171. package/lib/interpreter.d.ts +0 -209
  172. package/lib/interpreter.js +0 -1408
  173. package/lib/invoke.d.ts +0 -11
  174. package/lib/invoke.js +0 -20
  175. package/lib/invokeUtils.d.ts +0 -7
  176. package/lib/invokeUtils.js +0 -45
  177. package/lib/json.d.ts +0 -31
  178. package/lib/json.js +0 -94
  179. package/lib/mapState.d.ts +0 -4
  180. package/lib/mapState.js +0 -36
  181. package/lib/match.d.ts +0 -9
  182. package/lib/match.js +0 -38
  183. package/lib/model.d.ts +0 -5
  184. package/lib/model.js +0 -54
  185. package/lib/model.types.js +0 -2
  186. package/lib/patterns.d.ts +0 -14
  187. package/lib/patterns.js +0 -53
  188. package/lib/registry.d.ts +0 -9
  189. package/lib/registry.js +0 -23
  190. package/lib/scheduler.d.ts +0 -17
  191. package/lib/scheduler.js +0 -83
  192. package/lib/schema.d.ts +0 -3
  193. package/lib/schema.js +0 -11
  194. package/lib/scxml.d.ts +0 -6
  195. package/lib/scxml.js +0 -350
  196. package/lib/serviceScope.d.ts +0 -4
  197. package/lib/serviceScope.js +0 -21
  198. package/lib/stateUtils.d.ts +0 -17
  199. package/lib/stateUtils.js +0 -273
  200. package/lib/typegenTypes.js +0 -2
  201. package/lib/types.d.ts +0 -1037
  202. package/lib/types.js +0 -35
  203. package/lib/utils.d.ts +0 -72
  204. package/lib/utils.js +0 -673
  205. package/lib/waitFor.d.ts +0 -32
  206. package/lib/waitFor.js +0 -70
package/es/StateNode.js DELETED
@@ -1,1566 +0,0 @@
1
- import { __assign, __spreadArray, __read, __values, __rest } from './_virtual/_tslib.js';
2
- import { isFunction, mapValues, isArray, flatten, toArray, toStateValue, isString, getEventType, toSCXMLEvent, matchesState, path, evaluateGuard, mapContext, pathToStateValue, isBuiltInEvent, partition, updateHistoryValue, toStatePath, mapFilterValues, warn, toStatePaths, nestedPath, normalizeTarget, toGuard, toTransitionConfigArray, isMachine, createInvokeId } from './utils.js';
3
- import { SpecialTargets } from './types.js';
4
- import { State, stateValuesEqual } from './State.js';
5
- import { start as start$1, stop as stop$1, raise as raise$1, send, invoke, update, nullEvent } from './actionTypes.js';
6
- import { done, start, raise, stop, toActionObjects, resolveActions, doneInvoke, error, toActionObject, toActivityDefinition, after, send as send$1, cancel, initEvent } from './actions.js';
7
- import { IS_PRODUCTION } from './environment.js';
8
- import { STATE_DELIMITER } from './constants.js';
9
- import { getAllStateNodes, getConfiguration, isInFinalState, getTagsFromConfiguration, has, getChildren, getValue, isLeafNode } from './stateUtils.js';
10
- import { createInvocableActor } from './Actor.js';
11
- import { toInvokeDefinition } from './invokeUtils.js';
12
-
13
- var NULL_EVENT = '';
14
- var STATE_IDENTIFIER = '#';
15
- var WILDCARD = '*';
16
- var EMPTY_OBJECT = {};
17
-
18
- var isStateId = function (str) {
19
- return str[0] === STATE_IDENTIFIER;
20
- };
21
-
22
- var createDefaultOptions = function () {
23
- return {
24
- actions: {},
25
- guards: {},
26
- services: {},
27
- activities: {},
28
- delays: {}
29
- };
30
- };
31
-
32
- var validateArrayifiedTransitions = function (stateNode, event, transitions) {
33
- var hasNonLastUnguardedTarget = transitions.slice(0, -1).some(function (transition) {
34
- return !('cond' in transition) && !('in' in transition) && (isString(transition.target) || isMachine(transition.target));
35
- });
36
- var eventText = event === NULL_EVENT ? 'the transient event' : "event '".concat(event, "'");
37
- warn(!hasNonLastUnguardedTarget, "One or more transitions for ".concat(eventText, " on state '").concat(stateNode.id, "' are unreachable. ") + "Make sure that the default transition is the last one defined.");
38
- };
39
-
40
- var StateNode =
41
- /*#__PURE__*/
42
-
43
- /** @class */
44
- function () {
45
- function StateNode(
46
- /**
47
- * The raw config used to create the machine.
48
- */
49
- config, options,
50
- /**
51
- * The initial extended state
52
- */
53
- _context, // TODO: this is unsafe, but we're removing it in v5 anyway
54
- _stateInfo) {
55
- var _this = this;
56
-
57
- if (_context === void 0) {
58
- _context = 'context' in config ? config.context : undefined;
59
- }
60
-
61
- var _a;
62
-
63
- this.config = config;
64
- this._context = _context;
65
- /**
66
- * The order this state node appears. Corresponds to the implicit SCXML document order.
67
- */
68
-
69
- this.order = -1;
70
- this.__xstatenode = true;
71
- this.__cache = {
72
- events: undefined,
73
- relativeValue: new Map(),
74
- initialStateValue: undefined,
75
- initialState: undefined,
76
- on: undefined,
77
- transitions: undefined,
78
- candidates: {},
79
- delayedTransitions: undefined
80
- };
81
- this.idMap = {};
82
- this.tags = [];
83
- this.options = Object.assign(createDefaultOptions(), options);
84
- this.parent = _stateInfo === null || _stateInfo === void 0 ? void 0 : _stateInfo.parent;
85
- this.key = this.config.key || (_stateInfo === null || _stateInfo === void 0 ? void 0 : _stateInfo.key) || this.config.id || '(machine)';
86
- this.machine = this.parent ? this.parent.machine : this;
87
- this.path = this.parent ? this.parent.path.concat(this.key) : [];
88
- this.delimiter = this.config.delimiter || (this.parent ? this.parent.delimiter : STATE_DELIMITER);
89
- this.id = this.config.id || __spreadArray([this.machine.key], __read(this.path), false).join(this.delimiter);
90
- this.version = this.parent ? this.parent.version : this.config.version;
91
- this.type = this.config.type || (this.config.parallel ? 'parallel' : this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
92
- this.schema = this.parent ? this.machine.schema : (_a = this.config.schema) !== null && _a !== void 0 ? _a : {};
93
- this.description = this.config.description;
94
-
95
- if (!IS_PRODUCTION) {
96
- warn(!('parallel' in this.config), "The \"parallel\" property is deprecated and will be removed in version 4.1. ".concat(this.config.parallel ? "Replace with `type: 'parallel'`" : "Use `type: '".concat(this.type, "'`"), " in the config for state node '").concat(this.id, "' instead."));
97
- }
98
-
99
- this.initial = this.config.initial;
100
- this.states = this.config.states ? mapValues(this.config.states, function (stateConfig, key) {
101
- var _a;
102
-
103
- var stateNode = new StateNode(stateConfig, {}, undefined, {
104
- parent: _this,
105
- key: key
106
- });
107
- Object.assign(_this.idMap, __assign((_a = {}, _a[stateNode.id] = stateNode, _a), stateNode.idMap));
108
- return stateNode;
109
- }) : EMPTY_OBJECT; // Document order
110
-
111
- var order = 0;
112
-
113
- function dfs(stateNode) {
114
- var e_1, _a;
115
-
116
- stateNode.order = order++;
117
-
118
- try {
119
- for (var _b = __values(getChildren(stateNode)), _c = _b.next(); !_c.done; _c = _b.next()) {
120
- var child = _c.value;
121
- dfs(child);
122
- }
123
- } catch (e_1_1) {
124
- e_1 = {
125
- error: e_1_1
126
- };
127
- } finally {
128
- try {
129
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
130
- } finally {
131
- if (e_1) throw e_1.error;
132
- }
133
- }
134
- }
135
-
136
- dfs(this); // History config
137
-
138
- this.history = this.config.history === true ? 'shallow' : this.config.history || false;
139
- this._transient = !!this.config.always || (!this.config.on ? false : Array.isArray(this.config.on) ? this.config.on.some(function (_a) {
140
- var event = _a.event;
141
- return event === NULL_EVENT;
142
- }) : NULL_EVENT in this.config.on);
143
- this.strict = !!this.config.strict; // TODO: deprecate (entry)
144
-
145
- this.onEntry = toArray(this.config.entry || this.config.onEntry).map(function (action) {
146
- return toActionObject(action);
147
- }); // TODO: deprecate (exit)
148
-
149
- this.onExit = toArray(this.config.exit || this.config.onExit).map(function (action) {
150
- return toActionObject(action);
151
- });
152
- this.meta = this.config.meta;
153
- this.doneData = this.type === 'final' ? this.config.data : undefined;
154
- this.invoke = toArray(this.config.invoke).map(function (invokeConfig, i) {
155
- var _a, _b;
156
-
157
- if (isMachine(invokeConfig)) {
158
- var invokeId = createInvokeId(_this.id, i);
159
- _this.machine.options.services = __assign((_a = {}, _a[invokeId] = invokeConfig, _a), _this.machine.options.services);
160
- return toInvokeDefinition({
161
- src: invokeId,
162
- id: invokeId
163
- });
164
- } else if (isString(invokeConfig.src)) {
165
- var invokeId = invokeConfig.id || createInvokeId(_this.id, i);
166
- return toInvokeDefinition(__assign(__assign({}, invokeConfig), {
167
- id: invokeId,
168
- src: invokeConfig.src
169
- }));
170
- } else if (isMachine(invokeConfig.src) || isFunction(invokeConfig.src)) {
171
- var invokeId = invokeConfig.id || createInvokeId(_this.id, i);
172
- _this.machine.options.services = __assign((_b = {}, _b[invokeId] = invokeConfig.src, _b), _this.machine.options.services);
173
- return toInvokeDefinition(__assign(__assign({
174
- id: invokeId
175
- }, invokeConfig), {
176
- src: invokeId
177
- }));
178
- } else {
179
- var invokeSource = invokeConfig.src;
180
- return toInvokeDefinition(__assign(__assign({
181
- id: createInvokeId(_this.id, i)
182
- }, invokeConfig), {
183
- src: invokeSource
184
- }));
185
- }
186
- });
187
- this.activities = toArray(this.config.activities).concat(this.invoke).map(function (activity) {
188
- return toActivityDefinition(activity);
189
- });
190
- this.transition = this.transition.bind(this);
191
- this.tags = toArray(this.config.tags); // TODO: this is the real fix for initialization once
192
- // state node getters are deprecated
193
- // if (!this.parent) {
194
- // this._init();
195
- // }
196
- }
197
-
198
- StateNode.prototype._init = function () {
199
- if (this.__cache.transitions) {
200
- return;
201
- }
202
-
203
- getAllStateNodes(this).forEach(function (stateNode) {
204
- return stateNode.on;
205
- });
206
- };
207
- /**
208
- * Clones this state machine with custom options and context.
209
- *
210
- * @param options Options (actions, guards, activities, services) to recursively merge with the existing options.
211
- * @param context Custom context (will override predefined context)
212
- */
213
-
214
-
215
- StateNode.prototype.withConfig = function (options, context) {
216
- var _a = this.options,
217
- actions = _a.actions,
218
- activities = _a.activities,
219
- guards = _a.guards,
220
- services = _a.services,
221
- delays = _a.delays;
222
- return new StateNode(this.config, {
223
- actions: __assign(__assign({}, actions), options.actions),
224
- activities: __assign(__assign({}, activities), options.activities),
225
- guards: __assign(__assign({}, guards), options.guards),
226
- services: __assign(__assign({}, services), options.services),
227
- delays: __assign(__assign({}, delays), options.delays)
228
- }, context !== null && context !== void 0 ? context : this.context);
229
- };
230
- /**
231
- * Clones this state machine with custom context.
232
- *
233
- * @param context Custom context (will override predefined context, not recursive)
234
- */
235
-
236
-
237
- StateNode.prototype.withContext = function (context) {
238
- return new StateNode(this.config, this.options, context);
239
- };
240
-
241
- Object.defineProperty(StateNode.prototype, "context", {
242
- get: function () {
243
- return isFunction(this._context) ? this._context() : this._context;
244
- },
245
- enumerable: false,
246
- configurable: true
247
- });
248
- Object.defineProperty(StateNode.prototype, "definition", {
249
- /**
250
- * The well-structured state node definition.
251
- */
252
- get: function () {
253
- return {
254
- id: this.id,
255
- key: this.key,
256
- version: this.version,
257
- context: this.context,
258
- type: this.type,
259
- initial: this.initial,
260
- history: this.history,
261
- states: mapValues(this.states, function (state) {
262
- return state.definition;
263
- }),
264
- on: this.on,
265
- transitions: this.transitions,
266
- entry: this.onEntry,
267
- exit: this.onExit,
268
- activities: this.activities || [],
269
- meta: this.meta,
270
- order: this.order || -1,
271
- data: this.doneData,
272
- invoke: this.invoke,
273
- description: this.description,
274
- tags: this.tags
275
- };
276
- },
277
- enumerable: false,
278
- configurable: true
279
- });
280
-
281
- StateNode.prototype.toJSON = function () {
282
- return this.definition;
283
- };
284
-
285
- Object.defineProperty(StateNode.prototype, "on", {
286
- /**
287
- * The mapping of events to transitions.
288
- */
289
- get: function () {
290
- if (this.__cache.on) {
291
- return this.__cache.on;
292
- }
293
-
294
- var transitions = this.transitions;
295
- return this.__cache.on = transitions.reduce(function (map, transition) {
296
- map[transition.eventType] = map[transition.eventType] || [];
297
- map[transition.eventType].push(transition);
298
- return map;
299
- }, {});
300
- },
301
- enumerable: false,
302
- configurable: true
303
- });
304
- Object.defineProperty(StateNode.prototype, "after", {
305
- get: function () {
306
- return this.__cache.delayedTransitions || (this.__cache.delayedTransitions = this.getDelayedTransitions(), this.__cache.delayedTransitions);
307
- },
308
- enumerable: false,
309
- configurable: true
310
- });
311
- Object.defineProperty(StateNode.prototype, "transitions", {
312
- /**
313
- * All the transitions that can be taken from this state node.
314
- */
315
- get: function () {
316
- return this.__cache.transitions || (this.__cache.transitions = this.formatTransitions(), this.__cache.transitions);
317
- },
318
- enumerable: false,
319
- configurable: true
320
- });
321
-
322
- StateNode.prototype.getCandidates = function (eventName) {
323
- if (this.__cache.candidates[eventName]) {
324
- return this.__cache.candidates[eventName];
325
- }
326
-
327
- var transient = eventName === NULL_EVENT;
328
- var candidates = this.transitions.filter(function (transition) {
329
- var sameEventType = transition.eventType === eventName; // null events should only match against eventless transitions
330
-
331
- return transient ? sameEventType : sameEventType || transition.eventType === WILDCARD;
332
- });
333
- this.__cache.candidates[eventName] = candidates;
334
- return candidates;
335
- };
336
- /**
337
- * All delayed transitions from the config.
338
- */
339
-
340
-
341
- StateNode.prototype.getDelayedTransitions = function () {
342
- var _this = this;
343
-
344
- var afterConfig = this.config.after;
345
-
346
- if (!afterConfig) {
347
- return [];
348
- }
349
-
350
- var mutateEntryExit = function (delay, i) {
351
- var delayRef = isFunction(delay) ? "".concat(_this.id, ":delay[").concat(i, "]") : delay;
352
- var eventType = after(delayRef, _this.id);
353
-
354
- _this.onEntry.push(send$1(eventType, {
355
- delay: delay
356
- }));
357
-
358
- _this.onExit.push(cancel(eventType));
359
-
360
- return eventType;
361
- };
362
-
363
- var delayedTransitions = isArray(afterConfig) ? afterConfig.map(function (transition, i) {
364
- var eventType = mutateEntryExit(transition.delay, i);
365
- return __assign(__assign({}, transition), {
366
- event: eventType
367
- });
368
- }) : flatten(Object.keys(afterConfig).map(function (delay, i) {
369
- var configTransition = afterConfig[delay];
370
- var resolvedTransition = isString(configTransition) ? {
371
- target: configTransition
372
- } : configTransition;
373
- var resolvedDelay = !isNaN(+delay) ? +delay : delay;
374
- var eventType = mutateEntryExit(resolvedDelay, i);
375
- return toArray(resolvedTransition).map(function (transition) {
376
- return __assign(__assign({}, transition), {
377
- event: eventType,
378
- delay: resolvedDelay
379
- });
380
- });
381
- }));
382
- return delayedTransitions.map(function (delayedTransition) {
383
- var delay = delayedTransition.delay;
384
- return __assign(__assign({}, _this.formatTransition(delayedTransition)), {
385
- delay: delay
386
- });
387
- });
388
- };
389
- /**
390
- * Returns the state nodes represented by the current state value.
391
- *
392
- * @param state The state value or State instance
393
- */
394
-
395
-
396
- StateNode.prototype.getStateNodes = function (state) {
397
- var _a;
398
-
399
- var _this = this;
400
-
401
- if (!state) {
402
- return [];
403
- }
404
-
405
- var stateValue = state instanceof State ? state.value : toStateValue(state, this.delimiter);
406
-
407
- if (isString(stateValue)) {
408
- var initialStateValue = this.getStateNode(stateValue).initial;
409
- return initialStateValue !== undefined ? this.getStateNodes((_a = {}, _a[stateValue] = initialStateValue, _a)) : [this, this.states[stateValue]];
410
- }
411
-
412
- var subStateKeys = Object.keys(stateValue);
413
- var subStateNodes = [this];
414
- subStateNodes.push.apply(subStateNodes, __spreadArray([], __read(flatten(subStateKeys.map(function (subStateKey) {
415
- return _this.getStateNode(subStateKey).getStateNodes(stateValue[subStateKey]);
416
- }))), false));
417
- return subStateNodes;
418
- };
419
- /**
420
- * Returns `true` if this state node explicitly handles the given event.
421
- *
422
- * @param event The event in question
423
- */
424
-
425
-
426
- StateNode.prototype.handles = function (event) {
427
- var eventType = getEventType(event);
428
- return this.events.includes(eventType);
429
- };
430
- /**
431
- * Resolves the given `state` to a new `State` instance relative to this machine.
432
- *
433
- * This ensures that `.events` and `.nextEvents` represent the correct values.
434
- *
435
- * @param state The state to resolve
436
- */
437
-
438
-
439
- StateNode.prototype.resolveState = function (state) {
440
- var stateFromConfig = state instanceof State ? state : State.create(state);
441
- var configuration = Array.from(getConfiguration([], this.getStateNodes(stateFromConfig.value)));
442
- return new State(__assign(__assign({}, stateFromConfig), {
443
- value: this.resolve(stateFromConfig.value),
444
- configuration: configuration,
445
- done: isInFinalState(configuration, this),
446
- tags: getTagsFromConfiguration(configuration),
447
- machine: this.machine
448
- }));
449
- };
450
-
451
- StateNode.prototype.transitionLeafNode = function (stateValue, state, _event) {
452
- var stateNode = this.getStateNode(stateValue);
453
- var next = stateNode.next(state, _event);
454
-
455
- if (!next || !next.transitions.length) {
456
- return this.next(state, _event);
457
- }
458
-
459
- return next;
460
- };
461
-
462
- StateNode.prototype.transitionCompoundNode = function (stateValue, state, _event) {
463
- var subStateKeys = Object.keys(stateValue);
464
- var stateNode = this.getStateNode(subStateKeys[0]);
465
-
466
- var next = stateNode._transition(stateValue[subStateKeys[0]], state, _event);
467
-
468
- if (!next || !next.transitions.length) {
469
- return this.next(state, _event);
470
- }
471
-
472
- return next;
473
- };
474
-
475
- StateNode.prototype.transitionParallelNode = function (stateValue, state, _event) {
476
- var e_2, _a;
477
-
478
- var transitionMap = {};
479
-
480
- try {
481
- for (var _b = __values(Object.keys(stateValue)), _c = _b.next(); !_c.done; _c = _b.next()) {
482
- var subStateKey = _c.value;
483
- var subStateValue = stateValue[subStateKey];
484
-
485
- if (!subStateValue) {
486
- continue;
487
- }
488
-
489
- var subStateNode = this.getStateNode(subStateKey);
490
-
491
- var next = subStateNode._transition(subStateValue, state, _event);
492
-
493
- if (next) {
494
- transitionMap[subStateKey] = next;
495
- }
496
- }
497
- } catch (e_2_1) {
498
- e_2 = {
499
- error: e_2_1
500
- };
501
- } finally {
502
- try {
503
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
504
- } finally {
505
- if (e_2) throw e_2.error;
506
- }
507
- }
508
-
509
- var stateTransitions = Object.keys(transitionMap).map(function (key) {
510
- return transitionMap[key];
511
- });
512
- var enabledTransitions = flatten(stateTransitions.map(function (st) {
513
- return st.transitions;
514
- }));
515
- var willTransition = stateTransitions.some(function (st) {
516
- return st.transitions.length > 0;
517
- });
518
-
519
- if (!willTransition) {
520
- return this.next(state, _event);
521
- }
522
-
523
- var entryNodes = flatten(stateTransitions.map(function (t) {
524
- return t.entrySet;
525
- }));
526
- var configuration = flatten(Object.keys(transitionMap).map(function (key) {
527
- return transitionMap[key].configuration;
528
- }));
529
- return {
530
- transitions: enabledTransitions,
531
- entrySet: entryNodes,
532
- exitSet: flatten(stateTransitions.map(function (t) {
533
- return t.exitSet;
534
- })),
535
- configuration: configuration,
536
- source: state,
537
- actions: flatten(Object.keys(transitionMap).map(function (key) {
538
- return transitionMap[key].actions;
539
- }))
540
- };
541
- };
542
-
543
- StateNode.prototype._transition = function (stateValue, state, _event) {
544
- // leaf node
545
- if (isString(stateValue)) {
546
- return this.transitionLeafNode(stateValue, state, _event);
547
- } // hierarchical node
548
-
549
-
550
- if (Object.keys(stateValue).length === 1) {
551
- return this.transitionCompoundNode(stateValue, state, _event);
552
- } // orthogonal node
553
-
554
-
555
- return this.transitionParallelNode(stateValue, state, _event);
556
- };
557
-
558
- StateNode.prototype.getTransitionData = function (state, event) {
559
- return this._transition(state.value, state, toSCXMLEvent(event));
560
- };
561
-
562
- StateNode.prototype.next = function (state, _event) {
563
- var e_3, _a;
564
-
565
- var _this = this;
566
-
567
- var eventName = _event.name;
568
- var actions = [];
569
- var nextStateNodes = [];
570
- var selectedTransition;
571
-
572
- try {
573
- for (var _b = __values(this.getCandidates(eventName)), _c = _b.next(); !_c.done; _c = _b.next()) {
574
- var candidate = _c.value;
575
- var cond = candidate.cond,
576
- stateIn = candidate.in;
577
- var resolvedContext = state.context;
578
- var isInState = stateIn ? isString(stateIn) && isStateId(stateIn) ? // Check if in state by ID
579
- state.matches(toStateValue(this.getStateNodeById(stateIn).path, this.delimiter)) : // Check if in state by relative grandparent
580
- matchesState(toStateValue(stateIn, this.delimiter), path(this.path.slice(0, -2))(state.value)) : true;
581
- var guardPassed = false;
582
-
583
- try {
584
- guardPassed = !cond || evaluateGuard(this.machine, cond, resolvedContext, _event, state);
585
- } catch (err) {
586
- throw new Error("Unable to evaluate guard '".concat(cond.name || cond.type, "' in transition for event '").concat(eventName, "' in state node '").concat(this.id, "':\n").concat(err.message));
587
- }
588
-
589
- if (guardPassed && isInState) {
590
- if (candidate.target !== undefined) {
591
- nextStateNodes = candidate.target;
592
- }
593
-
594
- actions.push.apply(actions, __spreadArray([], __read(candidate.actions), false));
595
- selectedTransition = candidate;
596
- break;
597
- }
598
- }
599
- } catch (e_3_1) {
600
- e_3 = {
601
- error: e_3_1
602
- };
603
- } finally {
604
- try {
605
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
606
- } finally {
607
- if (e_3) throw e_3.error;
608
- }
609
- }
610
-
611
- if (!selectedTransition) {
612
- return undefined;
613
- }
614
-
615
- if (!nextStateNodes.length) {
616
- return {
617
- transitions: [selectedTransition],
618
- entrySet: [],
619
- exitSet: [],
620
- configuration: state.value ? [this] : [],
621
- source: state,
622
- actions: actions
623
- };
624
- }
625
-
626
- var allNextStateNodes = flatten(nextStateNodes.map(function (stateNode) {
627
- return _this.getRelativeStateNodes(stateNode, state.historyValue);
628
- }));
629
- var isInternal = !!selectedTransition.internal;
630
- var reentryNodes = isInternal ? [] : flatten(allNextStateNodes.map(function (n) {
631
- return _this.nodesFromChild(n);
632
- }));
633
- return {
634
- transitions: [selectedTransition],
635
- entrySet: reentryNodes,
636
- exitSet: isInternal ? [] : [this],
637
- configuration: allNextStateNodes,
638
- source: state,
639
- actions: actions
640
- };
641
- };
642
-
643
- StateNode.prototype.nodesFromChild = function (childStateNode) {
644
- if (childStateNode.escapes(this)) {
645
- return [];
646
- }
647
-
648
- var nodes = [];
649
- var marker = childStateNode;
650
-
651
- while (marker && marker !== this) {
652
- nodes.push(marker);
653
- marker = marker.parent;
654
- }
655
-
656
- nodes.push(this); // inclusive
657
-
658
- return nodes;
659
- };
660
- /**
661
- * Whether the given state node "escapes" this state node. If the `stateNode` is equal to or the parent of
662
- * this state node, it does not escape.
663
- */
664
-
665
-
666
- StateNode.prototype.escapes = function (stateNode) {
667
- if (this === stateNode) {
668
- return false;
669
- }
670
-
671
- var parent = this.parent;
672
-
673
- while (parent) {
674
- if (parent === stateNode) {
675
- return false;
676
- }
677
-
678
- parent = parent.parent;
679
- }
680
-
681
- return true;
682
- };
683
-
684
- StateNode.prototype.getActions = function (transition, currentContext, _event, prevState) {
685
- var e_4, _a, e_5, _b;
686
-
687
- var prevConfig = getConfiguration([], prevState ? this.getStateNodes(prevState.value) : [this]);
688
- var resolvedConfig = transition.configuration.length ? getConfiguration(prevConfig, transition.configuration) : prevConfig;
689
-
690
- try {
691
- for (var resolvedConfig_1 = __values(resolvedConfig), resolvedConfig_1_1 = resolvedConfig_1.next(); !resolvedConfig_1_1.done; resolvedConfig_1_1 = resolvedConfig_1.next()) {
692
- var sn = resolvedConfig_1_1.value;
693
-
694
- if (!has(prevConfig, sn)) {
695
- transition.entrySet.push(sn);
696
- }
697
- }
698
- } catch (e_4_1) {
699
- e_4 = {
700
- error: e_4_1
701
- };
702
- } finally {
703
- try {
704
- if (resolvedConfig_1_1 && !resolvedConfig_1_1.done && (_a = resolvedConfig_1.return)) _a.call(resolvedConfig_1);
705
- } finally {
706
- if (e_4) throw e_4.error;
707
- }
708
- }
709
-
710
- try {
711
- for (var prevConfig_1 = __values(prevConfig), prevConfig_1_1 = prevConfig_1.next(); !prevConfig_1_1.done; prevConfig_1_1 = prevConfig_1.next()) {
712
- var sn = prevConfig_1_1.value;
713
-
714
- if (!has(resolvedConfig, sn) || has(transition.exitSet, sn.parent)) {
715
- transition.exitSet.push(sn);
716
- }
717
- }
718
- } catch (e_5_1) {
719
- e_5 = {
720
- error: e_5_1
721
- };
722
- } finally {
723
- try {
724
- if (prevConfig_1_1 && !prevConfig_1_1.done && (_b = prevConfig_1.return)) _b.call(prevConfig_1);
725
- } finally {
726
- if (e_5) throw e_5.error;
727
- }
728
- }
729
-
730
- var doneEvents = flatten(transition.entrySet.map(function (sn) {
731
- var events = [];
732
-
733
- if (sn.type !== 'final') {
734
- return events;
735
- }
736
-
737
- var parent = sn.parent;
738
-
739
- if (!parent.parent) {
740
- return events;
741
- }
742
-
743
- events.push(done(sn.id, sn.doneData), // TODO: deprecate - final states should not emit done events for their own state.
744
- done(parent.id, sn.doneData ? mapContext(sn.doneData, currentContext, _event) : undefined));
745
- var grandparent = parent.parent;
746
-
747
- if (grandparent.type === 'parallel') {
748
- if (getChildren(grandparent).every(function (parentNode) {
749
- return isInFinalState(transition.configuration, parentNode);
750
- })) {
751
- events.push(done(grandparent.id));
752
- }
753
- }
754
-
755
- return events;
756
- }));
757
- transition.exitSet.sort(function (a, b) {
758
- return b.order - a.order;
759
- });
760
- transition.entrySet.sort(function (a, b) {
761
- return a.order - b.order;
762
- });
763
- var entryStates = new Set(transition.entrySet);
764
- var exitStates = new Set(transition.exitSet);
765
-
766
- var _c = __read([flatten(Array.from(entryStates).map(function (stateNode) {
767
- return __spreadArray(__spreadArray([], __read(stateNode.activities.map(function (activity) {
768
- return start(activity);
769
- })), false), __read(stateNode.onEntry), false);
770
- })).concat(doneEvents.map(raise)), flatten(Array.from(exitStates).map(function (stateNode) {
771
- return __spreadArray(__spreadArray([], __read(stateNode.onExit), false), __read(stateNode.activities.map(function (activity) {
772
- return stop(activity);
773
- })), false);
774
- }))], 2),
775
- entryActions = _c[0],
776
- exitActions = _c[1];
777
-
778
- var actions = toActionObjects(exitActions.concat(transition.actions).concat(entryActions), this.machine.options.actions);
779
- return actions;
780
- };
781
- /**
782
- * Determines the next state given the current `state` and sent `event`.
783
- *
784
- * @param state The current State instance or state value
785
- * @param event The event that was sent at the current state
786
- * @param context The current context (extended state) of the current state
787
- */
788
-
789
-
790
- StateNode.prototype.transition = function (state, event, context) {
791
- if (state === void 0) {
792
- state = this.initialState;
793
- }
794
-
795
- var _event = toSCXMLEvent(event);
796
-
797
- var currentState;
798
-
799
- if (state instanceof State) {
800
- currentState = context === undefined ? state : this.resolveState(State.from(state, context));
801
- } else {
802
- var resolvedStateValue = isString(state) ? this.resolve(pathToStateValue(this.getResolvedPath(state))) : this.resolve(state);
803
- var resolvedContext = context !== null && context !== void 0 ? context : this.machine.context;
804
- currentState = this.resolveState(State.from(resolvedStateValue, resolvedContext));
805
- }
806
-
807
- if (!IS_PRODUCTION && _event.name === WILDCARD) {
808
- throw new Error("An event cannot have the wildcard type ('".concat(WILDCARD, "')"));
809
- }
810
-
811
- if (this.strict) {
812
- if (!this.events.includes(_event.name) && !isBuiltInEvent(_event.name)) {
813
- throw new Error("Machine '".concat(this.id, "' does not accept event '").concat(_event.name, "'"));
814
- }
815
- }
816
-
817
- var stateTransition = this._transition(currentState.value, currentState, _event) || {
818
- transitions: [],
819
- configuration: [],
820
- entrySet: [],
821
- exitSet: [],
822
- source: currentState,
823
- actions: []
824
- };
825
- var prevConfig = getConfiguration([], this.getStateNodes(currentState.value));
826
- var resolvedConfig = stateTransition.configuration.length ? getConfiguration(prevConfig, stateTransition.configuration) : prevConfig;
827
- stateTransition.configuration = __spreadArray([], __read(resolvedConfig), false);
828
- return this.resolveTransition(stateTransition, currentState, currentState.context, _event);
829
- };
830
-
831
- StateNode.prototype.resolveRaisedTransition = function (state, _event, originalEvent) {
832
- var _a;
833
-
834
- var currentActions = state.actions;
835
- state = this.transition(state, _event); // Save original event to state
836
- // TODO: this should be the raised event! Delete in V5 (breaking)
837
-
838
- state._event = originalEvent;
839
- state.event = originalEvent.data;
840
-
841
- (_a = state.actions).unshift.apply(_a, __spreadArray([], __read(currentActions), false));
842
-
843
- return state;
844
- };
845
-
846
- StateNode.prototype.resolveTransition = function (stateTransition, currentState, context, _event) {
847
- var e_6, _a;
848
-
849
- var _this = this;
850
-
851
- if (_event === void 0) {
852
- _event = initEvent;
853
- }
854
-
855
- var configuration = stateTransition.configuration; // Transition will "apply" if:
856
- // - this is the initial state (there is no current state)
857
- // - OR there are transitions
858
-
859
- var willTransition = !currentState || stateTransition.transitions.length > 0;
860
- var resolvedStateValue = willTransition ? getValue(this.machine, configuration) : undefined;
861
- var historyValue = currentState ? currentState.historyValue ? currentState.historyValue : stateTransition.source ? this.machine.historyValue(currentState.value) : undefined : undefined;
862
- var actions = this.getActions(stateTransition, context, _event, currentState);
863
- var activities = currentState ? __assign({}, currentState.activities) : {};
864
-
865
- try {
866
- for (var actions_1 = __values(actions), actions_1_1 = actions_1.next(); !actions_1_1.done; actions_1_1 = actions_1.next()) {
867
- var action = actions_1_1.value;
868
-
869
- if (action.type === start$1) {
870
- activities[action.activity.id || action.activity.type] = action;
871
- } else if (action.type === stop$1) {
872
- activities[action.activity.id || action.activity.type] = false;
873
- }
874
- }
875
- } catch (e_6_1) {
876
- e_6 = {
877
- error: e_6_1
878
- };
879
- } finally {
880
- try {
881
- if (actions_1_1 && !actions_1_1.done && (_a = actions_1.return)) _a.call(actions_1);
882
- } finally {
883
- if (e_6) throw e_6.error;
884
- }
885
- }
886
-
887
- var _b = __read(resolveActions(this, currentState, context, _event, actions, this.machine.config.preserveActionOrder), 2),
888
- resolvedActions = _b[0],
889
- updatedContext = _b[1];
890
-
891
- var _c = __read(partition(resolvedActions, function (action) {
892
- return action.type === raise$1 || action.type === send && action.to === SpecialTargets.Internal;
893
- }), 2),
894
- raisedEvents = _c[0],
895
- nonRaisedActions = _c[1];
896
-
897
- var invokeActions = resolvedActions.filter(function (action) {
898
- var _a;
899
-
900
- return action.type === start$1 && ((_a = action.activity) === null || _a === void 0 ? void 0 : _a.type) === invoke;
901
- });
902
- var children = invokeActions.reduce(function (acc, action) {
903
- acc[action.activity.id] = createInvocableActor(action.activity, _this.machine, updatedContext, _event);
904
- return acc;
905
- }, currentState ? __assign({}, currentState.children) : {});
906
- var resolvedConfiguration = willTransition ? stateTransition.configuration : currentState ? currentState.configuration : [];
907
- var isDone = isInFinalState(resolvedConfiguration, this);
908
- var nextState = new State({
909
- value: resolvedStateValue || currentState.value,
910
- context: updatedContext,
911
- _event: _event,
912
- // Persist _sessionid between states
913
- _sessionid: currentState ? currentState._sessionid : null,
914
- historyValue: resolvedStateValue ? historyValue ? updateHistoryValue(historyValue, resolvedStateValue) : undefined : currentState ? currentState.historyValue : undefined,
915
- history: !resolvedStateValue || stateTransition.source ? currentState : undefined,
916
- actions: resolvedStateValue ? nonRaisedActions : [],
917
- activities: resolvedStateValue ? activities : currentState ? currentState.activities : {},
918
- events: [],
919
- configuration: resolvedConfiguration,
920
- transitions: stateTransition.transitions,
921
- children: children,
922
- done: isDone,
923
- tags: getTagsFromConfiguration(resolvedConfiguration),
924
- machine: this
925
- });
926
- var didUpdateContext = context !== updatedContext;
927
- nextState.changed = _event.name === update || didUpdateContext; // Dispose of penultimate histories to prevent memory leaks
928
-
929
- var history = nextState.history;
930
-
931
- if (history) {
932
- delete history.history;
933
- } // There are transient transitions if the machine is not in a final state
934
- // and if some of the state nodes have transient ("always") transitions.
935
-
936
-
937
- var isTransient = !isDone && (this._transient || configuration.some(function (stateNode) {
938
- return stateNode._transient;
939
- })); // If there are no enabled transitions, check if there are transient transitions.
940
- // If there are transient transitions, continue checking for more transitions
941
- // because an transient transition should be triggered even if there are no
942
- // enabled transitions.
943
- //
944
- // If we're already working on an transient transition (by checking
945
- // if the event is a NULL_EVENT), then stop to prevent an infinite loop.
946
- //
947
- // Otherwise, if there are no enabled nor transient transitions, we are done.
948
-
949
- if (!willTransition && (!isTransient || _event.name === NULL_EVENT)) {
950
- return nextState;
951
- }
952
-
953
- var maybeNextState = nextState;
954
-
955
- if (!isDone) {
956
- if (isTransient) {
957
- maybeNextState = this.resolveRaisedTransition(maybeNextState, {
958
- type: nullEvent
959
- }, _event);
960
- }
961
-
962
- while (raisedEvents.length) {
963
- var raisedEvent = raisedEvents.shift();
964
- maybeNextState = this.resolveRaisedTransition(maybeNextState, raisedEvent._event, _event);
965
- }
966
- } // Detect if state changed
967
-
968
-
969
- var changed = maybeNextState.changed || (history ? !!maybeNextState.actions.length || didUpdateContext || typeof history.value !== typeof maybeNextState.value || !stateValuesEqual(maybeNextState.value, history.value) : undefined);
970
- maybeNextState.changed = changed; // Preserve original history after raised events
971
-
972
- maybeNextState.history = history;
973
- return maybeNextState;
974
- };
975
- /**
976
- * Returns the child state node from its relative `stateKey`, or throws.
977
- */
978
-
979
-
980
- StateNode.prototype.getStateNode = function (stateKey) {
981
- if (isStateId(stateKey)) {
982
- return this.machine.getStateNodeById(stateKey);
983
- }
984
-
985
- if (!this.states) {
986
- throw new Error("Unable to retrieve child state '".concat(stateKey, "' from '").concat(this.id, "'; no child states exist."));
987
- }
988
-
989
- var result = this.states[stateKey];
990
-
991
- if (!result) {
992
- throw new Error("Child state '".concat(stateKey, "' does not exist on '").concat(this.id, "'"));
993
- }
994
-
995
- return result;
996
- };
997
- /**
998
- * Returns the state node with the given `stateId`, or throws.
999
- *
1000
- * @param stateId The state ID. The prefix "#" is removed.
1001
- */
1002
-
1003
-
1004
- StateNode.prototype.getStateNodeById = function (stateId) {
1005
- var resolvedStateId = isStateId(stateId) ? stateId.slice(STATE_IDENTIFIER.length) : stateId;
1006
-
1007
- if (resolvedStateId === this.id) {
1008
- return this;
1009
- }
1010
-
1011
- var stateNode = this.machine.idMap[resolvedStateId];
1012
-
1013
- if (!stateNode) {
1014
- throw new Error("Child state node '#".concat(resolvedStateId, "' does not exist on machine '").concat(this.id, "'"));
1015
- }
1016
-
1017
- return stateNode;
1018
- };
1019
- /**
1020
- * Returns the relative state node from the given `statePath`, or throws.
1021
- *
1022
- * @param statePath The string or string array relative path to the state node.
1023
- */
1024
-
1025
-
1026
- StateNode.prototype.getStateNodeByPath = function (statePath) {
1027
- if (typeof statePath === 'string' && isStateId(statePath)) {
1028
- try {
1029
- return this.getStateNodeById(statePath.slice(1));
1030
- } catch (e) {// try individual paths
1031
- // throw e;
1032
- }
1033
- }
1034
-
1035
- var arrayStatePath = toStatePath(statePath, this.delimiter).slice();
1036
- var currentStateNode = this;
1037
-
1038
- while (arrayStatePath.length) {
1039
- var key = arrayStatePath.shift();
1040
-
1041
- if (!key.length) {
1042
- break;
1043
- }
1044
-
1045
- currentStateNode = currentStateNode.getStateNode(key);
1046
- }
1047
-
1048
- return currentStateNode;
1049
- };
1050
- /**
1051
- * Resolves a partial state value with its full representation in this machine.
1052
- *
1053
- * @param stateValue The partial state value to resolve.
1054
- */
1055
-
1056
-
1057
- StateNode.prototype.resolve = function (stateValue) {
1058
- var _a;
1059
-
1060
- var _this = this;
1061
-
1062
- if (!stateValue) {
1063
- return this.initialStateValue || EMPTY_OBJECT; // TODO: type-specific properties
1064
- }
1065
-
1066
- switch (this.type) {
1067
- case 'parallel':
1068
- return mapValues(this.initialStateValue, function (subStateValue, subStateKey) {
1069
- return subStateValue ? _this.getStateNode(subStateKey).resolve(stateValue[subStateKey] || subStateValue) : EMPTY_OBJECT;
1070
- });
1071
-
1072
- case 'compound':
1073
- if (isString(stateValue)) {
1074
- var subStateNode = this.getStateNode(stateValue);
1075
-
1076
- if (subStateNode.type === 'parallel' || subStateNode.type === 'compound') {
1077
- return _a = {}, _a[stateValue] = subStateNode.initialStateValue, _a;
1078
- }
1079
-
1080
- return stateValue;
1081
- }
1082
-
1083
- if (!Object.keys(stateValue).length) {
1084
- return this.initialStateValue || {};
1085
- }
1086
-
1087
- return mapValues(stateValue, function (subStateValue, subStateKey) {
1088
- return subStateValue ? _this.getStateNode(subStateKey).resolve(subStateValue) : EMPTY_OBJECT;
1089
- });
1090
-
1091
- default:
1092
- return stateValue || EMPTY_OBJECT;
1093
- }
1094
- };
1095
-
1096
- StateNode.prototype.getResolvedPath = function (stateIdentifier) {
1097
- if (isStateId(stateIdentifier)) {
1098
- var stateNode = this.machine.idMap[stateIdentifier.slice(STATE_IDENTIFIER.length)];
1099
-
1100
- if (!stateNode) {
1101
- throw new Error("Unable to find state node '".concat(stateIdentifier, "'"));
1102
- }
1103
-
1104
- return stateNode.path;
1105
- }
1106
-
1107
- return toStatePath(stateIdentifier, this.delimiter);
1108
- };
1109
-
1110
- Object.defineProperty(StateNode.prototype, "initialStateValue", {
1111
- get: function () {
1112
- var _a;
1113
-
1114
- if (this.__cache.initialStateValue) {
1115
- return this.__cache.initialStateValue;
1116
- }
1117
-
1118
- var initialStateValue;
1119
-
1120
- if (this.type === 'parallel') {
1121
- initialStateValue = mapFilterValues(this.states, function (state) {
1122
- return state.initialStateValue || EMPTY_OBJECT;
1123
- }, function (stateNode) {
1124
- return !(stateNode.type === 'history');
1125
- });
1126
- } else if (this.initial !== undefined) {
1127
- if (!this.states[this.initial]) {
1128
- throw new Error("Initial state '".concat(this.initial, "' not found on '").concat(this.key, "'"));
1129
- }
1130
-
1131
- initialStateValue = isLeafNode(this.states[this.initial]) ? this.initial : (_a = {}, _a[this.initial] = this.states[this.initial].initialStateValue, _a);
1132
- } else {
1133
- // The finite state value of a machine without child states is just an empty object
1134
- initialStateValue = {};
1135
- }
1136
-
1137
- this.__cache.initialStateValue = initialStateValue;
1138
- return this.__cache.initialStateValue;
1139
- },
1140
- enumerable: false,
1141
- configurable: true
1142
- });
1143
-
1144
- StateNode.prototype.getInitialState = function (stateValue, context) {
1145
- this._init(); // TODO: this should be in the constructor (see note in constructor)
1146
-
1147
-
1148
- var configuration = this.getStateNodes(stateValue);
1149
- return this.resolveTransition({
1150
- configuration: configuration,
1151
- entrySet: configuration,
1152
- exitSet: [],
1153
- transitions: [],
1154
- source: undefined,
1155
- actions: []
1156
- }, undefined, context !== null && context !== void 0 ? context : this.machine.context, undefined);
1157
- };
1158
-
1159
- Object.defineProperty(StateNode.prototype, "initialState", {
1160
- /**
1161
- * The initial State instance, which includes all actions to be executed from
1162
- * entering the initial state.
1163
- */
1164
- get: function () {
1165
- var initialStateValue = this.initialStateValue;
1166
-
1167
- if (!initialStateValue) {
1168
- throw new Error("Cannot retrieve initial state from simple state '".concat(this.id, "'."));
1169
- }
1170
-
1171
- return this.getInitialState(initialStateValue);
1172
- },
1173
- enumerable: false,
1174
- configurable: true
1175
- });
1176
- Object.defineProperty(StateNode.prototype, "target", {
1177
- /**
1178
- * The target state value of the history state node, if it exists. This represents the
1179
- * default state value to transition to if no history value exists yet.
1180
- */
1181
- get: function () {
1182
- var target;
1183
-
1184
- if (this.type === 'history') {
1185
- var historyConfig = this.config;
1186
-
1187
- if (isString(historyConfig.target)) {
1188
- target = isStateId(historyConfig.target) ? pathToStateValue(this.machine.getStateNodeById(historyConfig.target).path.slice(this.path.length - 1)) : historyConfig.target;
1189
- } else {
1190
- target = historyConfig.target;
1191
- }
1192
- }
1193
-
1194
- return target;
1195
- },
1196
- enumerable: false,
1197
- configurable: true
1198
- });
1199
- /**
1200
- * Returns the leaf nodes from a state path relative to this state node.
1201
- *
1202
- * @param relativeStateId The relative state path to retrieve the state nodes
1203
- * @param history The previous state to retrieve history
1204
- * @param resolve Whether state nodes should resolve to initial child state nodes
1205
- */
1206
-
1207
- StateNode.prototype.getRelativeStateNodes = function (relativeStateId, historyValue, resolve) {
1208
- if (resolve === void 0) {
1209
- resolve = true;
1210
- }
1211
-
1212
- return resolve ? relativeStateId.type === 'history' ? relativeStateId.resolveHistory(historyValue) : relativeStateId.initialStateNodes : [relativeStateId];
1213
- };
1214
-
1215
- Object.defineProperty(StateNode.prototype, "initialStateNodes", {
1216
- get: function () {
1217
- var _this = this;
1218
-
1219
- if (isLeafNode(this)) {
1220
- return [this];
1221
- } // Case when state node is compound but no initial state is defined
1222
-
1223
-
1224
- if (this.type === 'compound' && !this.initial) {
1225
- if (!IS_PRODUCTION) {
1226
- warn(false, "Compound state node '".concat(this.id, "' has no initial state."));
1227
- }
1228
-
1229
- return [this];
1230
- }
1231
-
1232
- var initialStateNodePaths = toStatePaths(this.initialStateValue);
1233
- return flatten(initialStateNodePaths.map(function (initialPath) {
1234
- return _this.getFromRelativePath(initialPath);
1235
- }));
1236
- },
1237
- enumerable: false,
1238
- configurable: true
1239
- });
1240
- /**
1241
- * Retrieves state nodes from a relative path to this state node.
1242
- *
1243
- * @param relativePath The relative path from this state node
1244
- * @param historyValue
1245
- */
1246
-
1247
- StateNode.prototype.getFromRelativePath = function (relativePath) {
1248
- if (!relativePath.length) {
1249
- return [this];
1250
- }
1251
-
1252
- var _a = __read(relativePath),
1253
- stateKey = _a[0],
1254
- childStatePath = _a.slice(1);
1255
-
1256
- if (!this.states) {
1257
- throw new Error("Cannot retrieve subPath '".concat(stateKey, "' from node with no states"));
1258
- }
1259
-
1260
- var childStateNode = this.getStateNode(stateKey);
1261
-
1262
- if (childStateNode.type === 'history') {
1263
- return childStateNode.resolveHistory();
1264
- }
1265
-
1266
- if (!this.states[stateKey]) {
1267
- throw new Error("Child state '".concat(stateKey, "' does not exist on '").concat(this.id, "'"));
1268
- }
1269
-
1270
- return this.states[stateKey].getFromRelativePath(childStatePath);
1271
- };
1272
-
1273
- StateNode.prototype.historyValue = function (relativeStateValue) {
1274
- if (!Object.keys(this.states).length) {
1275
- return undefined;
1276
- }
1277
-
1278
- return {
1279
- current: relativeStateValue || this.initialStateValue,
1280
- states: mapFilterValues(this.states, function (stateNode, key) {
1281
- if (!relativeStateValue) {
1282
- return stateNode.historyValue();
1283
- }
1284
-
1285
- var subStateValue = isString(relativeStateValue) ? undefined : relativeStateValue[key];
1286
- return stateNode.historyValue(subStateValue || stateNode.initialStateValue);
1287
- }, function (stateNode) {
1288
- return !stateNode.history;
1289
- })
1290
- };
1291
- };
1292
- /**
1293
- * Resolves to the historical value(s) of the parent state node,
1294
- * represented by state nodes.
1295
- *
1296
- * @param historyValue
1297
- */
1298
-
1299
-
1300
- StateNode.prototype.resolveHistory = function (historyValue) {
1301
- var _this = this;
1302
-
1303
- if (this.type !== 'history') {
1304
- return [this];
1305
- }
1306
-
1307
- var parent = this.parent;
1308
-
1309
- if (!historyValue) {
1310
- var historyTarget = this.target;
1311
- return historyTarget ? flatten(toStatePaths(historyTarget).map(function (relativeChildPath) {
1312
- return parent.getFromRelativePath(relativeChildPath);
1313
- })) : parent.initialStateNodes;
1314
- }
1315
-
1316
- var subHistoryValue = nestedPath(parent.path, 'states')(historyValue).current;
1317
-
1318
- if (isString(subHistoryValue)) {
1319
- return [parent.getStateNode(subHistoryValue)];
1320
- }
1321
-
1322
- return flatten(toStatePaths(subHistoryValue).map(function (subStatePath) {
1323
- return _this.history === 'deep' ? parent.getFromRelativePath(subStatePath) : [parent.states[subStatePath[0]]];
1324
- }));
1325
- };
1326
-
1327
- Object.defineProperty(StateNode.prototype, "stateIds", {
1328
- /**
1329
- * All the state node IDs of this state node and its descendant state nodes.
1330
- */
1331
- get: function () {
1332
- var _this = this;
1333
-
1334
- var childStateIds = flatten(Object.keys(this.states).map(function (stateKey) {
1335
- return _this.states[stateKey].stateIds;
1336
- }));
1337
- return [this.id].concat(childStateIds);
1338
- },
1339
- enumerable: false,
1340
- configurable: true
1341
- });
1342
- Object.defineProperty(StateNode.prototype, "events", {
1343
- /**
1344
- * All the event types accepted by this state node and its descendants.
1345
- */
1346
- get: function () {
1347
- var e_7, _a, e_8, _b;
1348
-
1349
- if (this.__cache.events) {
1350
- return this.__cache.events;
1351
- }
1352
-
1353
- var states = this.states;
1354
- var events = new Set(this.ownEvents);
1355
-
1356
- if (states) {
1357
- try {
1358
- for (var _c = __values(Object.keys(states)), _d = _c.next(); !_d.done; _d = _c.next()) {
1359
- var stateId = _d.value;
1360
- var state = states[stateId];
1361
-
1362
- if (state.states) {
1363
- try {
1364
- for (var _e = (e_8 = void 0, __values(state.events)), _f = _e.next(); !_f.done; _f = _e.next()) {
1365
- var event_1 = _f.value;
1366
- events.add("".concat(event_1));
1367
- }
1368
- } catch (e_8_1) {
1369
- e_8 = {
1370
- error: e_8_1
1371
- };
1372
- } finally {
1373
- try {
1374
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1375
- } finally {
1376
- if (e_8) throw e_8.error;
1377
- }
1378
- }
1379
- }
1380
- }
1381
- } catch (e_7_1) {
1382
- e_7 = {
1383
- error: e_7_1
1384
- };
1385
- } finally {
1386
- try {
1387
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1388
- } finally {
1389
- if (e_7) throw e_7.error;
1390
- }
1391
- }
1392
- }
1393
-
1394
- return this.__cache.events = Array.from(events);
1395
- },
1396
- enumerable: false,
1397
- configurable: true
1398
- });
1399
- Object.defineProperty(StateNode.prototype, "ownEvents", {
1400
- /**
1401
- * All the events that have transitions directly from this state node.
1402
- *
1403
- * Excludes any inert events.
1404
- */
1405
- get: function () {
1406
- var events = new Set(this.transitions.filter(function (transition) {
1407
- return !(!transition.target && !transition.actions.length && transition.internal);
1408
- }).map(function (transition) {
1409
- return transition.eventType;
1410
- }));
1411
- return Array.from(events);
1412
- },
1413
- enumerable: false,
1414
- configurable: true
1415
- });
1416
-
1417
- StateNode.prototype.resolveTarget = function (_target) {
1418
- var _this = this;
1419
-
1420
- if (_target === undefined) {
1421
- // an undefined target signals that the state node should not transition from that state when receiving that event
1422
- return undefined;
1423
- }
1424
-
1425
- return _target.map(function (target) {
1426
- if (!isString(target)) {
1427
- return target;
1428
- }
1429
-
1430
- var isInternalTarget = target[0] === _this.delimiter; // If internal target is defined on machine,
1431
- // do not include machine key on target
1432
-
1433
- if (isInternalTarget && !_this.parent) {
1434
- return _this.getStateNodeByPath(target.slice(1));
1435
- }
1436
-
1437
- var resolvedTarget = isInternalTarget ? _this.key + target : target;
1438
-
1439
- if (_this.parent) {
1440
- try {
1441
- var targetStateNode = _this.parent.getStateNodeByPath(resolvedTarget);
1442
-
1443
- return targetStateNode;
1444
- } catch (err) {
1445
- throw new Error("Invalid transition definition for state node '".concat(_this.id, "':\n").concat(err.message));
1446
- }
1447
- } else {
1448
- return _this.getStateNodeByPath(resolvedTarget);
1449
- }
1450
- });
1451
- };
1452
-
1453
- StateNode.prototype.formatTransition = function (transitionConfig) {
1454
- var _this = this;
1455
-
1456
- var normalizedTarget = normalizeTarget(transitionConfig.target);
1457
- var internal = 'internal' in transitionConfig ? transitionConfig.internal : normalizedTarget ? normalizedTarget.some(function (_target) {
1458
- return isString(_target) && _target[0] === _this.delimiter;
1459
- }) : true;
1460
- var guards = this.machine.options.guards;
1461
- var target = this.resolveTarget(normalizedTarget);
1462
-
1463
- var transition = __assign(__assign({}, transitionConfig), {
1464
- actions: toActionObjects(toArray(transitionConfig.actions)),
1465
- cond: toGuard(transitionConfig.cond, guards),
1466
- target: target,
1467
- source: this,
1468
- internal: internal,
1469
- eventType: transitionConfig.event,
1470
- toJSON: function () {
1471
- return __assign(__assign({}, transition), {
1472
- target: transition.target ? transition.target.map(function (t) {
1473
- return "#".concat(t.id);
1474
- }) : undefined,
1475
- source: "#".concat(_this.id)
1476
- });
1477
- }
1478
- });
1479
-
1480
- return transition;
1481
- };
1482
-
1483
- StateNode.prototype.formatTransitions = function () {
1484
- var e_9, _a;
1485
-
1486
- var _this = this;
1487
-
1488
- var onConfig;
1489
-
1490
- if (!this.config.on) {
1491
- onConfig = [];
1492
- } else if (Array.isArray(this.config.on)) {
1493
- onConfig = this.config.on;
1494
- } else {
1495
- var _b = this.config.on,
1496
- _c = WILDCARD,
1497
- _d = _b[_c],
1498
- wildcardConfigs = _d === void 0 ? [] : _d,
1499
- strictTransitionConfigs_1 = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
1500
-
1501
- onConfig = flatten(Object.keys(strictTransitionConfigs_1).map(function (key) {
1502
- if (!IS_PRODUCTION && key === NULL_EVENT) {
1503
- 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 \"#".concat(_this.id, "\"."));
1504
- }
1505
-
1506
- var transitionConfigArray = toTransitionConfigArray(key, strictTransitionConfigs_1[key]);
1507
-
1508
- if (!IS_PRODUCTION) {
1509
- validateArrayifiedTransitions(_this, key, transitionConfigArray);
1510
- }
1511
-
1512
- return transitionConfigArray;
1513
- }).concat(toTransitionConfigArray(WILDCARD, wildcardConfigs)));
1514
- }
1515
-
1516
- var eventlessConfig = this.config.always ? toTransitionConfigArray('', this.config.always) : [];
1517
- var doneConfig = this.config.onDone ? toTransitionConfigArray(String(done(this.id)), this.config.onDone) : [];
1518
-
1519
- if (!IS_PRODUCTION) {
1520
- warn(!(this.config.onDone && !this.parent), "Root nodes cannot have an \".onDone\" transition. Please check the config of \"".concat(this.id, "\"."));
1521
- }
1522
-
1523
- var invokeConfig = flatten(this.invoke.map(function (invokeDef) {
1524
- var settleTransitions = [];
1525
-
1526
- if (invokeDef.onDone) {
1527
- settleTransitions.push.apply(settleTransitions, __spreadArray([], __read(toTransitionConfigArray(String(doneInvoke(invokeDef.id)), invokeDef.onDone)), false));
1528
- }
1529
-
1530
- if (invokeDef.onError) {
1531
- settleTransitions.push.apply(settleTransitions, __spreadArray([], __read(toTransitionConfigArray(String(error(invokeDef.id)), invokeDef.onError)), false));
1532
- }
1533
-
1534
- return settleTransitions;
1535
- }));
1536
- var delayedTransitions = this.after;
1537
- var formattedTransitions = flatten(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(doneConfig), false), __read(invokeConfig), false), __read(onConfig), false), __read(eventlessConfig), false).map(function (transitionConfig) {
1538
- return toArray(transitionConfig).map(function (transition) {
1539
- return _this.formatTransition(transition);
1540
- });
1541
- }));
1542
-
1543
- try {
1544
- for (var delayedTransitions_1 = __values(delayedTransitions), delayedTransitions_1_1 = delayedTransitions_1.next(); !delayedTransitions_1_1.done; delayedTransitions_1_1 = delayedTransitions_1.next()) {
1545
- var delayedTransition = delayedTransitions_1_1.value;
1546
- formattedTransitions.push(delayedTransition);
1547
- }
1548
- } catch (e_9_1) {
1549
- e_9 = {
1550
- error: e_9_1
1551
- };
1552
- } finally {
1553
- try {
1554
- if (delayedTransitions_1_1 && !delayedTransitions_1_1.done && (_a = delayedTransitions_1.return)) _a.call(delayedTransitions_1);
1555
- } finally {
1556
- if (e_9) throw e_9.error;
1557
- }
1558
- }
1559
-
1560
- return formattedTransitions;
1561
- };
1562
-
1563
- return StateNode;
1564
- }();
1565
-
1566
- export { StateNode };