xstate 4.20.1 → 4.23.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 (92) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/dist/xstate.interpreter.js +1 -1
  3. package/dist/xstate.js +1 -1
  4. package/dist/xstate.web.js +2 -2
  5. package/es/Actor.js +17 -5
  6. package/es/Machine.d.ts +6 -3
  7. package/es/Machine.js +3 -6
  8. package/es/State.js +1 -3
  9. package/es/StateNode.d.ts +4 -3
  10. package/es/StateNode.js +34 -20
  11. package/es/_virtual/_tslib.js +59 -73
  12. package/es/actionTypes.js +3 -2
  13. package/es/actions.d.ts +1 -1
  14. package/es/actions.js +51 -37
  15. package/es/behaviors.d.ts +37 -0
  16. package/es/behaviors.js +65 -0
  17. package/es/constants.js +2 -1
  18. package/es/devTools.js +1 -1
  19. package/es/environment.js +2 -1
  20. package/es/index.js +3 -1
  21. package/es/interpreter.d.ts +10 -3
  22. package/es/interpreter.js +44 -22
  23. package/es/invokeUtils.js +4 -3
  24. package/es/mapState.js +1 -1
  25. package/es/match.js +1 -1
  26. package/es/model.d.ts +2 -36
  27. package/es/model.types.d.ts +37 -0
  28. package/es/registry.js +2 -1
  29. package/es/scheduler.js +2 -1
  30. package/es/schema.js +1 -1
  31. package/es/serviceScope.js +1 -3
  32. package/es/stateUtils.js +1 -9
  33. package/es/types.d.ts +26 -5
  34. package/es/types.js +1 -1
  35. package/es/utils.d.ts +3 -2
  36. package/es/utils.js +4 -40
  37. package/lib/Actor.d.ts +25 -25
  38. package/lib/Actor.js +85 -66
  39. package/lib/Machine.d.ts +17 -14
  40. package/lib/Machine.js +14 -14
  41. package/lib/SimulatedClock.d.ts +16 -16
  42. package/lib/State.d.ts +108 -108
  43. package/lib/State.js +246 -236
  44. package/lib/StateNode.d.ts +279 -278
  45. package/lib/StateNode.js +1535 -1339
  46. package/lib/_virtual/_tslib.js +81 -0
  47. package/lib/actionTypes.d.ts +19 -19
  48. package/lib/actionTypes.js +43 -23
  49. package/lib/actions.d.ts +138 -138
  50. package/lib/actions.js +465 -387
  51. package/lib/behaviors.d.ts +37 -0
  52. package/lib/behaviors.js +69 -0
  53. package/lib/constants.d.ts +5 -5
  54. package/lib/constants.js +13 -7
  55. package/lib/devTools.d.ts +15 -15
  56. package/lib/devTools.js +37 -26
  57. package/lib/each.d.ts +3 -3
  58. package/lib/environment.d.ts +1 -1
  59. package/lib/environment.js +7 -4
  60. package/lib/index.d.ts +30 -30
  61. package/lib/index.js +67 -57
  62. package/lib/interpreter.d.ts +205 -198
  63. package/lib/interpreter.js +1306 -1054
  64. package/lib/invoke.d.ts +10 -10
  65. package/lib/invokeUtils.d.ts +6 -6
  66. package/lib/invokeUtils.js +40 -37
  67. package/lib/json.d.ts +30 -30
  68. package/lib/mapState.d.ts +3 -3
  69. package/lib/mapState.js +31 -32
  70. package/lib/match.d.ts +8 -8
  71. package/lib/match.js +33 -47
  72. package/lib/model.d.ts +4 -38
  73. package/lib/model.js +5 -1
  74. package/lib/model.types.d.ts +37 -0
  75. package/lib/model.types.js +2 -0
  76. package/lib/patterns.d.ts +13 -13
  77. package/lib/registry.d.ts +8 -8
  78. package/lib/registry.js +21 -18
  79. package/lib/scheduler.d.ts +16 -16
  80. package/lib/scheduler.js +79 -70
  81. package/lib/schema.d.ts +1 -1
  82. package/lib/schema.js +6 -4
  83. package/lib/scxml.d.ts +5 -5
  84. package/lib/serviceScope.d.ts +3 -3
  85. package/lib/serviceScope.js +16 -12
  86. package/lib/stateUtils.d.ts +14 -14
  87. package/lib/stateUtils.js +231 -199
  88. package/lib/types.d.ts +928 -907
  89. package/lib/types.js +29 -29
  90. package/lib/utils.d.ts +68 -67
  91. package/lib/utils.js +530 -529
  92. package/package.json +6 -6
package/lib/actions.js CHANGED
@@ -1,460 +1,538 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.resolveActions = exports.choose = exports.escalate = exports.forwardTo = exports.pure = exports.error = exports.doneInvoke = exports.done = exports.after = exports.isActionObject = exports.assign = exports.resolveStop = exports.stop = exports.start = exports.cancel = exports.resolveLog = exports.log = exports.respond = exports.sendUpdate = exports.sendParent = exports.resolveSend = exports.send = exports.resolveRaise = exports.raise = exports.toActivityDefinition = exports.toActionObjects = exports.toActionObject = exports.getActionFunction = exports.initEvent = exports.actionTypes = void 0;
31
- var types_1 = require("./types");
32
- var actionTypes = require("./actionTypes");
33
- exports.actionTypes = actionTypes;
34
- var utils_1 = require("./utils");
35
- var environment_1 = require("./environment");
36
- exports.initEvent = utils_1.toSCXMLEvent({ type: actionTypes.init });
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ var environment = require('./environment.js');
7
+ var utils = require('./utils.js');
8
+ var types = require('./types.js');
9
+ var actionTypes = require('./actionTypes.js');
10
+
11
+ var initEvent = /*#__PURE__*/utils.toSCXMLEvent({
12
+ type: actionTypes.init
13
+ });
37
14
  function getActionFunction(actionType, actionFunctionMap) {
38
- return actionFunctionMap
39
- ? actionFunctionMap[actionType] || undefined
40
- : undefined;
15
+ return actionFunctionMap ? actionFunctionMap[actionType] || undefined : undefined;
41
16
  }
42
- exports.getActionFunction = getActionFunction;
43
17
  function toActionObject(action, actionFunctionMap) {
44
- var actionObject;
45
- if (utils_1.isString(action) || typeof action === 'number') {
46
- var exec = getActionFunction(action, actionFunctionMap);
47
- if (utils_1.isFunction(exec)) {
48
- actionObject = {
49
- type: action,
50
- exec: exec
51
- };
52
- }
53
- else if (exec) {
54
- actionObject = exec;
55
- }
56
- else {
57
- actionObject = { type: action, exec: undefined };
58
- }
59
- }
60
- else if (utils_1.isFunction(action)) {
61
- actionObject = {
62
- // Convert action to string if unnamed
63
- type: action.name || action.toString(),
64
- exec: action
65
- };
18
+ var actionObject;
19
+
20
+ if (utils.isString(action) || typeof action === 'number') {
21
+ var exec = getActionFunction(action, actionFunctionMap);
22
+
23
+ if (utils.isFunction(exec)) {
24
+ actionObject = {
25
+ type: action,
26
+ exec: exec
27
+ };
28
+ } else if (exec) {
29
+ actionObject = exec;
30
+ } else {
31
+ actionObject = {
32
+ type: action,
33
+ exec: undefined
34
+ };
66
35
  }
67
- else {
68
- var exec = getActionFunction(action.type, actionFunctionMap);
69
- if (utils_1.isFunction(exec)) {
70
- actionObject = __assign(__assign({}, action), { exec: exec });
71
- }
72
- else if (exec) {
73
- var actionType = exec.type || action.type;
74
- actionObject = __assign(__assign(__assign({}, exec), action), { type: actionType });
75
- }
76
- else {
77
- actionObject = action;
78
- }
36
+ } else if (utils.isFunction(action)) {
37
+ actionObject = {
38
+ // Convert action to string if unnamed
39
+ type: action.name || action.toString(),
40
+ exec: action
41
+ };
42
+ } else {
43
+ var exec = getActionFunction(action.type, actionFunctionMap);
44
+
45
+ if (utils.isFunction(exec)) {
46
+ actionObject = _tslib.__assign(_tslib.__assign({}, action), {
47
+ exec: exec
48
+ });
49
+ } else if (exec) {
50
+ var actionType = exec.type || action.type;
51
+ actionObject = _tslib.__assign(_tslib.__assign(_tslib.__assign({}, exec), action), {
52
+ type: actionType
53
+ });
54
+ } else {
55
+ actionObject = action;
79
56
  }
80
- Object.defineProperty(actionObject, 'toString', {
81
- value: function () { return actionObject.type; },
82
- enumerable: false,
83
- configurable: true
84
- });
85
- return actionObject;
57
+ }
58
+
59
+ Object.defineProperty(actionObject, 'toString', {
60
+ value: function () {
61
+ return actionObject.type;
62
+ },
63
+ enumerable: false,
64
+ configurable: true
65
+ });
66
+ return actionObject;
86
67
  }
87
- exports.toActionObject = toActionObject;
88
68
  var toActionObjects = function (action, actionFunctionMap) {
89
- if (!action) {
90
- return [];
91
- }
92
- var actions = utils_1.isArray(action) ? action : [action];
93
- return actions.map(function (subAction) {
94
- return toActionObject(subAction, actionFunctionMap);
95
- });
69
+ if (!action) {
70
+ return [];
71
+ }
72
+
73
+ var actions = utils.isArray(action) ? action : [action];
74
+ return actions.map(function (subAction) {
75
+ return toActionObject(subAction, actionFunctionMap);
76
+ });
96
77
  };
97
- exports.toActionObjects = toActionObjects;
98
78
  function toActivityDefinition(action) {
99
- var actionObject = toActionObject(action);
100
- return __assign(__assign({ id: utils_1.isString(action) ? action : actionObject.id }, actionObject), { type: actionObject.type });
79
+ var actionObject = toActionObject(action);
80
+ return _tslib.__assign(_tslib.__assign({
81
+ id: utils.isString(action) ? action : actionObject.id
82
+ }, actionObject), {
83
+ type: actionObject.type
84
+ });
101
85
  }
102
- exports.toActivityDefinition = toActivityDefinition;
103
- /**
104
- * Raises an event. This places the event in the internal event queue, so that
105
- * the event is immediately consumed by the machine in the current step.
106
- *
107
- * @param eventType The event to raise.
86
+ /**
87
+ * Raises an event. This places the event in the internal event queue, so that
88
+ * the event is immediately consumed by the machine in the current step.
89
+ *
90
+ * @param eventType The event to raise.
108
91
  */
92
+
109
93
  function raise(event) {
110
- if (!utils_1.isString(event)) {
111
- return send(event, { to: types_1.SpecialTargets.Internal });
112
- }
113
- return {
114
- type: actionTypes.raise,
115
- event: event
116
- };
94
+ if (!utils.isString(event)) {
95
+ return send(event, {
96
+ to: types.SpecialTargets.Internal
97
+ });
98
+ }
99
+
100
+ return {
101
+ type: actionTypes.raise,
102
+ event: event
103
+ };
117
104
  }
118
- exports.raise = raise;
119
105
  function resolveRaise(action) {
120
- return {
121
- type: actionTypes.raise,
122
- _event: utils_1.toSCXMLEvent(action.event)
123
- };
106
+ return {
107
+ type: actionTypes.raise,
108
+ _event: utils.toSCXMLEvent(action.event)
109
+ };
124
110
  }
125
- exports.resolveRaise = resolveRaise;
126
- /**
127
- * Sends an event. This returns an action that will be read by an interpreter to
128
- * send the event in the next step, after the current step is finished executing.
129
- *
130
- * @param event The event to send.
131
- * @param options Options to pass into the send event:
132
- * - `id` - The unique send event identifier (used with `cancel()`).
133
- * - `delay` - The number of milliseconds to delay the sending of the event.
134
- * - `to` - The target of this event (by default, the machine the event was sent from).
111
+ /**
112
+ * Sends an event. This returns an action that will be read by an interpreter to
113
+ * send the event in the next step, after the current step is finished executing.
114
+ *
115
+ * @param event The event to send.
116
+ * @param options Options to pass into the send event:
117
+ * - `id` - The unique send event identifier (used with `cancel()`).
118
+ * - `delay` - The number of milliseconds to delay the sending of the event.
119
+ * - `to` - The target of this event (by default, the machine the event was sent from).
135
120
  */
121
+
136
122
  function send(event, options) {
137
- return {
138
- to: options ? options.to : undefined,
139
- type: actionTypes.send,
140
- event: utils_1.isFunction(event) ? event : utils_1.toEventObject(event),
141
- delay: options ? options.delay : undefined,
142
- id: options && options.id !== undefined
143
- ? options.id
144
- : utils_1.isFunction(event)
145
- ? event.name
146
- : utils_1.getEventType(event)
147
- };
123
+ return {
124
+ to: options ? options.to : undefined,
125
+ type: actionTypes.send,
126
+ event: utils.isFunction(event) ? event : utils.toEventObject(event),
127
+ delay: options ? options.delay : undefined,
128
+ id: options && options.id !== undefined ? options.id : utils.isFunction(event) ? event.name : utils.getEventType(event)
129
+ };
148
130
  }
149
- exports.send = send;
150
131
  function resolveSend(action, ctx, _event, delaysMap) {
151
- var meta = {
152
- _event: _event
153
- };
154
- // TODO: helper function for resolving Expr
155
- var resolvedEvent = utils_1.toSCXMLEvent(utils_1.isFunction(action.event)
156
- ? action.event(ctx, _event.data, meta)
157
- : action.event);
158
- var resolvedDelay;
159
- if (utils_1.isString(action.delay)) {
160
- var configDelay = delaysMap && delaysMap[action.delay];
161
- resolvedDelay = utils_1.isFunction(configDelay)
162
- ? configDelay(ctx, _event.data, meta)
163
- : configDelay;
164
- }
165
- else {
166
- resolvedDelay = utils_1.isFunction(action.delay)
167
- ? action.delay(ctx, _event.data, meta)
168
- : action.delay;
169
- }
170
- var resolvedTarget = utils_1.isFunction(action.to)
171
- ? action.to(ctx, _event.data, meta)
172
- : action.to;
173
- return __assign(__assign({}, action), { to: resolvedTarget, _event: resolvedEvent, event: resolvedEvent.data, delay: resolvedDelay });
132
+ var meta = {
133
+ _event: _event
134
+ }; // TODO: helper function for resolving Expr
135
+
136
+ var resolvedEvent = utils.toSCXMLEvent(utils.isFunction(action.event) ? action.event(ctx, _event.data, meta) : action.event);
137
+ var resolvedDelay;
138
+
139
+ if (utils.isString(action.delay)) {
140
+ var configDelay = delaysMap && delaysMap[action.delay];
141
+ resolvedDelay = utils.isFunction(configDelay) ? configDelay(ctx, _event.data, meta) : configDelay;
142
+ } else {
143
+ resolvedDelay = utils.isFunction(action.delay) ? action.delay(ctx, _event.data, meta) : action.delay;
144
+ }
145
+
146
+ var resolvedTarget = utils.isFunction(action.to) ? action.to(ctx, _event.data, meta) : action.to;
147
+ return _tslib.__assign(_tslib.__assign({}, action), {
148
+ to: resolvedTarget,
149
+ _event: resolvedEvent,
150
+ event: resolvedEvent.data,
151
+ delay: resolvedDelay
152
+ });
174
153
  }
175
- exports.resolveSend = resolveSend;
176
- /**
177
- * Sends an event to this machine's parent.
178
- *
179
- * @param event The event to send to the parent machine.
180
- * @param options Options to pass into the send event.
154
+ /**
155
+ * Sends an event to this machine's parent.
156
+ *
157
+ * @param event The event to send to the parent machine.
158
+ * @param options Options to pass into the send event.
181
159
  */
160
+
182
161
  function sendParent(event, options) {
183
- return send(event, __assign(__assign({}, options), { to: types_1.SpecialTargets.Parent }));
162
+ return send(event, _tslib.__assign(_tslib.__assign({}, options), {
163
+ to: types.SpecialTargets.Parent
164
+ }));
184
165
  }
185
- exports.sendParent = sendParent;
186
- /**
187
- * Sends an update event to this machine's parent.
166
+ /**
167
+ * Sends an update event to this machine's parent.
188
168
  */
169
+
189
170
  function sendUpdate() {
190
- return sendParent(actionTypes.update);
171
+ return sendParent(actionTypes.update);
191
172
  }
192
- exports.sendUpdate = sendUpdate;
193
- /**
194
- * Sends an event back to the sender of the original event.
195
- *
196
- * @param event The event to send back to the sender
197
- * @param options Options to pass into the send event
173
+ /**
174
+ * Sends an event back to the sender of the original event.
175
+ *
176
+ * @param event The event to send back to the sender
177
+ * @param options Options to pass into the send event
198
178
  */
179
+
199
180
  function respond(event, options) {
200
- return send(event, __assign(__assign({}, options), { to: function (_, __, _a) {
201
- var _event = _a._event;
202
- return _event.origin; // TODO: handle when _event.origin is undefined
203
- } }));
181
+ return send(event, _tslib.__assign(_tslib.__assign({}, options), {
182
+ to: function (_, __, _a) {
183
+ var _event = _a._event;
184
+ return _event.origin; // TODO: handle when _event.origin is undefined
185
+ }
186
+ }));
204
187
  }
205
- exports.respond = respond;
206
- var defaultLogExpr = function (context, event) { return ({
188
+
189
+ var defaultLogExpr = function (context, event) {
190
+ return {
207
191
  context: context,
208
192
  event: event
209
- }); };
210
- /**
211
- *
212
- * @param expr The expression function to evaluate which will be logged.
213
- * Takes in 2 arguments:
214
- * - `ctx` - the current state context
215
- * - `event` - the event that caused this action to be executed.
216
- * @param label The label to give to the logged expression.
193
+ };
194
+ };
195
+ /**
196
+ *
197
+ * @param expr The expression function to evaluate which will be logged.
198
+ * Takes in 2 arguments:
199
+ * - `ctx` - the current state context
200
+ * - `event` - the event that caused this action to be executed.
201
+ * @param label The label to give to the logged expression.
217
202
  */
203
+
204
+
218
205
  function log(expr, label) {
219
- if (expr === void 0) { expr = defaultLogExpr; }
220
- return {
221
- type: actionTypes.log,
222
- label: label,
223
- expr: expr
224
- };
206
+ if (expr === void 0) {
207
+ expr = defaultLogExpr;
208
+ }
209
+
210
+ return {
211
+ type: actionTypes.log,
212
+ label: label,
213
+ expr: expr
214
+ };
225
215
  }
226
- exports.log = log;
227
- var resolveLog = function (action, ctx, _event) { return (__assign(__assign({}, action), { value: utils_1.isString(action.expr)
228
- ? action.expr
229
- : action.expr(ctx, _event.data, {
230
- _event: _event
231
- }) })); };
232
- exports.resolveLog = resolveLog;
233
- /**
234
- * Cancels an in-flight `send(...)` action. A canceled sent action will not
235
- * be executed, nor will its event be sent, unless it has already been sent
236
- * (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
237
- *
238
- * @param sendId The `id` of the `send(...)` action to cancel.
216
+ var resolveLog = function (action, ctx, _event) {
217
+ return _tslib.__assign(_tslib.__assign({}, action), {
218
+ value: utils.isString(action.expr) ? action.expr : action.expr(ctx, _event.data, {
219
+ _event: _event
220
+ })
221
+ });
222
+ };
223
+ /**
224
+ * Cancels an in-flight `send(...)` action. A canceled sent action will not
225
+ * be executed, nor will its event be sent, unless it has already been sent
226
+ * (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`).
227
+ *
228
+ * @param sendId The `id` of the `send(...)` action to cancel.
239
229
  */
230
+
240
231
  var cancel = function (sendId) {
241
- return {
242
- type: actionTypes.cancel,
243
- sendId: sendId
244
- };
232
+ return {
233
+ type: actionTypes.cancel,
234
+ sendId: sendId
235
+ };
245
236
  };
246
- exports.cancel = cancel;
247
- /**
248
- * Starts an activity.
249
- *
250
- * @param activity The activity to start.
237
+ /**
238
+ * Starts an activity.
239
+ *
240
+ * @param activity The activity to start.
251
241
  */
242
+
252
243
  function start(activity) {
253
- var activityDef = toActivityDefinition(activity);
254
- return {
255
- type: types_1.ActionTypes.Start,
256
- activity: activityDef,
257
- exec: undefined
258
- };
244
+ var activityDef = toActivityDefinition(activity);
245
+ return {
246
+ type: types.ActionTypes.Start,
247
+ activity: activityDef,
248
+ exec: undefined
249
+ };
259
250
  }
260
- exports.start = start;
261
- /**
262
- * Stops an activity.
263
- *
264
- * @param actorRef The activity to stop.
251
+ /**
252
+ * Stops an activity.
253
+ *
254
+ * @param actorRef The activity to stop.
265
255
  */
256
+
266
257
  function stop(actorRef) {
267
- var activity = utils_1.isFunction(actorRef)
268
- ? actorRef
269
- : toActivityDefinition(actorRef);
270
- return {
271
- type: types_1.ActionTypes.Stop,
272
- activity: activity,
273
- exec: undefined
274
- };
258
+ var activity = utils.isFunction(actorRef) ? actorRef : toActivityDefinition(actorRef);
259
+ return {
260
+ type: types.ActionTypes.Stop,
261
+ activity: activity,
262
+ exec: undefined
263
+ };
275
264
  }
276
- exports.stop = stop;
277
265
  function resolveStop(action, context, _event) {
278
- var actorRefOrString = utils_1.isFunction(action.activity)
279
- ? action.activity(context, _event.data)
280
- : action.activity;
281
- var resolvedActorRef = typeof actorRefOrString === 'string'
282
- ? { id: actorRefOrString }
283
- : actorRefOrString;
284
- var actionObject = {
285
- type: types_1.ActionTypes.Stop,
286
- activity: resolvedActorRef
287
- };
288
- return actionObject;
266
+ var actorRefOrString = utils.isFunction(action.activity) ? action.activity(context, _event.data) : action.activity;
267
+ var resolvedActorRef = typeof actorRefOrString === 'string' ? {
268
+ id: actorRefOrString
269
+ } : actorRefOrString;
270
+ var actionObject = {
271
+ type: types.ActionTypes.Stop,
272
+ activity: resolvedActorRef
273
+ };
274
+ return actionObject;
289
275
  }
290
- exports.resolveStop = resolveStop;
291
- /**
292
- * Updates the current context of the machine.
293
- *
294
- * @param assignment An object that represents the partial context to update.
276
+ /**
277
+ * Updates the current context of the machine.
278
+ *
279
+ * @param assignment An object that represents the partial context to update.
295
280
  */
281
+
296
282
  var assign = function (assignment) {
297
- return {
298
- type: actionTypes.assign,
299
- assignment: assignment
300
- };
283
+ return {
284
+ type: actionTypes.assign,
285
+ assignment: assignment
286
+ };
301
287
  };
302
- exports.assign = assign;
303
- function isActionObject(action) {
304
- return typeof action === 'object' && 'type' in action;
305
- }
306
- exports.isActionObject = isActionObject;
307
- /**
308
- * Returns an event type that represents an implicit event that
309
- * is sent after the specified `delay`.
310
- *
311
- * @param delayRef The delay in milliseconds
312
- * @param id The state node ID where this event is handled
288
+ /**
289
+ * Returns an event type that represents an implicit event that
290
+ * is sent after the specified `delay`.
291
+ *
292
+ * @param delayRef The delay in milliseconds
293
+ * @param id The state node ID where this event is handled
313
294
  */
295
+
314
296
  function after(delayRef, id) {
315
- var idSuffix = id ? "#" + id : '';
316
- return types_1.ActionTypes.After + "(" + delayRef + ")" + idSuffix;
297
+ var idSuffix = id ? "#" + id : '';
298
+ return types.ActionTypes.After + "(" + delayRef + ")" + idSuffix;
317
299
  }
318
- exports.after = after;
319
- /**
320
- * Returns an event that represents that a final state node
321
- * has been reached in the parent state node.
322
- *
323
- * @param id The final state node's parent state node `id`
324
- * @param data The data to pass into the event
300
+ /**
301
+ * Returns an event that represents that a final state node
302
+ * has been reached in the parent state node.
303
+ *
304
+ * @param id The final state node's parent state node `id`
305
+ * @param data The data to pass into the event
325
306
  */
307
+
326
308
  function done(id, data) {
327
- var type = types_1.ActionTypes.DoneState + "." + id;
328
- var eventObject = {
329
- type: type,
330
- data: data
331
- };
332
- eventObject.toString = function () { return type; };
333
- return eventObject;
309
+ var type = types.ActionTypes.DoneState + "." + id;
310
+ var eventObject = {
311
+ type: type,
312
+ data: data
313
+ };
314
+
315
+ eventObject.toString = function () {
316
+ return type;
317
+ };
318
+
319
+ return eventObject;
334
320
  }
335
- exports.done = done;
336
- /**
337
- * Returns an event that represents that an invoked service has terminated.
338
- *
339
- * An invoked service is terminated when it has reached a top-level final state node,
340
- * but not when it is canceled.
341
- *
342
- * @param id The final state node ID
343
- * @param data The data to pass into the event
321
+ /**
322
+ * Returns an event that represents that an invoked service has terminated.
323
+ *
324
+ * An invoked service is terminated when it has reached a top-level final state node,
325
+ * but not when it is canceled.
326
+ *
327
+ * @param id The final state node ID
328
+ * @param data The data to pass into the event
344
329
  */
330
+
345
331
  function doneInvoke(id, data) {
346
- var type = types_1.ActionTypes.DoneInvoke + "." + id;
347
- var eventObject = {
348
- type: type,
349
- data: data
350
- };
351
- eventObject.toString = function () { return type; };
352
- return eventObject;
332
+ var type = types.ActionTypes.DoneInvoke + "." + id;
333
+ var eventObject = {
334
+ type: type,
335
+ data: data
336
+ };
337
+
338
+ eventObject.toString = function () {
339
+ return type;
340
+ };
341
+
342
+ return eventObject;
353
343
  }
354
- exports.doneInvoke = doneInvoke;
355
344
  function error(id, data) {
356
- var type = types_1.ActionTypes.ErrorPlatform + "." + id;
357
- var eventObject = { type: type, data: data };
358
- eventObject.toString = function () { return type; };
359
- return eventObject;
345
+ var type = types.ActionTypes.ErrorPlatform + "." + id;
346
+ var eventObject = {
347
+ type: type,
348
+ data: data
349
+ };
350
+
351
+ eventObject.toString = function () {
352
+ return type;
353
+ };
354
+
355
+ return eventObject;
360
356
  }
361
- exports.error = error;
362
357
  function pure(getActions) {
363
- return {
364
- type: types_1.ActionTypes.Pure,
365
- get: getActions
366
- };
358
+ return {
359
+ type: types.ActionTypes.Pure,
360
+ get: getActions
361
+ };
367
362
  }
368
- exports.pure = pure;
369
- /**
370
- * Forwards (sends) an event to a specified service.
371
- *
372
- * @param target The target service to forward the event to.
373
- * @param options Options to pass into the send action creator.
363
+ /**
364
+ * Forwards (sends) an event to a specified service.
365
+ *
366
+ * @param target The target service to forward the event to.
367
+ * @param options Options to pass into the send action creator.
374
368
  */
369
+
375
370
  function forwardTo(target, options) {
376
- return send(function (_, event) { return event; }, __assign(__assign({}, options), { to: target }));
371
+ return send(function (_, event) {
372
+ return event;
373
+ }, _tslib.__assign(_tslib.__assign({}, options), {
374
+ to: target
375
+ }));
377
376
  }
378
- exports.forwardTo = forwardTo;
379
- /**
380
- * Escalates an error by sending it as an event to this machine's parent.
381
- *
382
- * @param errorData The error data to send, or the expression function that
383
- * takes in the `context`, `event`, and `meta`, and returns the error data to send.
384
- * @param options Options to pass into the send action creator.
377
+ /**
378
+ * Escalates an error by sending it as an event to this machine's parent.
379
+ *
380
+ * @param errorData The error data to send, or the expression function that
381
+ * takes in the `context`, `event`, and `meta`, and returns the error data to send.
382
+ * @param options Options to pass into the send action creator.
385
383
  */
384
+
386
385
  function escalate(errorData, options) {
387
- return sendParent(function (context, event, meta) {
388
- return {
389
- type: actionTypes.error,
390
- data: utils_1.isFunction(errorData)
391
- ? errorData(context, event, meta)
392
- : errorData
393
- };
394
- }, __assign(__assign({}, options), { to: types_1.SpecialTargets.Parent }));
395
- }
396
- exports.escalate = escalate;
397
- function choose(conds) {
386
+ return sendParent(function (context, event, meta) {
398
387
  return {
399
- type: types_1.ActionTypes.Choose,
400
- conds: conds
388
+ type: actionTypes.error,
389
+ data: utils.isFunction(errorData) ? errorData(context, event, meta) : errorData
401
390
  };
391
+ }, _tslib.__assign(_tslib.__assign({}, options), {
392
+ to: types.SpecialTargets.Parent
393
+ }));
402
394
  }
403
- exports.choose = choose;
404
- function resolveActions(machine, currentState, currentContext, _event, actions) {
405
- var _a = __read(utils_1.partition(actions, function (action) {
406
- return action.type === actionTypes.assign;
407
- }), 2), assignActions = _a[0], otherActions = _a[1];
408
- var updatedContext = assignActions.length
409
- ? utils_1.updateContext(currentContext, _event, assignActions, currentState)
410
- : currentContext;
411
- var resolvedActions = utils_1.flatten(otherActions.map(function (actionObject) {
412
- var _a;
413
- switch (actionObject.type) {
414
- case actionTypes.raise:
415
- return resolveRaise(actionObject);
416
- case actionTypes.send:
417
- var sendAction = resolveSend(actionObject, updatedContext, _event, machine.options.delays); // TODO: fix ActionTypes.Init
418
- if (!environment_1.IS_PRODUCTION) {
419
- // warn after resolving as we can create better contextual message here
420
- utils_1.warn(!utils_1.isString(actionObject.delay) ||
421
- typeof sendAction.delay === 'number',
422
- // tslint:disable-next-line:max-line-length
423
- "No delay reference for delay expression '" + actionObject.delay + "' was found on machine '" + machine.id + "'");
424
- }
425
- return sendAction;
426
- case actionTypes.log:
427
- return exports.resolveLog(actionObject, updatedContext, _event);
428
- case actionTypes.choose: {
429
- var chooseAction = actionObject;
430
- var matchedActions = (_a = chooseAction.conds.find(function (condition) {
431
- var guard = utils_1.toGuard(condition.cond, machine.options.guards);
432
- return (!guard ||
433
- utils_1.evaluateGuard(machine, guard, updatedContext, _event, currentState));
434
- })) === null || _a === void 0 ? void 0 : _a.actions;
435
- if (!matchedActions) {
436
- return [];
437
- }
438
- var resolved = resolveActions(machine, currentState, updatedContext, _event, exports.toActionObjects(utils_1.toArray(matchedActions), machine.options.actions));
439
- updatedContext = resolved[1];
440
- return resolved[0];
441
- }
442
- case actionTypes.pure: {
443
- var matchedActions = actionObject.get(updatedContext, _event.data);
444
- if (!matchedActions) {
445
- return [];
446
- }
447
- var resolved = resolveActions(machine, currentState, updatedContext, _event, exports.toActionObjects(utils_1.toArray(matchedActions), machine.options.actions));
448
- updatedContext = resolved[1];
449
- return resolved[0];
450
- }
451
- case actionTypes.stop: {
452
- return resolveStop(actionObject, updatedContext, _event);
395
+ function choose(conds) {
396
+ return {
397
+ type: types.ActionTypes.Choose,
398
+ conds: conds
399
+ };
400
+ }
401
+ function resolveActions(machine, currentState, currentContext, _event, actions, preserveActionOrder) {
402
+ if (preserveActionOrder === void 0) {
403
+ preserveActionOrder = false;
404
+ }
405
+
406
+ var _a = _tslib.__read(preserveActionOrder ? [[], actions] : utils.partition(actions, function (action) {
407
+ return action.type === actionTypes.assign;
408
+ }), 2),
409
+ assignActions = _a[0],
410
+ otherActions = _a[1];
411
+
412
+ var updatedContext = assignActions.length ? utils.updateContext(currentContext, _event, assignActions, currentState) : currentContext;
413
+ var preservedContexts = preserveActionOrder ? [currentContext] : undefined;
414
+ var resolvedActions = utils.flatten(otherActions.map(function (actionObject) {
415
+ var _a;
416
+
417
+ switch (actionObject.type) {
418
+ case actionTypes.raise:
419
+ return resolveRaise(actionObject);
420
+
421
+ case actionTypes.send:
422
+ var sendAction = resolveSend(actionObject, updatedContext, _event, machine.options.delays); // TODO: fix ActionTypes.Init
423
+
424
+ if (!environment.IS_PRODUCTION) {
425
+ // warn after resolving as we can create better contextual message here
426
+ utils.warn(!utils.isString(actionObject.delay) || typeof sendAction.delay === 'number', // tslint:disable-next-line:max-line-length
427
+ "No delay reference for delay expression '" + actionObject.delay + "' was found on machine '" + machine.id + "'");
428
+ }
429
+
430
+ return sendAction;
431
+
432
+ case actionTypes.log:
433
+ return resolveLog(actionObject, updatedContext, _event);
434
+
435
+ case actionTypes.choose:
436
+ {
437
+ var chooseAction = actionObject;
438
+ var matchedActions = (_a = chooseAction.conds.find(function (condition) {
439
+ var guard = utils.toGuard(condition.cond, machine.options.guards);
440
+ return !guard || utils.evaluateGuard(machine, guard, updatedContext, _event, currentState);
441
+ })) === null || _a === void 0 ? void 0 : _a.actions;
442
+
443
+ if (!matchedActions) {
444
+ return [];
445
+ }
446
+
447
+ var _b = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(utils.toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
448
+ resolvedActionsFromChoose = _b[0],
449
+ resolvedContextFromChoose = _b[1];
450
+
451
+ updatedContext = resolvedContextFromChoose;
452
+ preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
453
+ return resolvedActionsFromChoose;
454
+ }
455
+
456
+ case actionTypes.pure:
457
+ {
458
+ var matchedActions = actionObject.get(updatedContext, _event.data);
459
+
460
+ if (!matchedActions) {
461
+ return [];
462
+ }
463
+
464
+ var _c = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(utils.toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
465
+ resolvedActionsFromPure = _c[0],
466
+ resolvedContext = _c[1];
467
+
468
+ updatedContext = resolvedContext;
469
+ preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
470
+ return resolvedActionsFromPure;
471
+ }
472
+
473
+ case actionTypes.stop:
474
+ {
475
+ return resolveStop(actionObject, updatedContext, _event);
476
+ }
477
+
478
+ case actionTypes.assign:
479
+ {
480
+ updatedContext = utils.updateContext(updatedContext, _event, [actionObject], currentState);
481
+ preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
482
+ break;
483
+ }
484
+
485
+ default:
486
+ var resolvedActionObject = toActionObject(actionObject, machine.options.actions);
487
+ var exec_1 = resolvedActionObject.exec;
488
+
489
+ if (exec_1 && preservedContexts) {
490
+ var contextIndex_1 = preservedContexts.length - 1;
491
+
492
+ resolvedActionObject.exec = function (_ctx) {
493
+ var args = [];
494
+
495
+ for (var _i = 1; _i < arguments.length; _i++) {
496
+ args[_i - 1] = arguments[_i];
453
497
  }
454
- default:
455
- return toActionObject(actionObject, machine.options.actions);
498
+
499
+ exec_1 === null || exec_1 === void 0 ? void 0 : exec_1.apply(void 0, _tslib.__spreadArray([preservedContexts[contextIndex_1]], _tslib.__read(args)));
500
+ };
456
501
  }
457
- }));
458
- return [resolvedActions, updatedContext];
502
+
503
+ return resolvedActionObject;
504
+ }
505
+ }).filter(function (a) {
506
+ return !!a;
507
+ }));
508
+ return [resolvedActions, updatedContext];
459
509
  }
510
+
511
+ exports.after = after;
512
+ exports.assign = assign;
513
+ exports.cancel = cancel;
514
+ exports.choose = choose;
515
+ exports.done = done;
516
+ exports.doneInvoke = doneInvoke;
517
+ exports.error = error;
518
+ exports.escalate = escalate;
519
+ exports.forwardTo = forwardTo;
520
+ exports.getActionFunction = getActionFunction;
521
+ exports.initEvent = initEvent;
522
+ exports.log = log;
523
+ exports.pure = pure;
524
+ exports.raise = raise;
460
525
  exports.resolveActions = resolveActions;
526
+ exports.resolveLog = resolveLog;
527
+ exports.resolveRaise = resolveRaise;
528
+ exports.resolveSend = resolveSend;
529
+ exports.resolveStop = resolveStop;
530
+ exports.respond = respond;
531
+ exports.send = send;
532
+ exports.sendParent = sendParent;
533
+ exports.sendUpdate = sendUpdate;
534
+ exports.start = start;
535
+ exports.stop = stop;
536
+ exports.toActionObject = toActionObject;
537
+ exports.toActionObjects = toActionObjects;
538
+ exports.toActivityDefinition = toActivityDefinition;