xstate 4.31.0 → 5.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/CHANGELOG.md +526 -0
  2. package/actions/ExecutableAction.ts +58 -0
  3. package/actions/dist/xstate-actions.cjs.d.ts +1 -0
  4. package/actions/dist/xstate-actions.cjs.dev.js +32 -0
  5. package/actions/dist/xstate-actions.cjs.js +7 -0
  6. package/actions/dist/xstate-actions.cjs.prod.js +32 -0
  7. package/actions/dist/xstate-actions.esm.js +2 -0
  8. package/actions/dynamicAction.ts +34 -0
  9. package/actions/package.json +4 -0
  10. package/actors/dist/xstate-actors.cjs.d.ts +1 -0
  11. package/actors/dist/xstate-actors.cjs.dev.js +21 -0
  12. package/actors/dist/xstate-actors.cjs.js +7 -0
  13. package/actors/dist/xstate-actors.cjs.prod.js +21 -0
  14. package/actors/dist/xstate-actors.esm.js +2 -0
  15. package/actors/package.json +4 -0
  16. package/dev/dist/xstate-dev.cjs.d.ts +1 -0
  17. package/dev/dist/xstate-dev.cjs.dev.js +11 -0
  18. package/dev/dist/xstate-dev.cjs.js +7 -0
  19. package/{lib/devTools.js → dev/dist/xstate-dev.cjs.prod.js} +17 -10
  20. package/dev/dist/xstate-dev.esm.js +1 -0
  21. package/dev/package.json +4 -0
  22. package/dist/Machine-9ce74c11.cjs.dev.js +755 -0
  23. package/dist/Machine-dca041dc.cjs.prod.js +749 -0
  24. package/dist/Machine-fb2f47f5.esm.js +751 -0
  25. package/dist/actions-6b86876d.cjs.dev.js +4815 -0
  26. package/dist/actions-70094c93.cjs.prod.js +4765 -0
  27. package/dist/actions-ff512f16.esm.js +4757 -0
  28. package/dist/declarations/actions/ExecutableAction.d.ts +23 -0
  29. package/dist/declarations/actions/dynamicAction.d.ts +3 -0
  30. package/dist/declarations/src/Machine.d.ts +4 -0
  31. package/dist/declarations/src/Mailbox.d.ts +12 -0
  32. package/dist/declarations/src/ObservableActorRef.d.ts +25 -0
  33. package/{lib → dist/declarations/src}/SimulatedClock.d.ts +0 -1
  34. package/dist/declarations/src/State.d.ts +104 -0
  35. package/dist/declarations/src/StateMachine.d.ts +112 -0
  36. package/dist/declarations/src/StateNode.d.ts +127 -0
  37. package/{es → dist/declarations/src}/actionTypes.d.ts +0 -3
  38. package/dist/declarations/src/actions/assign.d.ts +7 -0
  39. package/dist/declarations/src/actions/cancel.d.ts +10 -0
  40. package/dist/declarations/src/actions/choose.d.ts +3 -0
  41. package/dist/declarations/src/actions/invoke.d.ts +3 -0
  42. package/dist/declarations/src/actions/log.d.ts +11 -0
  43. package/dist/declarations/src/actions/pure.d.ts +3 -0
  44. package/dist/declarations/src/actions/raise.d.ts +9 -0
  45. package/dist/declarations/src/actions/send.d.ts +55 -0
  46. package/dist/declarations/src/actions/stop.d.ts +8 -0
  47. package/dist/declarations/src/actions.d.ts +44 -0
  48. package/dist/declarations/src/actors.d.ts +38 -0
  49. package/dist/declarations/src/constants.d.ts +5 -0
  50. package/{lib/devTools.d.ts → dist/declarations/src/dev/index.d.ts} +2 -2
  51. package/{es → dist/declarations/src}/environment.d.ts +0 -1
  52. package/dist/declarations/src/guards.d.ts +9 -0
  53. package/dist/declarations/src/index.d.ts +47 -0
  54. package/dist/declarations/src/interpreter.d.ts +147 -0
  55. package/{es → dist/declarations/src}/mapState.d.ts +0 -1
  56. package/dist/declarations/src/match.d.ts +5 -0
  57. package/dist/declarations/src/memo.d.ts +1 -0
  58. package/dist/declarations/src/model.d.ts +5 -0
  59. package/{lib → dist/declarations/src}/model.types.d.ts +10 -9
  60. package/dist/declarations/src/registry.d.ts +8 -0
  61. package/{es → dist/declarations/src}/schema.d.ts +0 -1
  62. package/dist/declarations/src/spawn.d.ts +2 -0
  63. package/dist/declarations/src/stateUtils.d.ts +85 -0
  64. package/{lib → dist/declarations/src}/typegenTypes.d.ts +28 -26
  65. package/dist/declarations/src/types.d.ts +1026 -0
  66. package/dist/declarations/src/utils.d.ts +56 -0
  67. package/{es/devTools.js → dist/index-20b364c4.esm.js} +18 -6
  68. package/dist/index-2a564d03.cjs.dev.js +64 -0
  69. package/dist/xstate.cjs.d.ts +1 -0
  70. package/dist/xstate.cjs.dev.js +217 -0
  71. package/dist/xstate.cjs.js +7 -0
  72. package/dist/xstate.cjs.prod.js +217 -0
  73. package/dist/xstate.esm.js +172 -0
  74. package/guards/dist/xstate-guards.cjs.d.ts +1 -0
  75. package/guards/dist/xstate-guards.cjs.dev.js +15 -0
  76. package/guards/dist/xstate-guards.cjs.js +7 -0
  77. package/guards/dist/xstate-guards.cjs.prod.js +15 -0
  78. package/guards/dist/xstate-guards.esm.js +2 -0
  79. package/guards/package.json +4 -0
  80. package/invoke/package.json +4 -0
  81. package/model/dist/xstate-model.cjs.d.ts +1 -0
  82. package/model/dist/xstate-model.cjs.dev.js +41 -0
  83. package/model/dist/xstate-model.cjs.js +7 -0
  84. package/model/dist/xstate-model.cjs.prod.js +41 -0
  85. package/model/dist/xstate-model.esm.js +37 -0
  86. package/model/package.json +4 -0
  87. package/package.json +21 -30
  88. package/dist/xstate.interpreter.js +0 -15
  89. package/dist/xstate.js +0 -15
  90. package/dist/xstate.web.js +0 -15
  91. package/es/Actor.d.ts +0 -25
  92. package/es/Actor.js +0 -99
  93. package/es/Machine.d.ts +0 -12
  94. package/es/Machine.js +0 -14
  95. package/es/SimulatedClock.d.ts +0 -17
  96. package/es/SimulatedClock.js +0 -81
  97. package/es/State.d.ts +0 -123
  98. package/es/State.js +0 -274
  99. package/es/StateNode.d.ts +0 -287
  100. package/es/StateNode.js +0 -1566
  101. package/es/_virtual/_tslib.js +0 -78
  102. package/es/actionTypes.js +0 -22
  103. package/es/actions.d.ts +0 -154
  104. package/es/actions.js +0 -520
  105. package/es/behaviors.d.ts +0 -37
  106. package/es/behaviors.js +0 -131
  107. package/es/constants.d.ts +0 -6
  108. package/es/constants.js +0 -6
  109. package/es/devTools.d.ts +0 -14
  110. package/es/each.d.ts +0 -4
  111. package/es/each.js +0 -13
  112. package/es/environment.js +0 -3
  113. package/es/index.d.ts +0 -22
  114. package/es/index.js +0 -23
  115. package/es/interpreter.d.ts +0 -209
  116. package/es/interpreter.js +0 -1402
  117. package/es/invoke.d.ts +0 -11
  118. package/es/invokeUtils.d.ts +0 -7
  119. package/es/invokeUtils.js +0 -40
  120. package/es/json.d.ts +0 -31
  121. package/es/json.js +0 -86
  122. package/es/mapState.js +0 -32
  123. package/es/match.d.ts +0 -9
  124. package/es/match.js +0 -34
  125. package/es/model.d.ts +0 -5
  126. package/es/model.js +0 -50
  127. package/es/model.types.d.ts +0 -52
  128. package/es/patterns.d.ts +0 -14
  129. package/es/patterns.js +0 -48
  130. package/es/registry.d.ts +0 -9
  131. package/es/registry.js +0 -19
  132. package/es/scheduler.d.ts +0 -17
  133. package/es/scheduler.js +0 -79
  134. package/es/schema.js +0 -6
  135. package/es/scxml.d.ts +0 -6
  136. package/es/serviceScope.d.ts +0 -4
  137. package/es/serviceScope.js +0 -16
  138. package/es/stateUtils.d.ts +0 -17
  139. package/es/stateUtils.js +0 -259
  140. package/es/typegenTypes.d.ts +0 -130
  141. package/es/types.d.ts +0 -1037
  142. package/es/types.js +0 -33
  143. package/es/utils.d.ts +0 -72
  144. package/es/utils.js +0 -626
  145. package/es/waitFor.d.ts +0 -32
  146. package/es/waitFor.js +0 -66
  147. package/lib/Actor.d.ts +0 -25
  148. package/lib/Actor.js +0 -108
  149. package/lib/Machine.d.ts +0 -12
  150. package/lib/Machine.js +0 -19
  151. package/lib/SimulatedClock.js +0 -85
  152. package/lib/State.d.ts +0 -123
  153. package/lib/State.js +0 -282
  154. package/lib/StateNode.d.ts +0 -287
  155. package/lib/StateNode.js +0 -1570
  156. package/lib/_virtual/_tslib.js +0 -85
  157. package/lib/actionTypes.d.ts +0 -20
  158. package/lib/actionTypes.js +0 -43
  159. package/lib/actions.d.ts +0 -154
  160. package/lib/actions.js +0 -552
  161. package/lib/behaviors.d.ts +0 -37
  162. package/lib/behaviors.js +0 -137
  163. package/lib/constants.d.ts +0 -6
  164. package/lib/constants.js +0 -13
  165. package/lib/each.d.ts +0 -4
  166. package/lib/each.js +0 -17
  167. package/lib/environment.d.ts +0 -2
  168. package/lib/environment.js +0 -7
  169. package/lib/index.d.ts +0 -22
  170. package/lib/index.js +0 -60
  171. package/lib/interpreter.d.ts +0 -209
  172. package/lib/interpreter.js +0 -1408
  173. package/lib/invoke.d.ts +0 -11
  174. package/lib/invoke.js +0 -20
  175. package/lib/invokeUtils.d.ts +0 -7
  176. package/lib/invokeUtils.js +0 -45
  177. package/lib/json.d.ts +0 -31
  178. package/lib/json.js +0 -94
  179. package/lib/mapState.d.ts +0 -4
  180. package/lib/mapState.js +0 -36
  181. package/lib/match.d.ts +0 -9
  182. package/lib/match.js +0 -38
  183. package/lib/model.d.ts +0 -5
  184. package/lib/model.js +0 -54
  185. package/lib/model.types.js +0 -2
  186. package/lib/patterns.d.ts +0 -14
  187. package/lib/patterns.js +0 -53
  188. package/lib/registry.d.ts +0 -9
  189. package/lib/registry.js +0 -23
  190. package/lib/scheduler.d.ts +0 -17
  191. package/lib/scheduler.js +0 -83
  192. package/lib/schema.d.ts +0 -3
  193. package/lib/schema.js +0 -11
  194. package/lib/scxml.d.ts +0 -6
  195. package/lib/scxml.js +0 -350
  196. package/lib/serviceScope.d.ts +0 -4
  197. package/lib/serviceScope.js +0 -21
  198. package/lib/stateUtils.d.ts +0 -17
  199. package/lib/stateUtils.js +0 -273
  200. package/lib/typegenTypes.js +0 -2
  201. package/lib/types.d.ts +0 -1037
  202. package/lib/types.js +0 -35
  203. package/lib/utils.d.ts +0 -72
  204. package/lib/utils.js +0 -673
  205. package/lib/waitFor.d.ts +0 -32
  206. package/lib/waitFor.js +0 -70
@@ -1,1408 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _tslib = require('./_virtual/_tslib.js');
6
- var types = require('./types.js');
7
- var State = require('./State.js');
8
- var actionTypes = require('./actionTypes.js');
9
- var actions = require('./actions.js');
10
- var environment = require('./environment.js');
11
- var utils = require('./utils.js');
12
- var scheduler = require('./scheduler.js');
13
- var Actor = require('./Actor.js');
14
- var stateUtils = require('./stateUtils.js');
15
- var registry = require('./registry.js');
16
- var devTools = require('./devTools.js');
17
- var serviceScope = require('./serviceScope.js');
18
- var behaviors = require('./behaviors.js');
19
-
20
- var DEFAULT_SPAWN_OPTIONS = {
21
- sync: false,
22
- autoForward: false
23
- };
24
- exports.InterpreterStatus = void 0;
25
-
26
- (function (InterpreterStatus) {
27
- InterpreterStatus[InterpreterStatus["NotStarted"] = 0] = "NotStarted";
28
- InterpreterStatus[InterpreterStatus["Running"] = 1] = "Running";
29
- InterpreterStatus[InterpreterStatus["Stopped"] = 2] = "Stopped";
30
- })(exports.InterpreterStatus || (exports.InterpreterStatus = {}));
31
-
32
- var Interpreter =
33
- /*#__PURE__*/
34
-
35
- /** @class */
36
- function () {
37
- /**
38
- * Creates a new Interpreter instance (i.e., service) for the given machine with the provided options, if any.
39
- *
40
- * @param machine The machine to be interpreted
41
- * @param options Interpreter options
42
- */
43
- function Interpreter(machine, options) {
44
- var _this = this;
45
-
46
- if (options === void 0) {
47
- options = Interpreter.defaultOptions;
48
- }
49
-
50
- this.machine = machine;
51
- this.delayedEventsMap = {};
52
- this.listeners = new Set();
53
- this.contextListeners = new Set();
54
- this.stopListeners = new Set();
55
- this.doneListeners = new Set();
56
- this.eventListeners = new Set();
57
- this.sendListeners = new Set();
58
- /**
59
- * Whether the service is started.
60
- */
61
-
62
- this.initialized = false;
63
- this.status = exports.InterpreterStatus.NotStarted;
64
- this.children = new Map();
65
- this.forwardTo = new Set();
66
- /**
67
- * Alias for Interpreter.prototype.start
68
- */
69
-
70
- this.init = this.start;
71
- /**
72
- * Sends an event to the running interpreter to trigger a transition.
73
- *
74
- * An array of events (batched) can be sent as well, which will send all
75
- * batched events to the running interpreter. The listeners will be
76
- * notified only **once** when all events are processed.
77
- *
78
- * @param event The event(s) to send
79
- */
80
-
81
- this.send = function (event, payload) {
82
- if (utils.isArray(event)) {
83
- _this.batch(event);
84
-
85
- return _this.state;
86
- }
87
-
88
- var _event = utils.toSCXMLEvent(utils.toEventObject(event, payload));
89
-
90
- if (_this.status === exports.InterpreterStatus.Stopped) {
91
- // do nothing
92
- if (!environment.IS_PRODUCTION) {
93
- utils.warn(false, "Event \"".concat(_event.name, "\" was sent to stopped service \"").concat(_this.machine.id, "\". This service has already reached its final state, and will not transition.\nEvent: ").concat(JSON.stringify(_event.data)));
94
- }
95
-
96
- return _this.state;
97
- }
98
-
99
- if (_this.status !== exports.InterpreterStatus.Running && !_this.options.deferEvents) {
100
- throw new Error("Event \"".concat(_event.name, "\" was sent to uninitialized service \"").concat(_this.machine.id // tslint:disable-next-line:max-line-length
101
- , "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: ").concat(JSON.stringify(_event.data)));
102
- }
103
-
104
- _this.scheduler.schedule(function () {
105
- // Forward copy of event to child actors
106
- _this.forward(_event);
107
-
108
- var nextState = _this.nextState(_event);
109
-
110
- _this.update(nextState, _event);
111
- });
112
-
113
- return _this._state; // TODO: deprecate (should return void)
114
- // tslint:disable-next-line:semicolon
115
- };
116
-
117
- this.sendTo = function (event, to) {
118
- var isParent = _this.parent && (to === types.SpecialTargets.Parent || _this.parent.id === to);
119
- var target = isParent ? _this.parent : utils.isString(to) ? _this.children.get(to) || registry.registry.get(to) : utils.isActor(to) ? to : undefined;
120
-
121
- if (!target) {
122
- if (!isParent) {
123
- throw new Error("Unable to send event to child '".concat(to, "' from service '").concat(_this.id, "'."));
124
- } // tslint:disable-next-line:no-console
125
-
126
-
127
- if (!environment.IS_PRODUCTION) {
128
- utils.warn(false, "Service '".concat(_this.id, "' has no parent: unable to send event ").concat(event.type));
129
- }
130
-
131
- return;
132
- }
133
-
134
- if ('machine' in target) {
135
- // Send SCXML events to machines
136
- target.send(_tslib.__assign(_tslib.__assign({}, event), {
137
- name: event.name === actionTypes.error ? "".concat(actions.error(_this.id)) : event.name,
138
- origin: _this.sessionId
139
- }));
140
- } else {
141
- // Send normal events to other targets
142
- target.send(event.data);
143
- }
144
- };
145
-
146
- var resolvedOptions = _tslib.__assign(_tslib.__assign({}, Interpreter.defaultOptions), options);
147
-
148
- var clock = resolvedOptions.clock,
149
- logger = resolvedOptions.logger,
150
- parent = resolvedOptions.parent,
151
- id = resolvedOptions.id;
152
- var resolvedId = id !== undefined ? id : machine.id;
153
- this.id = resolvedId;
154
- this.logger = logger;
155
- this.clock = clock;
156
- this.parent = parent;
157
- this.options = resolvedOptions;
158
- this.scheduler = new scheduler.Scheduler({
159
- deferEvents: this.options.deferEvents
160
- });
161
- this.sessionId = registry.registry.bookId();
162
- }
163
-
164
- Object.defineProperty(Interpreter.prototype, "initialState", {
165
- get: function () {
166
- var _this = this;
167
-
168
- if (this._initialState) {
169
- return this._initialState;
170
- }
171
-
172
- return serviceScope.provide(this, function () {
173
- _this._initialState = _this.machine.initialState;
174
- return _this._initialState;
175
- });
176
- },
177
- enumerable: false,
178
- configurable: true
179
- });
180
- Object.defineProperty(Interpreter.prototype, "state", {
181
- get: function () {
182
- if (!environment.IS_PRODUCTION) {
183
- utils.warn(this.status !== exports.InterpreterStatus.NotStarted, "Attempted to read state from uninitialized service '".concat(this.id, "'. Make sure the service is started first."));
184
- }
185
-
186
- return this._state;
187
- },
188
- enumerable: false,
189
- configurable: true
190
- });
191
- /**
192
- * Executes the actions of the given state, with that state's `context` and `event`.
193
- *
194
- * @param state The state whose actions will be executed
195
- * @param actionsConfig The action implementations to use
196
- */
197
-
198
- Interpreter.prototype.execute = function (state, actionsConfig) {
199
- var e_1, _a;
200
-
201
- try {
202
- for (var _b = _tslib.__values(state.actions), _c = _b.next(); !_c.done; _c = _b.next()) {
203
- var action = _c.value;
204
- this.exec(action, state, actionsConfig);
205
- }
206
- } catch (e_1_1) {
207
- e_1 = {
208
- error: e_1_1
209
- };
210
- } finally {
211
- try {
212
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
213
- } finally {
214
- if (e_1) throw e_1.error;
215
- }
216
- }
217
- };
218
-
219
- Interpreter.prototype.update = function (state, _event) {
220
- var e_2, _a, e_3, _b, e_4, _c, e_5, _d;
221
-
222
- var _this = this; // Attach session ID to state
223
-
224
-
225
- state._sessionid = this.sessionId; // Update state
226
-
227
- this._state = state; // Execute actions
228
-
229
- if (this.options.execute) {
230
- this.execute(this.state);
231
- } // Update children
232
-
233
-
234
- this.children.forEach(function (child) {
235
- _this.state.children[child.id] = child;
236
- }); // Dev tools
237
-
238
- if (this.devTools) {
239
- this.devTools.send(_event.data, state);
240
- } // Execute listeners
241
-
242
-
243
- if (state.event) {
244
- try {
245
- for (var _e = _tslib.__values(this.eventListeners), _f = _e.next(); !_f.done; _f = _e.next()) {
246
- var listener = _f.value;
247
- listener(state.event);
248
- }
249
- } catch (e_2_1) {
250
- e_2 = {
251
- error: e_2_1
252
- };
253
- } finally {
254
- try {
255
- if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
256
- } finally {
257
- if (e_2) throw e_2.error;
258
- }
259
- }
260
- }
261
-
262
- try {
263
- for (var _g = _tslib.__values(this.listeners), _h = _g.next(); !_h.done; _h = _g.next()) {
264
- var listener = _h.value;
265
- listener(state, state.event);
266
- }
267
- } catch (e_3_1) {
268
- e_3 = {
269
- error: e_3_1
270
- };
271
- } finally {
272
- try {
273
- if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
274
- } finally {
275
- if (e_3) throw e_3.error;
276
- }
277
- }
278
-
279
- try {
280
- for (var _j = _tslib.__values(this.contextListeners), _k = _j.next(); !_k.done; _k = _j.next()) {
281
- var contextListener = _k.value;
282
- contextListener(this.state.context, this.state.history ? this.state.history.context : undefined);
283
- }
284
- } catch (e_4_1) {
285
- e_4 = {
286
- error: e_4_1
287
- };
288
- } finally {
289
- try {
290
- if (_k && !_k.done && (_c = _j.return)) _c.call(_j);
291
- } finally {
292
- if (e_4) throw e_4.error;
293
- }
294
- }
295
-
296
- var isDone = stateUtils.isInFinalState(state.configuration || [], this.machine);
297
-
298
- if (this.state.configuration && isDone) {
299
- // get final child state node
300
- var finalChildStateNode = state.configuration.find(function (sn) {
301
- return sn.type === 'final' && sn.parent === _this.machine;
302
- });
303
- var doneData = finalChildStateNode && finalChildStateNode.doneData ? utils.mapContext(finalChildStateNode.doneData, state.context, _event) : undefined;
304
-
305
- try {
306
- for (var _l = _tslib.__values(this.doneListeners), _m = _l.next(); !_m.done; _m = _l.next()) {
307
- var listener = _m.value;
308
- listener(actions.doneInvoke(this.id, doneData));
309
- }
310
- } catch (e_5_1) {
311
- e_5 = {
312
- error: e_5_1
313
- };
314
- } finally {
315
- try {
316
- if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
317
- } finally {
318
- if (e_5) throw e_5.error;
319
- }
320
- }
321
-
322
- this.stop();
323
- }
324
- };
325
- /*
326
- * Adds a listener that is notified whenever a state transition happens. The listener is called with
327
- * the next state and the event object that caused the state transition.
328
- *
329
- * @param listener The state listener
330
- */
331
-
332
-
333
- Interpreter.prototype.onTransition = function (listener) {
334
- this.listeners.add(listener); // Send current state to listener
335
-
336
- if (this.status === exports.InterpreterStatus.Running) {
337
- listener(this.state, this.state.event);
338
- }
339
-
340
- return this;
341
- };
342
-
343
- Interpreter.prototype.subscribe = function (nextListenerOrObserver, _, // TODO: error listener
344
- completeListener) {
345
- var _this = this;
346
-
347
- if (!nextListenerOrObserver) {
348
- return {
349
- unsubscribe: function () {
350
- return void 0;
351
- }
352
- };
353
- }
354
-
355
- var listener;
356
- var resolvedCompleteListener = completeListener;
357
-
358
- if (typeof nextListenerOrObserver === 'function') {
359
- listener = nextListenerOrObserver;
360
- } else {
361
- listener = nextListenerOrObserver.next.bind(nextListenerOrObserver);
362
- resolvedCompleteListener = nextListenerOrObserver.complete.bind(nextListenerOrObserver);
363
- }
364
-
365
- this.listeners.add(listener); // Send current state to listener
366
-
367
- if (this.status !== exports.InterpreterStatus.NotStarted) {
368
- listener(this.state);
369
- }
370
-
371
- if (resolvedCompleteListener) {
372
- if (this.status === exports.InterpreterStatus.Stopped) {
373
- resolvedCompleteListener();
374
- } else {
375
- this.onDone(resolvedCompleteListener);
376
- }
377
- }
378
-
379
- return {
380
- unsubscribe: function () {
381
- listener && _this.listeners.delete(listener);
382
- resolvedCompleteListener && _this.doneListeners.delete(resolvedCompleteListener);
383
- }
384
- };
385
- };
386
- /**
387
- * Adds an event listener that is notified whenever an event is sent to the running interpreter.
388
- * @param listener The event listener
389
- */
390
-
391
-
392
- Interpreter.prototype.onEvent = function (listener) {
393
- this.eventListeners.add(listener);
394
- return this;
395
- };
396
- /**
397
- * Adds an event listener that is notified whenever a `send` event occurs.
398
- * @param listener The event listener
399
- */
400
-
401
-
402
- Interpreter.prototype.onSend = function (listener) {
403
- this.sendListeners.add(listener);
404
- return this;
405
- };
406
- /**
407
- * Adds a context listener that is notified whenever the state context changes.
408
- * @param listener The context listener
409
- */
410
-
411
-
412
- Interpreter.prototype.onChange = function (listener) {
413
- this.contextListeners.add(listener);
414
- return this;
415
- };
416
- /**
417
- * Adds a listener that is notified when the machine is stopped.
418
- * @param listener The listener
419
- */
420
-
421
-
422
- Interpreter.prototype.onStop = function (listener) {
423
- this.stopListeners.add(listener);
424
- return this;
425
- };
426
- /**
427
- * Adds a state listener that is notified when the statechart has reached its final state.
428
- * @param listener The state listener
429
- */
430
-
431
-
432
- Interpreter.prototype.onDone = function (listener) {
433
- this.doneListeners.add(listener);
434
- return this;
435
- };
436
- /**
437
- * Removes a listener.
438
- * @param listener The listener to remove
439
- */
440
-
441
-
442
- Interpreter.prototype.off = function (listener) {
443
- this.listeners.delete(listener);
444
- this.eventListeners.delete(listener);
445
- this.sendListeners.delete(listener);
446
- this.stopListeners.delete(listener);
447
- this.doneListeners.delete(listener);
448
- this.contextListeners.delete(listener);
449
- return this;
450
- };
451
- /**
452
- * Starts the interpreter from the given state, or the initial state.
453
- * @param initialState The state to start the statechart from
454
- */
455
-
456
-
457
- Interpreter.prototype.start = function (initialState) {
458
- var _this = this;
459
-
460
- if (this.status === exports.InterpreterStatus.Running) {
461
- // Do not restart the service if it is already started
462
- return this;
463
- } // yes, it's a hack but we need the related cache to be populated for some things to work (like delayed transitions)
464
- // this is usually called by `machine.getInitialState` but if we rehydrate from a state we might bypass this call
465
- // we also don't want to call this method here as it resolves the full initial state which might involve calling assign actions
466
- // and that could potentially lead to some unwanted side-effects (even such as creating some rogue actors)
467
-
468
-
469
- this.machine._init();
470
-
471
- registry.registry.register(this.sessionId, this);
472
- this.initialized = true;
473
- this.status = exports.InterpreterStatus.Running;
474
- var resolvedState = initialState === undefined ? this.initialState : serviceScope.provide(this, function () {
475
- return State.isStateConfig(initialState) ? _this.machine.resolveState(initialState) : _this.machine.resolveState(State.State.from(initialState, _this.machine.context));
476
- });
477
-
478
- if (this.options.devTools) {
479
- this.attachDev();
480
- }
481
-
482
- this.scheduler.initialize(function () {
483
- _this.update(resolvedState, actions.initEvent);
484
- });
485
- return this;
486
- };
487
- /**
488
- * Stops the interpreter and unsubscribe all listeners.
489
- *
490
- * This will also notify the `onStop` listeners.
491
- */
492
-
493
-
494
- Interpreter.prototype.stop = function () {
495
- var e_6, _a, e_7, _b, e_8, _c, e_9, _d, e_10, _e;
496
-
497
- var _this = this;
498
-
499
- try {
500
- for (var _f = _tslib.__values(this.listeners), _g = _f.next(); !_g.done; _g = _f.next()) {
501
- var listener = _g.value;
502
- this.listeners.delete(listener);
503
- }
504
- } catch (e_6_1) {
505
- e_6 = {
506
- error: e_6_1
507
- };
508
- } finally {
509
- try {
510
- if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
511
- } finally {
512
- if (e_6) throw e_6.error;
513
- }
514
- }
515
-
516
- try {
517
- for (var _h = _tslib.__values(this.stopListeners), _j = _h.next(); !_j.done; _j = _h.next()) {
518
- var listener = _j.value; // call listener, then remove
519
-
520
- listener();
521
- this.stopListeners.delete(listener);
522
- }
523
- } catch (e_7_1) {
524
- e_7 = {
525
- error: e_7_1
526
- };
527
- } finally {
528
- try {
529
- if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
530
- } finally {
531
- if (e_7) throw e_7.error;
532
- }
533
- }
534
-
535
- try {
536
- for (var _k = _tslib.__values(this.contextListeners), _l = _k.next(); !_l.done; _l = _k.next()) {
537
- var listener = _l.value;
538
- this.contextListeners.delete(listener);
539
- }
540
- } catch (e_8_1) {
541
- e_8 = {
542
- error: e_8_1
543
- };
544
- } finally {
545
- try {
546
- if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
547
- } finally {
548
- if (e_8) throw e_8.error;
549
- }
550
- }
551
-
552
- try {
553
- for (var _m = _tslib.__values(this.doneListeners), _o = _m.next(); !_o.done; _o = _m.next()) {
554
- var listener = _o.value;
555
- this.doneListeners.delete(listener);
556
- }
557
- } catch (e_9_1) {
558
- e_9 = {
559
- error: e_9_1
560
- };
561
- } finally {
562
- try {
563
- if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
564
- } finally {
565
- if (e_9) throw e_9.error;
566
- }
567
- }
568
-
569
- if (!this.initialized) {
570
- // Interpreter already stopped; do nothing
571
- return this;
572
- }
573
-
574
- _tslib.__spreadArray([], _tslib.__read(this.state.configuration), false).sort(function (a, b) {
575
- return b.order - a.order;
576
- }).forEach(function (stateNode) {
577
- var e_11, _a;
578
-
579
- try {
580
- for (var _b = _tslib.__values(stateNode.definition.exit), _c = _b.next(); !_c.done; _c = _b.next()) {
581
- var action = _c.value;
582
-
583
- _this.exec(action, _this.state);
584
- }
585
- } catch (e_11_1) {
586
- e_11 = {
587
- error: e_11_1
588
- };
589
- } finally {
590
- try {
591
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
592
- } finally {
593
- if (e_11) throw e_11.error;
594
- }
595
- }
596
- }); // Stop all children
597
-
598
-
599
- this.children.forEach(function (child) {
600
- if (utils.isFunction(child.stop)) {
601
- child.stop();
602
- }
603
- });
604
- this.children.clear();
605
-
606
- try {
607
- // Cancel all delayed events
608
- for (var _p = _tslib.__values(Object.keys(this.delayedEventsMap)), _q = _p.next(); !_q.done; _q = _p.next()) {
609
- var key = _q.value;
610
- this.clock.clearTimeout(this.delayedEventsMap[key]);
611
- }
612
- } catch (e_10_1) {
613
- e_10 = {
614
- error: e_10_1
615
- };
616
- } finally {
617
- try {
618
- if (_q && !_q.done && (_e = _p.return)) _e.call(_p);
619
- } finally {
620
- if (e_10) throw e_10.error;
621
- }
622
- }
623
-
624
- this.scheduler.clear();
625
- this.scheduler = new scheduler.Scheduler({
626
- deferEvents: this.options.deferEvents
627
- });
628
- this.initialized = false;
629
- this.status = exports.InterpreterStatus.Stopped;
630
- this._initialState = undefined;
631
- registry.registry.free(this.sessionId);
632
- return this;
633
- };
634
-
635
- Interpreter.prototype.batch = function (events) {
636
- var _this = this;
637
-
638
- if (this.status === exports.InterpreterStatus.NotStarted && this.options.deferEvents) {
639
- // tslint:disable-next-line:no-console
640
- if (!environment.IS_PRODUCTION) {
641
- utils.warn(false, "".concat(events.length, " event(s) were sent to uninitialized service \"").concat(this.machine.id, "\" and are deferred. Make sure .start() is called for this service.\nEvent: ").concat(JSON.stringify(event)));
642
- }
643
- } else if (this.status !== exports.InterpreterStatus.Running) {
644
- throw new Error( // tslint:disable-next-line:max-line-length
645
- "".concat(events.length, " event(s) were sent to uninitialized service \"").concat(this.machine.id, "\". Make sure .start() is called for this service, or set { deferEvents: true } in the service options."));
646
- }
647
-
648
- this.scheduler.schedule(function () {
649
- var e_12, _a;
650
-
651
- var nextState = _this.state;
652
- var batchChanged = false;
653
- var batchedActions = [];
654
-
655
- var _loop_1 = function (event_1) {
656
- var _event = utils.toSCXMLEvent(event_1);
657
-
658
- _this.forward(_event);
659
-
660
- nextState = serviceScope.provide(_this, function () {
661
- return _this.machine.transition(nextState, _event);
662
- });
663
- batchedActions.push.apply(batchedActions, _tslib.__spreadArray([], _tslib.__read(nextState.actions.map(function (a) {
664
- return State.bindActionToState(a, nextState);
665
- })), false));
666
- batchChanged = batchChanged || !!nextState.changed;
667
- };
668
-
669
- try {
670
- for (var events_1 = _tslib.__values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
671
- var event_1 = events_1_1.value;
672
-
673
- _loop_1(event_1);
674
- }
675
- } catch (e_12_1) {
676
- e_12 = {
677
- error: e_12_1
678
- };
679
- } finally {
680
- try {
681
- if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
682
- } finally {
683
- if (e_12) throw e_12.error;
684
- }
685
- }
686
-
687
- nextState.changed = batchChanged;
688
- nextState.actions = batchedActions;
689
-
690
- _this.update(nextState, utils.toSCXMLEvent(events[events.length - 1]));
691
- });
692
- };
693
- /**
694
- * Returns a send function bound to this interpreter instance.
695
- *
696
- * @param event The event to be sent by the sender.
697
- */
698
-
699
-
700
- Interpreter.prototype.sender = function (event) {
701
- return this.send.bind(this, event);
702
- };
703
- /**
704
- * Returns the next state given the interpreter's current state and the event.
705
- *
706
- * This is a pure method that does _not_ update the interpreter's state.
707
- *
708
- * @param event The event to determine the next state
709
- */
710
-
711
-
712
- Interpreter.prototype.nextState = function (event) {
713
- var _this = this;
714
-
715
- var _event = utils.toSCXMLEvent(event);
716
-
717
- if (_event.name.indexOf(actionTypes.errorPlatform) === 0 && !this.state.nextEvents.some(function (nextEvent) {
718
- return nextEvent.indexOf(actionTypes.errorPlatform) === 0;
719
- })) {
720
- throw _event.data.data;
721
- }
722
-
723
- var nextState = serviceScope.provide(this, function () {
724
- return _this.machine.transition(_this.state, _event);
725
- });
726
- return nextState;
727
- };
728
-
729
- Interpreter.prototype.forward = function (event) {
730
- var e_13, _a;
731
-
732
- try {
733
- for (var _b = _tslib.__values(this.forwardTo), _c = _b.next(); !_c.done; _c = _b.next()) {
734
- var id = _c.value;
735
- var child = this.children.get(id);
736
-
737
- if (!child) {
738
- throw new Error("Unable to forward event '".concat(event, "' from interpreter '").concat(this.id, "' to nonexistant child '").concat(id, "'."));
739
- }
740
-
741
- child.send(event);
742
- }
743
- } catch (e_13_1) {
744
- e_13 = {
745
- error: e_13_1
746
- };
747
- } finally {
748
- try {
749
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
750
- } finally {
751
- if (e_13) throw e_13.error;
752
- }
753
- }
754
- };
755
-
756
- Interpreter.prototype.defer = function (sendAction) {
757
- var _this = this;
758
-
759
- this.delayedEventsMap[sendAction.id] = this.clock.setTimeout(function () {
760
- if (sendAction.to) {
761
- _this.sendTo(sendAction._event, sendAction.to);
762
- } else {
763
- _this.send(sendAction._event);
764
- }
765
- }, sendAction.delay);
766
- };
767
-
768
- Interpreter.prototype.cancel = function (sendId) {
769
- this.clock.clearTimeout(this.delayedEventsMap[sendId]);
770
- delete this.delayedEventsMap[sendId];
771
- };
772
-
773
- Interpreter.prototype.exec = function (action, state, actionFunctionMap) {
774
- if (actionFunctionMap === void 0) {
775
- actionFunctionMap = this.machine.options.actions;
776
- }
777
-
778
- var context = state.context,
779
- _event = state._event;
780
- var actionOrExec = action.exec || actions.getActionFunction(action.type, actionFunctionMap);
781
- var exec = utils.isFunction(actionOrExec) ? actionOrExec : actionOrExec ? actionOrExec.exec : action.exec;
782
-
783
- if (exec) {
784
- try {
785
- return exec(context, _event.data, {
786
- action: action,
787
- state: this.state,
788
- _event: _event
789
- });
790
- } catch (err) {
791
- if (this.parent) {
792
- this.parent.send({
793
- type: 'xstate.error',
794
- data: err
795
- });
796
- }
797
-
798
- throw err;
799
- }
800
- }
801
-
802
- switch (action.type) {
803
- case actionTypes.send:
804
- var sendAction = action;
805
-
806
- if (typeof sendAction.delay === 'number') {
807
- this.defer(sendAction);
808
- return;
809
- } else {
810
- if (sendAction.to) {
811
- this.sendTo(sendAction._event, sendAction.to);
812
- } else {
813
- this.send(sendAction._event);
814
- }
815
- }
816
-
817
- break;
818
-
819
- case actionTypes.cancel:
820
- this.cancel(action.sendId);
821
- break;
822
-
823
- case actionTypes.start:
824
- {
825
- if (this.status !== exports.InterpreterStatus.Running) {
826
- return;
827
- }
828
-
829
- var activity = action.activity; // If the activity will be stopped right after it's started
830
- // (such as in transient states)
831
- // don't bother starting the activity.
832
-
833
- if (!this.state.activities[activity.id || activity.type]) {
834
- break;
835
- } // Invoked services
836
-
837
-
838
- if (activity.type === types.ActionTypes.Invoke) {
839
- var invokeSource = utils.toInvokeSource(activity.src);
840
- var serviceCreator = this.machine.options.services ? this.machine.options.services[invokeSource.type] : undefined;
841
- var id = activity.id,
842
- data = activity.data;
843
-
844
- if (!environment.IS_PRODUCTION) {
845
- utils.warn(!('forward' in activity), // tslint:disable-next-line:max-line-length
846
- "`forward` property is deprecated (found in invocation of '".concat(activity.src, "' in in machine '").concat(this.machine.id, "'). ") + "Please use `autoForward` instead.");
847
- }
848
-
849
- var autoForward = 'autoForward' in activity ? activity.autoForward : !!activity.forward;
850
-
851
- if (!serviceCreator) {
852
- // tslint:disable-next-line:no-console
853
- if (!environment.IS_PRODUCTION) {
854
- utils.warn(false, "No service found for invocation '".concat(activity.src, "' in machine '").concat(this.machine.id, "'."));
855
- }
856
-
857
- return;
858
- }
859
-
860
- var resolvedData = data ? utils.mapContext(data, context, _event) : undefined;
861
-
862
- if (typeof serviceCreator === 'string') {
863
- // TODO: warn
864
- return;
865
- }
866
-
867
- var source = utils.isFunction(serviceCreator) ? serviceCreator(context, _event.data, {
868
- data: resolvedData,
869
- src: invokeSource,
870
- meta: activity.meta
871
- }) : serviceCreator;
872
-
873
- if (!source) {
874
- // TODO: warn?
875
- return;
876
- }
877
-
878
- var options = void 0;
879
-
880
- if (utils.isMachine(source)) {
881
- source = resolvedData ? source.withContext(resolvedData) : source;
882
- options = {
883
- autoForward: autoForward
884
- };
885
- }
886
-
887
- this.spawn(source, id, options);
888
- } else {
889
- this.spawnActivity(activity);
890
- }
891
-
892
- break;
893
- }
894
-
895
- case actionTypes.stop:
896
- {
897
- this.stopChild(action.activity.id);
898
- break;
899
- }
900
-
901
- case actionTypes.log:
902
- var label = action.label,
903
- value = action.value;
904
-
905
- if (label) {
906
- this.logger(label, value);
907
- } else {
908
- this.logger(value);
909
- }
910
-
911
- break;
912
-
913
- default:
914
- if (!environment.IS_PRODUCTION) {
915
- utils.warn(false, "No implementation found for action type '".concat(action.type, "'"));
916
- }
917
-
918
- break;
919
- }
920
-
921
- return undefined;
922
- };
923
-
924
- Interpreter.prototype.removeChild = function (childId) {
925
- var _a;
926
-
927
- this.children.delete(childId);
928
- this.forwardTo.delete(childId); // this.state might not exist at the time this is called,
929
- // such as when a child is added then removed while initializing the state
930
-
931
- (_a = this.state) === null || _a === void 0 ? true : delete _a.children[childId];
932
- };
933
-
934
- Interpreter.prototype.stopChild = function (childId) {
935
- var child = this.children.get(childId);
936
-
937
- if (!child) {
938
- return;
939
- }
940
-
941
- this.removeChild(childId);
942
-
943
- if (utils.isFunction(child.stop)) {
944
- child.stop();
945
- }
946
- };
947
-
948
- Interpreter.prototype.spawn = function (entity, name, options) {
949
- if (utils.isPromiseLike(entity)) {
950
- return this.spawnPromise(Promise.resolve(entity), name);
951
- } else if (utils.isFunction(entity)) {
952
- return this.spawnCallback(entity, name);
953
- } else if (Actor.isSpawnedActor(entity)) {
954
- return this.spawnActor(entity, name);
955
- } else if (utils.isObservable(entity)) {
956
- return this.spawnObservable(entity, name);
957
- } else if (utils.isMachine(entity)) {
958
- return this.spawnMachine(entity, _tslib.__assign(_tslib.__assign({}, options), {
959
- id: name
960
- }));
961
- } else if (utils.isBehavior(entity)) {
962
- return this.spawnBehavior(entity, name);
963
- } else {
964
- throw new Error("Unable to spawn entity \"".concat(name, "\" of type \"").concat(typeof entity, "\"."));
965
- }
966
- };
967
-
968
- Interpreter.prototype.spawnMachine = function (machine, options) {
969
- var _this = this;
970
-
971
- if (options === void 0) {
972
- options = {};
973
- }
974
-
975
- var childService = new Interpreter(machine, _tslib.__assign(_tslib.__assign({}, this.options), {
976
- parent: this,
977
- id: options.id || machine.id
978
- }));
979
-
980
- var resolvedOptions = _tslib.__assign(_tslib.__assign({}, DEFAULT_SPAWN_OPTIONS), options);
981
-
982
- if (resolvedOptions.sync) {
983
- childService.onTransition(function (state) {
984
- _this.send(actionTypes.update, {
985
- state: state,
986
- id: childService.id
987
- });
988
- });
989
- }
990
-
991
- var actor = childService;
992
- this.children.set(childService.id, actor);
993
-
994
- if (resolvedOptions.autoForward) {
995
- this.forwardTo.add(childService.id);
996
- }
997
-
998
- childService.onDone(function (doneEvent) {
999
- _this.removeChild(childService.id);
1000
-
1001
- _this.send(utils.toSCXMLEvent(doneEvent, {
1002
- origin: childService.id
1003
- }));
1004
- }).start();
1005
- return actor;
1006
- };
1007
-
1008
- Interpreter.prototype.spawnBehavior = function (behavior, id) {
1009
- var actorRef = behaviors.spawnBehavior(behavior, {
1010
- id: id,
1011
- parent: this
1012
- });
1013
- this.children.set(id, actorRef);
1014
- return actorRef;
1015
- };
1016
-
1017
- Interpreter.prototype.spawnPromise = function (promise, id) {
1018
- var _a;
1019
-
1020
- var _this = this;
1021
-
1022
- var canceled = false;
1023
- var resolvedData;
1024
- promise.then(function (response) {
1025
- if (!canceled) {
1026
- resolvedData = response;
1027
-
1028
- _this.removeChild(id);
1029
-
1030
- _this.send(utils.toSCXMLEvent(actions.doneInvoke(id, response), {
1031
- origin: id
1032
- }));
1033
- }
1034
- }, function (errorData) {
1035
- if (!canceled) {
1036
- _this.removeChild(id);
1037
-
1038
- var errorEvent = actions.error(id, errorData);
1039
-
1040
- try {
1041
- // Send "error.platform.id" to this (parent).
1042
- _this.send(utils.toSCXMLEvent(errorEvent, {
1043
- origin: id
1044
- }));
1045
- } catch (error) {
1046
- utils.reportUnhandledExceptionOnInvocation(errorData, error, id);
1047
-
1048
- if (_this.devTools) {
1049
- _this.devTools.send(errorEvent, _this.state);
1050
- }
1051
-
1052
- if (_this.machine.strict) {
1053
- // it would be better to always stop the state machine if unhandled
1054
- // exception/promise rejection happens but because we don't want to
1055
- // break existing code so enforce it on strict mode only especially so
1056
- // because documentation says that onError is optional
1057
- _this.stop();
1058
- }
1059
- }
1060
- }
1061
- });
1062
- var actor = (_a = {
1063
- id: id,
1064
- send: function () {
1065
- return void 0;
1066
- },
1067
- subscribe: function (next, handleError, complete) {
1068
- var observer = utils.toObserver(next, handleError, complete);
1069
- var unsubscribed = false;
1070
- promise.then(function (response) {
1071
- if (unsubscribed) {
1072
- return;
1073
- }
1074
-
1075
- observer.next(response);
1076
-
1077
- if (unsubscribed) {
1078
- return;
1079
- }
1080
-
1081
- observer.complete();
1082
- }, function (err) {
1083
- if (unsubscribed) {
1084
- return;
1085
- }
1086
-
1087
- observer.error(err);
1088
- });
1089
- return {
1090
- unsubscribe: function () {
1091
- return unsubscribed = true;
1092
- }
1093
- };
1094
- },
1095
- stop: function () {
1096
- canceled = true;
1097
- },
1098
- toJSON: function () {
1099
- return {
1100
- id: id
1101
- };
1102
- },
1103
- getSnapshot: function () {
1104
- return resolvedData;
1105
- }
1106
- }, _a[utils.symbolObservable] = function () {
1107
- return this;
1108
- }, _a);
1109
- this.children.set(id, actor);
1110
- return actor;
1111
- };
1112
-
1113
- Interpreter.prototype.spawnCallback = function (callback, id) {
1114
- var _a;
1115
-
1116
- var _this = this;
1117
-
1118
- var canceled = false;
1119
- var receivers = new Set();
1120
- var listeners = new Set();
1121
- var emitted;
1122
-
1123
- var receive = function (e) {
1124
- emitted = e;
1125
- listeners.forEach(function (listener) {
1126
- return listener(e);
1127
- });
1128
-
1129
- if (canceled) {
1130
- return;
1131
- }
1132
-
1133
- _this.send(utils.toSCXMLEvent(e, {
1134
- origin: id
1135
- }));
1136
- };
1137
-
1138
- var callbackStop;
1139
-
1140
- try {
1141
- callbackStop = callback(receive, function (newListener) {
1142
- receivers.add(newListener);
1143
- });
1144
- } catch (err) {
1145
- this.send(actions.error(id, err));
1146
- }
1147
-
1148
- if (utils.isPromiseLike(callbackStop)) {
1149
- // it turned out to be an async function, can't reliably check this before calling `callback`
1150
- // because transpiled async functions are not recognizable
1151
- return this.spawnPromise(callbackStop, id);
1152
- }
1153
-
1154
- var actor = (_a = {
1155
- id: id,
1156
- send: function (event) {
1157
- return receivers.forEach(function (receiver) {
1158
- return receiver(event);
1159
- });
1160
- },
1161
- subscribe: function (next) {
1162
- var observer = utils.toObserver(next);
1163
- listeners.add(observer.next);
1164
- return {
1165
- unsubscribe: function () {
1166
- listeners.delete(observer.next);
1167
- }
1168
- };
1169
- },
1170
- stop: function () {
1171
- canceled = true;
1172
-
1173
- if (utils.isFunction(callbackStop)) {
1174
- callbackStop();
1175
- }
1176
- },
1177
- toJSON: function () {
1178
- return {
1179
- id: id
1180
- };
1181
- },
1182
- getSnapshot: function () {
1183
- return emitted;
1184
- }
1185
- }, _a[utils.symbolObservable] = function () {
1186
- return this;
1187
- }, _a);
1188
- this.children.set(id, actor);
1189
- return actor;
1190
- };
1191
-
1192
- Interpreter.prototype.spawnObservable = function (source, id) {
1193
- var _a;
1194
-
1195
- var _this = this;
1196
-
1197
- var emitted;
1198
- var subscription = source.subscribe(function (value) {
1199
- emitted = value;
1200
-
1201
- _this.send(utils.toSCXMLEvent(value, {
1202
- origin: id
1203
- }));
1204
- }, function (err) {
1205
- _this.removeChild(id);
1206
-
1207
- _this.send(utils.toSCXMLEvent(actions.error(id, err), {
1208
- origin: id
1209
- }));
1210
- }, function () {
1211
- _this.removeChild(id);
1212
-
1213
- _this.send(utils.toSCXMLEvent(actions.doneInvoke(id), {
1214
- origin: id
1215
- }));
1216
- });
1217
- var actor = (_a = {
1218
- id: id,
1219
- send: function () {
1220
- return void 0;
1221
- },
1222
- subscribe: function (next, handleError, complete) {
1223
- return source.subscribe(next, handleError, complete);
1224
- },
1225
- stop: function () {
1226
- return subscription.unsubscribe();
1227
- },
1228
- getSnapshot: function () {
1229
- return emitted;
1230
- },
1231
- toJSON: function () {
1232
- return {
1233
- id: id
1234
- };
1235
- }
1236
- }, _a[utils.symbolObservable] = function () {
1237
- return this;
1238
- }, _a);
1239
- this.children.set(id, actor);
1240
- return actor;
1241
- };
1242
-
1243
- Interpreter.prototype.spawnActor = function (actor, name) {
1244
- this.children.set(name, actor);
1245
- return actor;
1246
- };
1247
-
1248
- Interpreter.prototype.spawnActivity = function (activity) {
1249
- var implementation = this.machine.options && this.machine.options.activities ? this.machine.options.activities[activity.type] : undefined;
1250
-
1251
- if (!implementation) {
1252
- if (!environment.IS_PRODUCTION) {
1253
- utils.warn(false, "No implementation found for activity '".concat(activity.type, "'"));
1254
- } // tslint:disable-next-line:no-console
1255
-
1256
-
1257
- return;
1258
- } // Start implementation
1259
-
1260
-
1261
- var dispose = implementation(this.state.context, activity);
1262
- this.spawnEffect(activity.id, dispose);
1263
- };
1264
-
1265
- Interpreter.prototype.spawnEffect = function (id, dispose) {
1266
- var _a;
1267
-
1268
- this.children.set(id, (_a = {
1269
- id: id,
1270
- send: function () {
1271
- return void 0;
1272
- },
1273
- subscribe: function () {
1274
- return {
1275
- unsubscribe: function () {
1276
- return void 0;
1277
- }
1278
- };
1279
- },
1280
- stop: dispose || undefined,
1281
- getSnapshot: function () {
1282
- return undefined;
1283
- },
1284
- toJSON: function () {
1285
- return {
1286
- id: id
1287
- };
1288
- }
1289
- }, _a[utils.symbolObservable] = function () {
1290
- return this;
1291
- }, _a));
1292
- };
1293
-
1294
- Interpreter.prototype.attachDev = function () {
1295
- var global = devTools.getGlobal();
1296
-
1297
- if (this.options.devTools && global) {
1298
- if (global.__REDUX_DEVTOOLS_EXTENSION__) {
1299
- var devToolsOptions = typeof this.options.devTools === 'object' ? this.options.devTools : undefined;
1300
- this.devTools = global.__REDUX_DEVTOOLS_EXTENSION__.connect(_tslib.__assign(_tslib.__assign({
1301
- name: this.id,
1302
- autoPause: true,
1303
- stateSanitizer: function (state) {
1304
- return {
1305
- value: state.value,
1306
- context: state.context,
1307
- actions: state.actions
1308
- };
1309
- }
1310
- }, devToolsOptions), {
1311
- features: _tslib.__assign({
1312
- jump: false,
1313
- skip: false
1314
- }, devToolsOptions ? devToolsOptions.features : undefined)
1315
- }), this.machine);
1316
- this.devTools.init(this.state);
1317
- } // add XState-specific dev tooling hook
1318
-
1319
-
1320
- devTools.registerService(this);
1321
- }
1322
- };
1323
-
1324
- Interpreter.prototype.toJSON = function () {
1325
- return {
1326
- id: this.id
1327
- };
1328
- };
1329
-
1330
- Interpreter.prototype[utils.symbolObservable] = function () {
1331
- return this;
1332
- };
1333
-
1334
- Interpreter.prototype.getSnapshot = function () {
1335
- if (this.status === exports.InterpreterStatus.NotStarted) {
1336
- return this.initialState;
1337
- }
1338
-
1339
- return this._state;
1340
- };
1341
- /**
1342
- * The default interpreter options:
1343
- *
1344
- * - `clock` uses the global `setTimeout` and `clearTimeout` functions
1345
- * - `logger` uses the global `console.log()` method
1346
- */
1347
-
1348
-
1349
- Interpreter.defaultOptions = {
1350
- execute: true,
1351
- deferEvents: true,
1352
- clock: {
1353
- setTimeout: function (fn, ms) {
1354
- return setTimeout(fn, ms);
1355
- },
1356
- clearTimeout: function (id) {
1357
- return clearTimeout(id);
1358
- }
1359
- },
1360
- logger: /*#__PURE__*/console.log.bind(console),
1361
- devTools: false
1362
- };
1363
- Interpreter.interpret = interpret;
1364
- return Interpreter;
1365
- }();
1366
-
1367
- var resolveSpawnOptions = function (nameOrOptions) {
1368
- if (utils.isString(nameOrOptions)) {
1369
- return _tslib.__assign(_tslib.__assign({}, DEFAULT_SPAWN_OPTIONS), {
1370
- name: nameOrOptions
1371
- });
1372
- }
1373
-
1374
- return _tslib.__assign(_tslib.__assign(_tslib.__assign({}, DEFAULT_SPAWN_OPTIONS), {
1375
- name: utils.uniqueId()
1376
- }), nameOrOptions);
1377
- };
1378
-
1379
- function spawn(entity, nameOrOptions) {
1380
- var resolvedOptions = resolveSpawnOptions(nameOrOptions);
1381
- return serviceScope.consume(function (service) {
1382
- if (!environment.IS_PRODUCTION) {
1383
- var isLazyEntity = utils.isMachine(entity) || utils.isFunction(entity);
1384
- utils.warn(!!service || isLazyEntity, "Attempted to spawn an Actor (ID: \"".concat(utils.isMachine(entity) ? entity.id : 'undefined', "\") outside of a service. This will have no effect."));
1385
- }
1386
-
1387
- if (service) {
1388
- return service.spawn(entity, resolvedOptions.name, resolvedOptions);
1389
- } else {
1390
- return Actor.createDeferredActor(entity, resolvedOptions.name);
1391
- }
1392
- });
1393
- }
1394
- /**
1395
- * Creates a new Interpreter instance for the given machine with the provided options, if any.
1396
- *
1397
- * @param machine The machine to interpret
1398
- * @param options Interpreter options
1399
- */
1400
-
1401
- function interpret(machine, options) {
1402
- var interpreter = new Interpreter(machine, options);
1403
- return interpreter;
1404
- }
1405
-
1406
- exports.Interpreter = Interpreter;
1407
- exports.interpret = interpret;
1408
- exports.spawn = spawn;