xstate 4.35.2 → 5.0.0-alpha.1

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 (203) hide show
  1. package/README.md +25 -16
  2. package/actions/dist/xstate-actions.cjs.d.ts +1 -0
  3. package/actions/dist/xstate-actions.cjs.dev.js +30 -0
  4. package/actions/dist/xstate-actions.cjs.js +7 -0
  5. package/actions/dist/xstate-actions.cjs.prod.js +30 -0
  6. package/actions/dist/xstate-actions.esm.js +2 -0
  7. package/actions/dist/xstate-actions.umd.min.js +2 -0
  8. package/actions/dist/xstate-actions.umd.min.js.map +1 -0
  9. package/actions/dynamicAction.ts +34 -0
  10. package/actions/package.json +8 -0
  11. package/actors/dist/xstate-actors.cjs.d.ts +1 -0
  12. package/actors/dist/xstate-actors.cjs.dev.js +20 -0
  13. package/actors/dist/xstate-actors.cjs.js +7 -0
  14. package/actors/dist/xstate-actors.cjs.prod.js +20 -0
  15. package/actors/dist/xstate-actors.esm.js +2 -0
  16. package/actors/dist/xstate-actors.umd.min.js +2 -0
  17. package/actors/dist/xstate-actors.umd.min.js.map +1 -0
  18. package/actors/package.json +8 -0
  19. package/dev/dist/xstate-dev.cjs.d.ts +1 -0
  20. package/dev/dist/xstate-dev.cjs.dev.js +11 -0
  21. package/dev/dist/xstate-dev.cjs.js +7 -0
  22. package/{lib/devTools.js → dev/dist/xstate-dev.cjs.prod.js} +17 -10
  23. package/dev/dist/xstate-dev.esm.js +1 -0
  24. package/dev/dist/xstate-dev.umd.min.js +2 -0
  25. package/dev/dist/xstate-dev.umd.min.js.map +1 -0
  26. package/dev/package.json +8 -0
  27. package/dist/actions-7ea9280f.cjs.prod.js +4674 -0
  28. package/dist/actions-b08d0da8.cjs.dev.js +4718 -0
  29. package/dist/actions-b673cb13.esm.js +4660 -0
  30. package/dist/declarations/actions/dynamicAction.d.ts +5 -0
  31. package/dist/declarations/src/Machine.d.ts +4 -0
  32. package/dist/declarations/src/Mailbox.d.ts +12 -0
  33. package/{es → dist/declarations/src}/SimulatedClock.d.ts +16 -17
  34. package/dist/declarations/src/State.d.ts +97 -0
  35. package/dist/declarations/src/StateMachine.d.ts +120 -0
  36. package/dist/declarations/src/StateNode.d.ts +127 -0
  37. package/{es → dist/declarations/src}/actionTypes.d.ts +16 -20
  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 +51 -0
  46. package/dist/declarations/src/actions/stop.d.ts +7 -0
  47. package/dist/declarations/src/actions.d.ts +43 -0
  48. package/dist/declarations/src/actors.d.ts +37 -0
  49. package/dist/declarations/src/constants.d.ts +5 -0
  50. package/{lib/devTools.d.ts → dist/declarations/src/dev/index.d.ts} +14 -14
  51. package/{es → dist/declarations/src}/environment.d.ts +1 -2
  52. package/dist/declarations/src/guards.d.ts +8 -0
  53. package/dist/declarations/src/index.d.ts +44 -0
  54. package/dist/declarations/src/interpreter.d.ts +108 -0
  55. package/{es → dist/declarations/src}/mapState.d.ts +3 -4
  56. package/dist/declarations/src/memo.d.ts +2 -0
  57. package/dist/declarations/src/registry.d.ts +8 -0
  58. package/{es → dist/declarations/src}/schema.d.ts +2 -3
  59. package/dist/declarations/src/spawn.d.ts +2 -0
  60. package/dist/declarations/src/stateUtils.d.ts +70 -0
  61. package/dist/declarations/src/symbolObservable.d.ts +1 -0
  62. package/{lib → dist/declarations/src}/typegenTypes.d.ts +132 -133
  63. package/dist/declarations/src/types.d.ts +1001 -0
  64. package/dist/declarations/src/utils.d.ts +54 -0
  65. package/{es/devTools.js → dist/index-20b364c4.esm.js} +18 -6
  66. package/dist/index-2a564d03.cjs.dev.js +64 -0
  67. package/dist/xstate.cjs.d.ts +1 -0
  68. package/dist/xstate.cjs.dev.js +993 -0
  69. package/dist/xstate.cjs.js +7 -0
  70. package/dist/xstate.cjs.prod.js +993 -0
  71. package/dist/xstate.esm.js +952 -0
  72. package/dist/xstate.umd.min.js +2 -0
  73. package/dist/xstate.umd.min.js.map +1 -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/dist/xstate-guards.umd.min.js +2 -0
  80. package/guards/dist/xstate-guards.umd.min.js.map +1 -0
  81. package/guards/package.json +8 -0
  82. package/invoke/package.json +4 -0
  83. package/package.json +44 -30
  84. package/dist/xstate.interpreter.js +0 -15
  85. package/dist/xstate.js +0 -15
  86. package/dist/xstate.web.js +0 -15
  87. package/es/Actor.d.ts +0 -25
  88. package/es/Actor.js +0 -99
  89. package/es/Machine.d.ts +0 -12
  90. package/es/Machine.js +0 -21
  91. package/es/SimulatedClock.js +0 -81
  92. package/es/State.d.ts +0 -123
  93. package/es/State.js +0 -274
  94. package/es/StateNode.d.ts +0 -282
  95. package/es/StateNode.js +0 -1587
  96. package/es/_virtual/_tslib.js +0 -78
  97. package/es/actionTypes.js +0 -22
  98. package/es/actions.d.ts +0 -157
  99. package/es/actions.js +0 -630
  100. package/es/behaviors.d.ts +0 -37
  101. package/es/behaviors.js +0 -131
  102. package/es/constants.d.ts +0 -6
  103. package/es/constants.js +0 -6
  104. package/es/devTools.d.ts +0 -14
  105. package/es/each.d.ts +0 -4
  106. package/es/each.js +0 -13
  107. package/es/environment.js +0 -3
  108. package/es/index.d.ts +0 -22
  109. package/es/index.js +0 -25
  110. package/es/interpreter.d.ts +0 -217
  111. package/es/interpreter.js +0 -1488
  112. package/es/invoke.d.ts +0 -11
  113. package/es/invokeUtils.d.ts +0 -7
  114. package/es/invokeUtils.js +0 -40
  115. package/es/json.d.ts +0 -31
  116. package/es/json.js +0 -86
  117. package/es/mapState.js +0 -32
  118. package/es/match.d.ts +0 -9
  119. package/es/match.js +0 -34
  120. package/es/model.d.ts +0 -8
  121. package/es/model.js +0 -50
  122. package/es/model.types.d.ts +0 -56
  123. package/es/patterns.d.ts +0 -14
  124. package/es/patterns.js +0 -48
  125. package/es/registry.d.ts +0 -9
  126. package/es/registry.js +0 -19
  127. package/es/scheduler.d.ts +0 -17
  128. package/es/scheduler.js +0 -79
  129. package/es/schema.js +0 -6
  130. package/es/scxml.d.ts +0 -6
  131. package/es/serviceScope.d.ts +0 -4
  132. package/es/serviceScope.js +0 -16
  133. package/es/stateUtils.d.ts +0 -18
  134. package/es/stateUtils.js +0 -262
  135. package/es/typegenTypes.d.ts +0 -133
  136. package/es/types.d.ts +0 -1040
  137. package/es/types.js +0 -33
  138. package/es/utils.d.ts +0 -72
  139. package/es/utils.js +0 -622
  140. package/es/waitFor.d.ts +0 -34
  141. package/es/waitFor.js +0 -73
  142. package/lib/Actor.d.ts +0 -25
  143. package/lib/Actor.js +0 -108
  144. package/lib/Machine.d.ts +0 -12
  145. package/lib/Machine.js +0 -26
  146. package/lib/SimulatedClock.d.ts +0 -17
  147. package/lib/SimulatedClock.js +0 -85
  148. package/lib/State.d.ts +0 -123
  149. package/lib/State.js +0 -282
  150. package/lib/StateNode.d.ts +0 -282
  151. package/lib/StateNode.js +0 -1591
  152. package/lib/_virtual/_tslib.js +0 -85
  153. package/lib/actionTypes.d.ts +0 -20
  154. package/lib/actionTypes.js +0 -43
  155. package/lib/actions.d.ts +0 -157
  156. package/lib/actions.js +0 -662
  157. package/lib/behaviors.d.ts +0 -37
  158. package/lib/behaviors.js +0 -137
  159. package/lib/constants.d.ts +0 -6
  160. package/lib/constants.js +0 -13
  161. package/lib/each.d.ts +0 -4
  162. package/lib/each.js +0 -17
  163. package/lib/environment.d.ts +0 -2
  164. package/lib/environment.js +0 -7
  165. package/lib/index.d.ts +0 -22
  166. package/lib/index.js +0 -64
  167. package/lib/interpreter.d.ts +0 -217
  168. package/lib/interpreter.js +0 -1494
  169. package/lib/invoke.d.ts +0 -11
  170. package/lib/invoke.js +0 -20
  171. package/lib/invokeUtils.d.ts +0 -7
  172. package/lib/invokeUtils.js +0 -45
  173. package/lib/json.d.ts +0 -31
  174. package/lib/json.js +0 -94
  175. package/lib/mapState.d.ts +0 -4
  176. package/lib/mapState.js +0 -36
  177. package/lib/match.d.ts +0 -9
  178. package/lib/match.js +0 -38
  179. package/lib/model.d.ts +0 -8
  180. package/lib/model.js +0 -54
  181. package/lib/model.types.d.ts +0 -56
  182. package/lib/model.types.js +0 -2
  183. package/lib/patterns.d.ts +0 -14
  184. package/lib/patterns.js +0 -53
  185. package/lib/registry.d.ts +0 -9
  186. package/lib/registry.js +0 -23
  187. package/lib/scheduler.d.ts +0 -17
  188. package/lib/scheduler.js +0 -83
  189. package/lib/schema.d.ts +0 -3
  190. package/lib/schema.js +0 -11
  191. package/lib/scxml.d.ts +0 -6
  192. package/lib/scxml.js +0 -350
  193. package/lib/serviceScope.d.ts +0 -4
  194. package/lib/serviceScope.js +0 -21
  195. package/lib/stateUtils.d.ts +0 -18
  196. package/lib/stateUtils.js +0 -277
  197. package/lib/typegenTypes.js +0 -2
  198. package/lib/types.d.ts +0 -1040
  199. package/lib/types.js +0 -35
  200. package/lib/utils.d.ts +0 -72
  201. package/lib/utils.js +0 -669
  202. package/lib/waitFor.d.ts +0 -34
  203. package/lib/waitFor.js +0 -77
package/lib/StateNode.js DELETED
@@ -1,1591 +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
- if (_context === void 0) {
60
- _context = 'context' in config ? config.context : undefined;
61
- }
62
-
63
- var _this = this;
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.getAllChildren(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 configuration = utils.flatten(Object.keys(transitionMap).map(function (key) {
528
- return transitionMap[key].configuration;
529
- }));
530
- return {
531
- transitions: enabledTransitions,
532
- exitSet: utils.flatten(stateTransitions.map(function (t) {
533
- return t.exitSet;
534
- })),
535
- configuration: configuration,
536
- source: state,
537
- actions: utils.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 (utils.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, utils.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 = _tslib.__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 ? utils.isString(stateIn) && isStateId(stateIn) ? // Check if in state by ID
579
- state.matches(utils.toStateValue(this.getStateNodeById(stateIn).path, this.delimiter)) : // Check if in state by relative grandparent
580
- utils.matchesState(utils.toStateValue(stateIn, this.delimiter), utils.path(this.path.slice(0, -2))(state.value)) : true;
581
- var guardPassed = false;
582
-
583
- try {
584
- guardPassed = !cond || utils.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, _tslib.__spreadArray([], _tslib.__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
- exitSet: [],
619
- configuration: state.value ? [this] : [],
620
- source: state,
621
- actions: actions
622
- };
623
- }
624
-
625
- var allNextStateNodes = utils.flatten(nextStateNodes.map(function (stateNode) {
626
- return _this.getRelativeStateNodes(stateNode, state.historyValue);
627
- }));
628
- var isInternal = !!selectedTransition.internal;
629
- return {
630
- transitions: [selectedTransition],
631
- exitSet: isInternal ? [] : utils.flatten(nextStateNodes.map(function (targetNode) {
632
- return _this.getPotentiallyReenteringNodes(targetNode);
633
- })),
634
- configuration: allNextStateNodes,
635
- source: state,
636
- actions: actions
637
- };
638
- }; // even though the name of this function mentions reentry nodes
639
- // we are pushing its result into `exitSet`
640
- // that's because what we exit might be reentered (it's an invariant of reentrancy)
641
-
642
-
643
- StateNode.prototype.getPotentiallyReenteringNodes = function (targetNode) {
644
- if (this.order < targetNode.order) {
645
- return [this];
646
- }
647
-
648
- var nodes = [];
649
- var marker = this;
650
- var possibleAncestor = targetNode;
651
-
652
- while (marker && marker !== possibleAncestor) {
653
- nodes.push(marker);
654
- marker = marker.parent;
655
- }
656
-
657
- if (marker !== possibleAncestor) {
658
- // we never got to `possibleAncestor`, therefore the initial `marker` "escapes" it
659
- // it's in a different part of the tree so no states will be reentered for such an external transition
660
- return [];
661
- }
662
-
663
- nodes.push(possibleAncestor);
664
- return nodes;
665
- };
666
-
667
- StateNode.prototype.getActions = function (resolvedConfig, isDone, transition, currentContext, _event, prevState, predictableExec) {
668
- var e_4, _a, e_5, _b;
669
-
670
- var _this = this;
671
-
672
- var prevConfig = prevState ? stateUtils.getConfiguration([], this.getStateNodes(prevState.value)) : [];
673
- var entrySet = new Set();
674
-
675
- try {
676
- for (var _c = _tslib.__values(Array.from(resolvedConfig).sort(function (a, b) {
677
- return a.order - b.order;
678
- })), _d = _c.next(); !_d.done; _d = _c.next()) {
679
- var sn = _d.value;
680
-
681
- if (!stateUtils.has(prevConfig, sn) || stateUtils.has(transition.exitSet, sn) || sn.parent && entrySet.has(sn.parent)) {
682
- entrySet.add(sn);
683
- }
684
- }
685
- } catch (e_4_1) {
686
- e_4 = {
687
- error: e_4_1
688
- };
689
- } finally {
690
- try {
691
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
692
- } finally {
693
- if (e_4) throw e_4.error;
694
- }
695
- }
696
-
697
- try {
698
- for (var prevConfig_1 = _tslib.__values(prevConfig), prevConfig_1_1 = prevConfig_1.next(); !prevConfig_1_1.done; prevConfig_1_1 = prevConfig_1.next()) {
699
- var sn = prevConfig_1_1.value;
700
-
701
- if (!stateUtils.has(resolvedConfig, sn) || stateUtils.has(transition.exitSet, sn.parent)) {
702
- transition.exitSet.push(sn);
703
- }
704
- }
705
- } catch (e_5_1) {
706
- e_5 = {
707
- error: e_5_1
708
- };
709
- } finally {
710
- try {
711
- if (prevConfig_1_1 && !prevConfig_1_1.done && (_b = prevConfig_1.return)) _b.call(prevConfig_1);
712
- } finally {
713
- if (e_5) throw e_5.error;
714
- }
715
- }
716
-
717
- transition.exitSet.sort(function (a, b) {
718
- return b.order - a.order;
719
- });
720
- var entryStates = Array.from(entrySet).sort(function (a, b) {
721
- return a.order - b.order;
722
- });
723
- var exitStates = new Set(transition.exitSet);
724
- var doneEvents = utils.flatten(entryStates.map(function (sn) {
725
- var events = [];
726
-
727
- if (sn.type !== 'final') {
728
- return events;
729
- }
730
-
731
- var parent = sn.parent;
732
-
733
- if (!parent.parent) {
734
- return events;
735
- }
736
-
737
- events.push(actions.done(sn.id, sn.doneData), // TODO: deprecate - final states should not emit done events for their own state.
738
- actions.done(parent.id, sn.doneData ? utils.mapContext(sn.doneData, currentContext, _event) : undefined));
739
- var grandparent = parent.parent;
740
-
741
- if (grandparent.type === 'parallel') {
742
- if (stateUtils.getChildren(grandparent).every(function (parentNode) {
743
- return stateUtils.isInFinalState(transition.configuration, parentNode);
744
- })) {
745
- events.push(actions.done(grandparent.id));
746
- }
747
- }
748
-
749
- return events;
750
- }));
751
- var entryActions = entryStates.map(function (stateNode) {
752
- var entryActions = stateNode.onEntry;
753
- var invokeActions = stateNode.activities.map(function (activity) {
754
- return actions.start(activity);
755
- });
756
- return {
757
- type: 'entry',
758
- actions: actions.toActionObjects(predictableExec ? _tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(entryActions), false), _tslib.__read(invokeActions), false) : _tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(invokeActions), false), _tslib.__read(entryActions), false), _this.machine.options.actions)
759
- };
760
- }).concat({
761
- type: 'state_done',
762
- actions: doneEvents.map(actions.raise)
763
- });
764
- var exitActions = Array.from(exitStates).map(function (stateNode) {
765
- return {
766
- type: 'exit',
767
- actions: actions.toActionObjects(_tslib.__spreadArray(_tslib.__spreadArray([], _tslib.__read(stateNode.onExit), false), _tslib.__read(stateNode.activities.map(function (activity) {
768
- return actions.stop(activity);
769
- })), false), _this.machine.options.actions)
770
- };
771
- });
772
- var actions$1 = exitActions.concat({
773
- type: 'transition',
774
- actions: actions.toActionObjects(transition.actions, this.machine.options.actions)
775
- }).concat(entryActions);
776
-
777
- if (isDone) {
778
- var stopActions = actions.toActionObjects(utils.flatten(_tslib.__spreadArray([], _tslib.__read(resolvedConfig), false).sort(function (a, b) {
779
- return b.order - a.order;
780
- }).map(function (stateNode) {
781
- return stateNode.onExit;
782
- })), this.machine.options.actions).filter(function (action) {
783
- return action.type !== actionTypes.raise && (action.type !== actionTypes.send || !!action.to && action.to !== types.SpecialTargets.Internal);
784
- });
785
- return actions$1.concat({
786
- type: 'stop',
787
- actions: stopActions
788
- });
789
- }
790
-
791
- return actions$1;
792
- };
793
- /**
794
- * Determines the next state given the current `state` and sent `event`.
795
- *
796
- * @param state The current State instance or state value
797
- * @param event The event that was sent at the current state
798
- * @param context The current context (extended state) of the current state
799
- */
800
-
801
-
802
- StateNode.prototype.transition = function (state, event, context, exec) {
803
- if (state === void 0) {
804
- state = this.initialState;
805
- }
806
-
807
- var _event = utils.toSCXMLEvent(event);
808
-
809
- var currentState;
810
-
811
- if (state instanceof State.State) {
812
- currentState = context === undefined ? state : this.resolveState(State.State.from(state, context));
813
- } else {
814
- var resolvedStateValue = utils.isString(state) ? this.resolve(utils.pathToStateValue(this.getResolvedPath(state))) : this.resolve(state);
815
- var resolvedContext = context !== null && context !== void 0 ? context : this.machine.context;
816
- currentState = this.resolveState(State.State.from(resolvedStateValue, resolvedContext));
817
- }
818
-
819
- if (!environment.IS_PRODUCTION && _event.name === WILDCARD) {
820
- throw new Error("An event cannot have the wildcard type ('".concat(WILDCARD, "')"));
821
- }
822
-
823
- if (this.strict) {
824
- if (!this.events.includes(_event.name) && !utils.isBuiltInEvent(_event.name)) {
825
- throw new Error("Machine '".concat(this.id, "' does not accept event '").concat(_event.name, "'"));
826
- }
827
- }
828
-
829
- var stateTransition = this._transition(currentState.value, currentState, _event) || {
830
- transitions: [],
831
- configuration: [],
832
- exitSet: [],
833
- source: currentState,
834
- actions: []
835
- };
836
- var prevConfig = stateUtils.getConfiguration([], this.getStateNodes(currentState.value));
837
- var resolvedConfig = stateTransition.configuration.length ? stateUtils.getConfiguration(prevConfig, stateTransition.configuration) : prevConfig;
838
- stateTransition.configuration = _tslib.__spreadArray([], _tslib.__read(resolvedConfig), false);
839
- return this.resolveTransition(stateTransition, currentState, currentState.context, exec, _event);
840
- };
841
-
842
- StateNode.prototype.resolveRaisedTransition = function (state, _event, originalEvent, predictableExec) {
843
- var _a;
844
-
845
- var currentActions = state.actions;
846
- state = this.transition(state, _event, undefined, predictableExec); // Save original event to state
847
- // TODO: this should be the raised event! Delete in V5 (breaking)
848
-
849
- state._event = originalEvent;
850
- state.event = originalEvent.data;
851
-
852
- (_a = state.actions).unshift.apply(_a, _tslib.__spreadArray([], _tslib.__read(currentActions), false));
853
-
854
- return state;
855
- };
856
-
857
- StateNode.prototype.resolveTransition = function (stateTransition, currentState, context, predictableExec, _event) {
858
- var e_6, _a, e_7, _b;
859
-
860
- var _this = this;
861
-
862
- if (_event === void 0) {
863
- _event = actions.initEvent;
864
- }
865
-
866
- var configuration = stateTransition.configuration; // Transition will "apply" if:
867
- // - this is the initial state (there is no current state)
868
- // - OR there are transitions
869
-
870
- var willTransition = !currentState || stateTransition.transitions.length > 0;
871
- var resolvedConfiguration = willTransition ? stateTransition.configuration : currentState ? currentState.configuration : [];
872
- var isDone = stateUtils.isInFinalState(resolvedConfiguration, this);
873
- var resolvedStateValue = willTransition ? stateUtils.getValue(this.machine, configuration) : undefined;
874
- var historyValue = currentState ? currentState.historyValue ? currentState.historyValue : stateTransition.source ? this.machine.historyValue(currentState.value) : undefined : undefined;
875
- var actionBlocks = this.getActions(new Set(resolvedConfiguration), isDone, stateTransition, context, _event, currentState, predictableExec);
876
- var activities = currentState ? _tslib.__assign({}, currentState.activities) : {};
877
-
878
- try {
879
- for (var actionBlocks_1 = _tslib.__values(actionBlocks), actionBlocks_1_1 = actionBlocks_1.next(); !actionBlocks_1_1.done; actionBlocks_1_1 = actionBlocks_1.next()) {
880
- var block = actionBlocks_1_1.value;
881
-
882
- try {
883
- for (var _c = (e_7 = void 0, _tslib.__values(block.actions)), _d = _c.next(); !_d.done; _d = _c.next()) {
884
- var action = _d.value;
885
-
886
- if (action.type === actionTypes.start) {
887
- activities[action.activity.id || action.activity.type] = action;
888
- } else if (action.type === actionTypes.stop) {
889
- activities[action.activity.id || action.activity.type] = false;
890
- }
891
- }
892
- } catch (e_7_1) {
893
- e_7 = {
894
- error: e_7_1
895
- };
896
- } finally {
897
- try {
898
- if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
899
- } finally {
900
- if (e_7) throw e_7.error;
901
- }
902
- }
903
- }
904
- } catch (e_6_1) {
905
- e_6 = {
906
- error: e_6_1
907
- };
908
- } finally {
909
- try {
910
- if (actionBlocks_1_1 && !actionBlocks_1_1.done && (_a = actionBlocks_1.return)) _a.call(actionBlocks_1);
911
- } finally {
912
- if (e_6) throw e_6.error;
913
- }
914
- }
915
-
916
- var _e = _tslib.__read(actions.resolveActions(this, currentState, context, _event, actionBlocks, predictableExec, this.machine.config.predictableActionArguments || this.machine.config.preserveActionOrder), 2),
917
- resolvedActions = _e[0],
918
- updatedContext = _e[1];
919
-
920
- var _f = _tslib.__read(utils.partition(resolvedActions, function (action) {
921
- return action.type === actionTypes.raise || action.type === actionTypes.send && action.to === types.SpecialTargets.Internal;
922
- }), 2),
923
- raisedEvents = _f[0],
924
- nonRaisedActions = _f[1];
925
-
926
- var invokeActions = resolvedActions.filter(function (action) {
927
- var _a;
928
-
929
- return action.type === actionTypes.start && ((_a = action.activity) === null || _a === void 0 ? void 0 : _a.type) === actionTypes.invoke;
930
- });
931
- var children = invokeActions.reduce(function (acc, action) {
932
- acc[action.activity.id] = Actor.createInvocableActor(action.activity, _this.machine, updatedContext, _event);
933
- return acc;
934
- }, currentState ? _tslib.__assign({}, currentState.children) : {});
935
- var nextState = new State.State({
936
- value: resolvedStateValue || currentState.value,
937
- context: updatedContext,
938
- _event: _event,
939
- // Persist _sessionid between states
940
- _sessionid: currentState ? currentState._sessionid : null,
941
- historyValue: resolvedStateValue ? historyValue ? utils.updateHistoryValue(historyValue, resolvedStateValue) : undefined : currentState ? currentState.historyValue : undefined,
942
- history: !resolvedStateValue || stateTransition.source ? currentState : undefined,
943
- actions: resolvedStateValue ? nonRaisedActions : [],
944
- activities: resolvedStateValue ? activities : currentState ? currentState.activities : {},
945
- events: [],
946
- configuration: resolvedConfiguration,
947
- transitions: stateTransition.transitions,
948
- children: children,
949
- done: isDone,
950
- tags: stateUtils.getTagsFromConfiguration(resolvedConfiguration),
951
- machine: this
952
- });
953
- var didUpdateContext = context !== updatedContext;
954
- nextState.changed = _event.name === actionTypes.update || didUpdateContext; // Dispose of penultimate histories to prevent memory leaks
955
-
956
- var history = nextState.history;
957
-
958
- if (history) {
959
- delete history.history;
960
- } // There are transient transitions if the machine is not in a final state
961
- // and if some of the state nodes have transient ("always") transitions.
962
-
963
-
964
- var hasAlwaysTransitions = !isDone && (this._transient || configuration.some(function (stateNode) {
965
- return stateNode._transient;
966
- })); // If there are no enabled transitions, check if there are transient transitions.
967
- // If there are transient transitions, continue checking for more transitions
968
- // because an transient transition should be triggered even if there are no
969
- // enabled transitions.
970
- //
971
- // If we're already working on an transient transition then stop to prevent an infinite loop.
972
- //
973
- // Otherwise, if there are no enabled nor transient transitions, we are done.
974
-
975
- if (!willTransition && (!hasAlwaysTransitions || _event.name === NULL_EVENT)) {
976
- return nextState;
977
- }
978
-
979
- var maybeNextState = nextState;
980
-
981
- if (!isDone) {
982
- if (hasAlwaysTransitions) {
983
- maybeNextState = this.resolveRaisedTransition(maybeNextState, {
984
- type: actionTypes.nullEvent
985
- }, _event, predictableExec);
986
- }
987
-
988
- while (raisedEvents.length) {
989
- var raisedEvent = raisedEvents.shift();
990
- maybeNextState = this.resolveRaisedTransition(maybeNextState, raisedEvent._event, _event, predictableExec);
991
- }
992
- } // Detect if state changed
993
-
994
-
995
- var changed = maybeNextState.changed || (history ? !!maybeNextState.actions.length || didUpdateContext || typeof history.value !== typeof maybeNextState.value || !State.stateValuesEqual(maybeNextState.value, history.value) : undefined);
996
- maybeNextState.changed = changed; // Preserve original history after raised events
997
-
998
- maybeNextState.history = history;
999
- return maybeNextState;
1000
- };
1001
- /**
1002
- * Returns the child state node from its relative `stateKey`, or throws.
1003
- */
1004
-
1005
-
1006
- StateNode.prototype.getStateNode = function (stateKey) {
1007
- if (isStateId(stateKey)) {
1008
- return this.machine.getStateNodeById(stateKey);
1009
- }
1010
-
1011
- if (!this.states) {
1012
- throw new Error("Unable to retrieve child state '".concat(stateKey, "' from '").concat(this.id, "'; no child states exist."));
1013
- }
1014
-
1015
- var result = this.states[stateKey];
1016
-
1017
- if (!result) {
1018
- throw new Error("Child state '".concat(stateKey, "' does not exist on '").concat(this.id, "'"));
1019
- }
1020
-
1021
- return result;
1022
- };
1023
- /**
1024
- * Returns the state node with the given `stateId`, or throws.
1025
- *
1026
- * @param stateId The state ID. The prefix "#" is removed.
1027
- */
1028
-
1029
-
1030
- StateNode.prototype.getStateNodeById = function (stateId) {
1031
- var resolvedStateId = isStateId(stateId) ? stateId.slice(STATE_IDENTIFIER.length) : stateId;
1032
-
1033
- if (resolvedStateId === this.id) {
1034
- return this;
1035
- }
1036
-
1037
- var stateNode = this.machine.idMap[resolvedStateId];
1038
-
1039
- if (!stateNode) {
1040
- throw new Error("Child state node '#".concat(resolvedStateId, "' does not exist on machine '").concat(this.id, "'"));
1041
- }
1042
-
1043
- return stateNode;
1044
- };
1045
- /**
1046
- * Returns the relative state node from the given `statePath`, or throws.
1047
- *
1048
- * @param statePath The string or string array relative path to the state node.
1049
- */
1050
-
1051
-
1052
- StateNode.prototype.getStateNodeByPath = function (statePath) {
1053
- if (typeof statePath === 'string' && isStateId(statePath)) {
1054
- try {
1055
- return this.getStateNodeById(statePath.slice(1));
1056
- } catch (e) {// try individual paths
1057
- // throw e;
1058
- }
1059
- }
1060
-
1061
- var arrayStatePath = utils.toStatePath(statePath, this.delimiter).slice();
1062
- var currentStateNode = this;
1063
-
1064
- while (arrayStatePath.length) {
1065
- var key = arrayStatePath.shift();
1066
-
1067
- if (!key.length) {
1068
- break;
1069
- }
1070
-
1071
- currentStateNode = currentStateNode.getStateNode(key);
1072
- }
1073
-
1074
- return currentStateNode;
1075
- };
1076
- /**
1077
- * Resolves a partial state value with its full representation in this machine.
1078
- *
1079
- * @param stateValue The partial state value to resolve.
1080
- */
1081
-
1082
-
1083
- StateNode.prototype.resolve = function (stateValue) {
1084
- var _a;
1085
-
1086
- var _this = this;
1087
-
1088
- if (!stateValue) {
1089
- return this.initialStateValue || EMPTY_OBJECT; // TODO: type-specific properties
1090
- }
1091
-
1092
- switch (this.type) {
1093
- case 'parallel':
1094
- return utils.mapValues(this.initialStateValue, function (subStateValue, subStateKey) {
1095
- return subStateValue ? _this.getStateNode(subStateKey).resolve(stateValue[subStateKey] || subStateValue) : EMPTY_OBJECT;
1096
- });
1097
-
1098
- case 'compound':
1099
- if (utils.isString(stateValue)) {
1100
- var subStateNode = this.getStateNode(stateValue);
1101
-
1102
- if (subStateNode.type === 'parallel' || subStateNode.type === 'compound') {
1103
- return _a = {}, _a[stateValue] = subStateNode.initialStateValue, _a;
1104
- }
1105
-
1106
- return stateValue;
1107
- }
1108
-
1109
- if (!Object.keys(stateValue).length) {
1110
- return this.initialStateValue || {};
1111
- }
1112
-
1113
- return utils.mapValues(stateValue, function (subStateValue, subStateKey) {
1114
- return subStateValue ? _this.getStateNode(subStateKey).resolve(subStateValue) : EMPTY_OBJECT;
1115
- });
1116
-
1117
- default:
1118
- return stateValue || EMPTY_OBJECT;
1119
- }
1120
- };
1121
-
1122
- StateNode.prototype.getResolvedPath = function (stateIdentifier) {
1123
- if (isStateId(stateIdentifier)) {
1124
- var stateNode = this.machine.idMap[stateIdentifier.slice(STATE_IDENTIFIER.length)];
1125
-
1126
- if (!stateNode) {
1127
- throw new Error("Unable to find state node '".concat(stateIdentifier, "'"));
1128
- }
1129
-
1130
- return stateNode.path;
1131
- }
1132
-
1133
- return utils.toStatePath(stateIdentifier, this.delimiter);
1134
- };
1135
-
1136
- Object.defineProperty(StateNode.prototype, "initialStateValue", {
1137
- get: function () {
1138
- var _a;
1139
-
1140
- if (this.__cache.initialStateValue) {
1141
- return this.__cache.initialStateValue;
1142
- }
1143
-
1144
- var initialStateValue;
1145
-
1146
- if (this.type === 'parallel') {
1147
- initialStateValue = utils.mapFilterValues(this.states, function (state) {
1148
- return state.initialStateValue || EMPTY_OBJECT;
1149
- }, function (stateNode) {
1150
- return !(stateNode.type === 'history');
1151
- });
1152
- } else if (this.initial !== undefined) {
1153
- if (!this.states[this.initial]) {
1154
- throw new Error("Initial state '".concat(this.initial, "' not found on '").concat(this.key, "'"));
1155
- }
1156
-
1157
- initialStateValue = stateUtils.isLeafNode(this.states[this.initial]) ? this.initial : (_a = {}, _a[this.initial] = this.states[this.initial].initialStateValue, _a);
1158
- } else {
1159
- // The finite state value of a machine without child states is just an empty object
1160
- initialStateValue = {};
1161
- }
1162
-
1163
- this.__cache.initialStateValue = initialStateValue;
1164
- return this.__cache.initialStateValue;
1165
- },
1166
- enumerable: false,
1167
- configurable: true
1168
- });
1169
-
1170
- StateNode.prototype.getInitialState = function (stateValue, context) {
1171
- this._init(); // TODO: this should be in the constructor (see note in constructor)
1172
-
1173
-
1174
- var configuration = this.getStateNodes(stateValue);
1175
- return this.resolveTransition({
1176
- configuration: configuration,
1177
- exitSet: [],
1178
- transitions: [],
1179
- source: undefined,
1180
- actions: []
1181
- }, undefined, context !== null && context !== void 0 ? context : this.machine.context, undefined);
1182
- };
1183
-
1184
- Object.defineProperty(StateNode.prototype, "initialState", {
1185
- /**
1186
- * The initial State instance, which includes all actions to be executed from
1187
- * entering the initial state.
1188
- */
1189
- get: function () {
1190
- var initialStateValue = this.initialStateValue;
1191
-
1192
- if (!initialStateValue) {
1193
- throw new Error("Cannot retrieve initial state from simple state '".concat(this.id, "'."));
1194
- }
1195
-
1196
- return this.getInitialState(initialStateValue);
1197
- },
1198
- enumerable: false,
1199
- configurable: true
1200
- });
1201
- Object.defineProperty(StateNode.prototype, "target", {
1202
- /**
1203
- * The target state value of the history state node, if it exists. This represents the
1204
- * default state value to transition to if no history value exists yet.
1205
- */
1206
- get: function () {
1207
- var target;
1208
-
1209
- if (this.type === 'history') {
1210
- var historyConfig = this.config;
1211
-
1212
- if (utils.isString(historyConfig.target)) {
1213
- target = isStateId(historyConfig.target) ? utils.pathToStateValue(this.machine.getStateNodeById(historyConfig.target).path.slice(this.path.length - 1)) : historyConfig.target;
1214
- } else {
1215
- target = historyConfig.target;
1216
- }
1217
- }
1218
-
1219
- return target;
1220
- },
1221
- enumerable: false,
1222
- configurable: true
1223
- });
1224
- /**
1225
- * Returns the leaf nodes from a state path relative to this state node.
1226
- *
1227
- * @param relativeStateId The relative state path to retrieve the state nodes
1228
- * @param history The previous state to retrieve history
1229
- * @param resolve Whether state nodes should resolve to initial child state nodes
1230
- */
1231
-
1232
- StateNode.prototype.getRelativeStateNodes = function (relativeStateId, historyValue, resolve) {
1233
- if (resolve === void 0) {
1234
- resolve = true;
1235
- }
1236
-
1237
- return resolve ? relativeStateId.type === 'history' ? relativeStateId.resolveHistory(historyValue) : relativeStateId.initialStateNodes : [relativeStateId];
1238
- };
1239
-
1240
- Object.defineProperty(StateNode.prototype, "initialStateNodes", {
1241
- get: function () {
1242
- var _this = this;
1243
-
1244
- if (stateUtils.isLeafNode(this)) {
1245
- return [this];
1246
- } // Case when state node is compound but no initial state is defined
1247
-
1248
-
1249
- if (this.type === 'compound' && !this.initial) {
1250
- if (!environment.IS_PRODUCTION) {
1251
- utils.warn(false, "Compound state node '".concat(this.id, "' has no initial state."));
1252
- }
1253
-
1254
- return [this];
1255
- }
1256
-
1257
- var initialStateNodePaths = utils.toStatePaths(this.initialStateValue);
1258
- return utils.flatten(initialStateNodePaths.map(function (initialPath) {
1259
- return _this.getFromRelativePath(initialPath);
1260
- }));
1261
- },
1262
- enumerable: false,
1263
- configurable: true
1264
- });
1265
- /**
1266
- * Retrieves state nodes from a relative path to this state node.
1267
- *
1268
- * @param relativePath The relative path from this state node
1269
- * @param historyValue
1270
- */
1271
-
1272
- StateNode.prototype.getFromRelativePath = function (relativePath) {
1273
- if (!relativePath.length) {
1274
- return [this];
1275
- }
1276
-
1277
- var _a = _tslib.__read(relativePath),
1278
- stateKey = _a[0],
1279
- childStatePath = _a.slice(1);
1280
-
1281
- if (!this.states) {
1282
- throw new Error("Cannot retrieve subPath '".concat(stateKey, "' from node with no states"));
1283
- }
1284
-
1285
- var childStateNode = this.getStateNode(stateKey);
1286
-
1287
- if (childStateNode.type === 'history') {
1288
- return childStateNode.resolveHistory();
1289
- }
1290
-
1291
- if (!this.states[stateKey]) {
1292
- throw new Error("Child state '".concat(stateKey, "' does not exist on '").concat(this.id, "'"));
1293
- }
1294
-
1295
- return this.states[stateKey].getFromRelativePath(childStatePath);
1296
- };
1297
-
1298
- StateNode.prototype.historyValue = function (relativeStateValue) {
1299
- if (!Object.keys(this.states).length) {
1300
- return undefined;
1301
- }
1302
-
1303
- return {
1304
- current: relativeStateValue || this.initialStateValue,
1305
- states: utils.mapFilterValues(this.states, function (stateNode, key) {
1306
- if (!relativeStateValue) {
1307
- return stateNode.historyValue();
1308
- }
1309
-
1310
- var subStateValue = utils.isString(relativeStateValue) ? undefined : relativeStateValue[key];
1311
- return stateNode.historyValue(subStateValue || stateNode.initialStateValue);
1312
- }, function (stateNode) {
1313
- return !stateNode.history;
1314
- })
1315
- };
1316
- };
1317
- /**
1318
- * Resolves to the historical value(s) of the parent state node,
1319
- * represented by state nodes.
1320
- *
1321
- * @param historyValue
1322
- */
1323
-
1324
-
1325
- StateNode.prototype.resolveHistory = function (historyValue) {
1326
- var _this = this;
1327
-
1328
- if (this.type !== 'history') {
1329
- return [this];
1330
- }
1331
-
1332
- var parent = this.parent;
1333
-
1334
- if (!historyValue) {
1335
- var historyTarget = this.target;
1336
- return historyTarget ? utils.flatten(utils.toStatePaths(historyTarget).map(function (relativeChildPath) {
1337
- return parent.getFromRelativePath(relativeChildPath);
1338
- })) : parent.initialStateNodes;
1339
- }
1340
-
1341
- var subHistoryValue = utils.nestedPath(parent.path, 'states')(historyValue).current;
1342
-
1343
- if (utils.isString(subHistoryValue)) {
1344
- return [parent.getStateNode(subHistoryValue)];
1345
- }
1346
-
1347
- return utils.flatten(utils.toStatePaths(subHistoryValue).map(function (subStatePath) {
1348
- return _this.history === 'deep' ? parent.getFromRelativePath(subStatePath) : [parent.states[subStatePath[0]]];
1349
- }));
1350
- };
1351
-
1352
- Object.defineProperty(StateNode.prototype, "stateIds", {
1353
- /**
1354
- * All the state node IDs of this state node and its descendant state nodes.
1355
- */
1356
- get: function () {
1357
- var _this = this;
1358
-
1359
- var childStateIds = utils.flatten(Object.keys(this.states).map(function (stateKey) {
1360
- return _this.states[stateKey].stateIds;
1361
- }));
1362
- return [this.id].concat(childStateIds);
1363
- },
1364
- enumerable: false,
1365
- configurable: true
1366
- });
1367
- Object.defineProperty(StateNode.prototype, "events", {
1368
- /**
1369
- * All the event types accepted by this state node and its descendants.
1370
- */
1371
- get: function () {
1372
- var e_8, _a, e_9, _b;
1373
-
1374
- if (this.__cache.events) {
1375
- return this.__cache.events;
1376
- }
1377
-
1378
- var states = this.states;
1379
- var events = new Set(this.ownEvents);
1380
-
1381
- if (states) {
1382
- try {
1383
- for (var _c = _tslib.__values(Object.keys(states)), _d = _c.next(); !_d.done; _d = _c.next()) {
1384
- var stateId = _d.value;
1385
- var state = states[stateId];
1386
-
1387
- if (state.states) {
1388
- try {
1389
- for (var _e = (e_9 = void 0, _tslib.__values(state.events)), _f = _e.next(); !_f.done; _f = _e.next()) {
1390
- var event_1 = _f.value;
1391
- events.add("".concat(event_1));
1392
- }
1393
- } catch (e_9_1) {
1394
- e_9 = {
1395
- error: e_9_1
1396
- };
1397
- } finally {
1398
- try {
1399
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1400
- } finally {
1401
- if (e_9) throw e_9.error;
1402
- }
1403
- }
1404
- }
1405
- }
1406
- } catch (e_8_1) {
1407
- e_8 = {
1408
- error: e_8_1
1409
- };
1410
- } finally {
1411
- try {
1412
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1413
- } finally {
1414
- if (e_8) throw e_8.error;
1415
- }
1416
- }
1417
- }
1418
-
1419
- return this.__cache.events = Array.from(events);
1420
- },
1421
- enumerable: false,
1422
- configurable: true
1423
- });
1424
- Object.defineProperty(StateNode.prototype, "ownEvents", {
1425
- /**
1426
- * All the events that have transitions directly from this state node.
1427
- *
1428
- * Excludes any inert events.
1429
- */
1430
- get: function () {
1431
- var events = new Set(this.transitions.filter(function (transition) {
1432
- return !(!transition.target && !transition.actions.length && transition.internal);
1433
- }).map(function (transition) {
1434
- return transition.eventType;
1435
- }));
1436
- return Array.from(events);
1437
- },
1438
- enumerable: false,
1439
- configurable: true
1440
- });
1441
-
1442
- StateNode.prototype.resolveTarget = function (_target) {
1443
- var _this = this;
1444
-
1445
- if (_target === undefined) {
1446
- // an undefined target signals that the state node should not transition from that state when receiving that event
1447
- return undefined;
1448
- }
1449
-
1450
- return _target.map(function (target) {
1451
- if (!utils.isString(target)) {
1452
- return target;
1453
- }
1454
-
1455
- var isInternalTarget = target[0] === _this.delimiter; // If internal target is defined on machine,
1456
- // do not include machine key on target
1457
-
1458
- if (isInternalTarget && !_this.parent) {
1459
- return _this.getStateNodeByPath(target.slice(1));
1460
- }
1461
-
1462
- var resolvedTarget = isInternalTarget ? _this.key + target : target;
1463
-
1464
- if (_this.parent) {
1465
- try {
1466
- var targetStateNode = _this.parent.getStateNodeByPath(resolvedTarget);
1467
-
1468
- return targetStateNode;
1469
- } catch (err) {
1470
- throw new Error("Invalid transition definition for state node '".concat(_this.id, "':\n").concat(err.message));
1471
- }
1472
- } else {
1473
- return _this.getStateNodeByPath(resolvedTarget);
1474
- }
1475
- });
1476
- };
1477
-
1478
- StateNode.prototype.formatTransition = function (transitionConfig) {
1479
- var _this = this;
1480
-
1481
- var normalizedTarget = utils.normalizeTarget(transitionConfig.target);
1482
- var internal = 'internal' in transitionConfig ? transitionConfig.internal : normalizedTarget ? normalizedTarget.some(function (_target) {
1483
- return utils.isString(_target) && _target[0] === _this.delimiter;
1484
- }) : true;
1485
- var guards = this.machine.options.guards;
1486
- var target = this.resolveTarget(normalizedTarget);
1487
-
1488
- var transition = _tslib.__assign(_tslib.__assign({}, transitionConfig), {
1489
- actions: actions.toActionObjects(utils.toArray(transitionConfig.actions)),
1490
- cond: utils.toGuard(transitionConfig.cond, guards),
1491
- target: target,
1492
- source: this,
1493
- internal: internal,
1494
- eventType: transitionConfig.event,
1495
- toJSON: function () {
1496
- return _tslib.__assign(_tslib.__assign({}, transition), {
1497
- target: transition.target ? transition.target.map(function (t) {
1498
- return "#".concat(t.id);
1499
- }) : undefined,
1500
- source: "#".concat(_this.id)
1501
- });
1502
- }
1503
- });
1504
-
1505
- return transition;
1506
- };
1507
-
1508
- StateNode.prototype.formatTransitions = function () {
1509
- var e_10, _a;
1510
-
1511
- var _this = this;
1512
-
1513
- var onConfig;
1514
-
1515
- if (!this.config.on) {
1516
- onConfig = [];
1517
- } else if (Array.isArray(this.config.on)) {
1518
- onConfig = this.config.on;
1519
- } else {
1520
- var _b = this.config.on,
1521
- _c = WILDCARD,
1522
- _d = _b[_c],
1523
- wildcardConfigs = _d === void 0 ? [] : _d,
1524
- strictTransitionConfigs_1 = _tslib.__rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
1525
-
1526
- onConfig = utils.flatten(Object.keys(strictTransitionConfigs_1).map(function (key) {
1527
- if (!environment.IS_PRODUCTION && key === NULL_EVENT) {
1528
- 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, "\"."));
1529
- }
1530
-
1531
- var transitionConfigArray = utils.toTransitionConfigArray(key, strictTransitionConfigs_1[key]);
1532
-
1533
- if (!environment.IS_PRODUCTION) {
1534
- validateArrayifiedTransitions(_this, key, transitionConfigArray);
1535
- }
1536
-
1537
- return transitionConfigArray;
1538
- }).concat(utils.toTransitionConfigArray(WILDCARD, wildcardConfigs)));
1539
- }
1540
-
1541
- var eventlessConfig = this.config.always ? utils.toTransitionConfigArray('', this.config.always) : [];
1542
- var doneConfig = this.config.onDone ? utils.toTransitionConfigArray(String(actions.done(this.id)), this.config.onDone) : [];
1543
-
1544
- if (!environment.IS_PRODUCTION) {
1545
- utils.warn(!(this.config.onDone && !this.parent), "Root nodes cannot have an \".onDone\" transition. Please check the config of \"".concat(this.id, "\"."));
1546
- }
1547
-
1548
- var invokeConfig = utils.flatten(this.invoke.map(function (invokeDef) {
1549
- var settleTransitions = [];
1550
-
1551
- if (invokeDef.onDone) {
1552
- settleTransitions.push.apply(settleTransitions, _tslib.__spreadArray([], _tslib.__read(utils.toTransitionConfigArray(String(actions.doneInvoke(invokeDef.id)), invokeDef.onDone)), false));
1553
- }
1554
-
1555
- if (invokeDef.onError) {
1556
- settleTransitions.push.apply(settleTransitions, _tslib.__spreadArray([], _tslib.__read(utils.toTransitionConfigArray(String(actions.error(invokeDef.id)), invokeDef.onError)), false));
1557
- }
1558
-
1559
- return settleTransitions;
1560
- }));
1561
- var delayedTransitions = this.after;
1562
- 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) {
1563
- return utils.toArray(transitionConfig).map(function (transition) {
1564
- return _this.formatTransition(transition);
1565
- });
1566
- }));
1567
-
1568
- try {
1569
- for (var delayedTransitions_1 = _tslib.__values(delayedTransitions), delayedTransitions_1_1 = delayedTransitions_1.next(); !delayedTransitions_1_1.done; delayedTransitions_1_1 = delayedTransitions_1.next()) {
1570
- var delayedTransition = delayedTransitions_1_1.value;
1571
- formattedTransitions.push(delayedTransition);
1572
- }
1573
- } catch (e_10_1) {
1574
- e_10 = {
1575
- error: e_10_1
1576
- };
1577
- } finally {
1578
- try {
1579
- if (delayedTransitions_1_1 && !delayedTransitions_1_1.done && (_a = delayedTransitions_1.return)) _a.call(delayedTransitions_1);
1580
- } finally {
1581
- if (e_10) throw e_10.error;
1582
- }
1583
- }
1584
-
1585
- return formattedTransitions;
1586
- };
1587
-
1588
- return StateNode;
1589
- }();
1590
-
1591
- exports.StateNode = StateNode;