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