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 actionTypes = require('./actionTypes.js');
|
8 | var utils = require('./utils.js');
|
9 | var environment = require('./environment.js');
|
10 |
|
11 | var initEvent = utils.toSCXMLEvent({
|
12 | type: actionTypes.init
|
13 | });
|
14 | function getActionFunction(actionType, actionFunctionMap) {
|
15 | return actionFunctionMap ? actionFunctionMap[actionType] || undefined : undefined;
|
16 | }
|
17 | function toActionObject(action, actionFunctionMap) {
|
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 | };
|
35 | }
|
36 | } else if (utils.isFunction(action)) {
|
37 | actionObject = {
|
38 |
|
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;
|
56 | }
|
57 | }
|
58 |
|
59 | return actionObject;
|
60 | }
|
61 | var toActionObjects = function (action, actionFunctionMap) {
|
62 | if (!action) {
|
63 | return [];
|
64 | }
|
65 |
|
66 | var actions = utils.isArray(action) ? action : [action];
|
67 | return actions.map(function (subAction) {
|
68 | return toActionObject(subAction, actionFunctionMap);
|
69 | });
|
70 | };
|
71 | function toActivityDefinition(action) {
|
72 | var actionObject = toActionObject(action);
|
73 | return _tslib.__assign(_tslib.__assign({
|
74 | id: utils.isString(action) ? action : actionObject.id
|
75 | }, actionObject), {
|
76 | type: actionObject.type
|
77 | });
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function raise(event) {
|
87 | if (!utils.isString(event)) {
|
88 | return send(event, {
|
89 | to: types.SpecialTargets.Internal
|
90 | });
|
91 | }
|
92 |
|
93 | return {
|
94 | type: actionTypes.raise,
|
95 | event: event
|
96 | };
|
97 | }
|
98 | function resolveRaise(action) {
|
99 | return {
|
100 | type: actionTypes.raise,
|
101 | _event: utils.toSCXMLEvent(action.event)
|
102 | };
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function send(event, options) {
|
116 | return {
|
117 | to: options ? options.to : undefined,
|
118 | type: actionTypes.send,
|
119 | event: utils.isFunction(event) ? event : utils.toEventObject(event),
|
120 | delay: options ? options.delay : undefined,
|
121 | id: options && options.id !== undefined ? options.id : utils.isFunction(event) ? event.name : utils.getEventType(event)
|
122 | };
|
123 | }
|
124 | function resolveSend(action, ctx, _event, delaysMap) {
|
125 | var meta = {
|
126 | _event: _event
|
127 | };
|
128 |
|
129 | var resolvedEvent = utils.toSCXMLEvent(utils.isFunction(action.event) ? action.event(ctx, _event.data, meta) : action.event);
|
130 | var resolvedDelay;
|
131 |
|
132 | if (utils.isString(action.delay)) {
|
133 | var configDelay = delaysMap && delaysMap[action.delay];
|
134 | resolvedDelay = utils.isFunction(configDelay) ? configDelay(ctx, _event.data, meta) : configDelay;
|
135 | } else {
|
136 | resolvedDelay = utils.isFunction(action.delay) ? action.delay(ctx, _event.data, meta) : action.delay;
|
137 | }
|
138 |
|
139 | var resolvedTarget = utils.isFunction(action.to) ? action.to(ctx, _event.data, meta) : action.to;
|
140 | return _tslib.__assign(_tslib.__assign({}, action), {
|
141 | to: resolvedTarget,
|
142 | _event: resolvedEvent,
|
143 | event: resolvedEvent.data,
|
144 | delay: resolvedDelay
|
145 | });
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | function sendParent(event, options) {
|
155 | return send(event, _tslib.__assign(_tslib.__assign({}, options), {
|
156 | to: types.SpecialTargets.Parent
|
157 | }));
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function sendTo(actor, event, options) {
|
169 | return send(event, _tslib.__assign(_tslib.__assign({}, options), {
|
170 | to: actor
|
171 | }));
|
172 | }
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function sendUpdate() {
|
178 | return sendParent(actionTypes.update);
|
179 | }
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | function respond(event, options) {
|
188 | return send(event, _tslib.__assign(_tslib.__assign({}, options), {
|
189 | to: function (_, __, _a) {
|
190 | var _event = _a._event;
|
191 | return _event.origin;
|
192 | }
|
193 | }));
|
194 | }
|
195 |
|
196 | var defaultLogExpr = function (context, event) {
|
197 | return {
|
198 | context: context,
|
199 | event: event
|
200 | };
|
201 | };
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | function log(expr, label) {
|
213 | if (expr === void 0) {
|
214 | expr = defaultLogExpr;
|
215 | }
|
216 |
|
217 | return {
|
218 | type: actionTypes.log,
|
219 | label: label,
|
220 | expr: expr
|
221 | };
|
222 | }
|
223 | var resolveLog = function (action, ctx, _event) {
|
224 | return _tslib.__assign(_tslib.__assign({}, action), {
|
225 | value: utils.isString(action.expr) ? action.expr : action.expr(ctx, _event.data, {
|
226 | _event: _event
|
227 | })
|
228 | });
|
229 | };
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | var cancel = function (sendId) {
|
239 | return {
|
240 | type: actionTypes.cancel,
|
241 | sendId: sendId
|
242 | };
|
243 | };
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function start(activity) {
|
251 | var activityDef = toActivityDefinition(activity);
|
252 | return {
|
253 | type: types.ActionTypes.Start,
|
254 | activity: activityDef,
|
255 | exec: undefined
|
256 | };
|
257 | }
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | function stop(actorRef) {
|
265 | var activity = utils.isFunction(actorRef) ? actorRef : toActivityDefinition(actorRef);
|
266 | return {
|
267 | type: types.ActionTypes.Stop,
|
268 | activity: activity,
|
269 | exec: undefined
|
270 | };
|
271 | }
|
272 | function resolveStop(action, context, _event) {
|
273 | var actorRefOrString = utils.isFunction(action.activity) ? action.activity(context, _event.data) : action.activity;
|
274 | var resolvedActorRef = typeof actorRefOrString === 'string' ? {
|
275 | id: actorRefOrString
|
276 | } : actorRefOrString;
|
277 | var actionObject = {
|
278 | type: types.ActionTypes.Stop,
|
279 | activity: resolvedActorRef
|
280 | };
|
281 | return actionObject;
|
282 | }
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | var assign = function (assignment) {
|
290 | return {
|
291 | type: actionTypes.assign,
|
292 | assignment: assignment
|
293 | };
|
294 | };
|
295 | function isActionObject(action) {
|
296 | return typeof action === 'object' && 'type' in action;
|
297 | }
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | function after(delayRef, id) {
|
307 | var idSuffix = id ? "#".concat(id) : '';
|
308 | return "".concat(types.ActionTypes.After, "(").concat(delayRef, ")").concat(idSuffix);
|
309 | }
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | function done(id, data) {
|
319 | var type = "".concat(types.ActionTypes.DoneState, ".").concat(id);
|
320 | var eventObject = {
|
321 | type: type,
|
322 | data: data
|
323 | };
|
324 |
|
325 | eventObject.toString = function () {
|
326 | return type;
|
327 | };
|
328 |
|
329 | return eventObject;
|
330 | }
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | function doneInvoke(id, data) {
|
342 | var type = "".concat(types.ActionTypes.DoneInvoke, ".").concat(id);
|
343 | var eventObject = {
|
344 | type: type,
|
345 | data: data
|
346 | };
|
347 |
|
348 | eventObject.toString = function () {
|
349 | return type;
|
350 | };
|
351 |
|
352 | return eventObject;
|
353 | }
|
354 | function error(id, data) {
|
355 | var type = "".concat(types.ActionTypes.ErrorPlatform, ".").concat(id);
|
356 | var eventObject = {
|
357 | type: type,
|
358 | data: data
|
359 | };
|
360 |
|
361 | eventObject.toString = function () {
|
362 | return type;
|
363 | };
|
364 |
|
365 | return eventObject;
|
366 | }
|
367 | function pure(getActions) {
|
368 | return {
|
369 | type: types.ActionTypes.Pure,
|
370 | get: getActions
|
371 | };
|
372 | }
|
373 | /**
|
374 | * Forwards (sends) an event to a specified service.
|
375 | *
|
376 | * @param target The target service to forward the event to.
|
377 | * @param options Options to pass into the send action creator.
|
378 | */
|
379 |
|
380 | function forwardTo(target, options) {
|
381 | return send(function (_, event) {
|
382 | return event;
|
383 | }, _tslib.__assign(_tslib.__assign({}, options), {
|
384 | to: target
|
385 | }));
|
386 | }
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 | function escalate(errorData, options) {
|
396 | return sendParent(function (context, event, meta) {
|
397 | return {
|
398 | type: actionTypes.error,
|
399 | data: utils.isFunction(errorData) ? errorData(context, event, meta) : errorData
|
400 | };
|
401 | }, _tslib.__assign(_tslib.__assign({}, options), {
|
402 | to: types.SpecialTargets.Parent
|
403 | }));
|
404 | }
|
405 | function choose(conds) {
|
406 | return {
|
407 | type: types.ActionTypes.Choose,
|
408 | conds: conds
|
409 | };
|
410 | }
|
411 | function resolveActions(machine, currentState, currentContext, _event, actions, preserveActionOrder) {
|
412 | if (preserveActionOrder === void 0) {
|
413 | preserveActionOrder = false;
|
414 | }
|
415 |
|
416 | var _a = _tslib.__read(preserveActionOrder ? [[], actions] : utils.partition(actions, function (action) {
|
417 | return action.type === actionTypes.assign;
|
418 | }), 2),
|
419 | assignActions = _a[0],
|
420 | otherActions = _a[1];
|
421 |
|
422 | var updatedContext = assignActions.length ? utils.updateContext(currentContext, _event, assignActions, currentState) : currentContext;
|
423 | var preservedContexts = preserveActionOrder ? [currentContext] : undefined;
|
424 | var resolvedActions = utils.flatten(otherActions.map(function (actionObject) {
|
425 | var _a;
|
426 |
|
427 | switch (actionObject.type) {
|
428 | case actionTypes.raise:
|
429 | return resolveRaise(actionObject);
|
430 |
|
431 | case actionTypes.send:
|
432 | var sendAction = resolveSend(actionObject, updatedContext, _event, machine.options.delays);
|
433 |
|
434 | if (!environment.IS_PRODUCTION) {
|
435 |
|
436 | utils.warn(!utils.isString(actionObject.delay) || typeof sendAction.delay === 'number',
|
437 | "No delay reference for delay expression '".concat(actionObject.delay, "' was found on machine '").concat(machine.id, "'"));
|
438 | }
|
439 |
|
440 | return sendAction;
|
441 |
|
442 | case actionTypes.log:
|
443 | return resolveLog(actionObject, updatedContext, _event);
|
444 |
|
445 | case actionTypes.choose:
|
446 | {
|
447 | var chooseAction = actionObject;
|
448 | var matchedActions = (_a = chooseAction.conds.find(function (condition) {
|
449 | var guard = utils.toGuard(condition.cond, machine.options.guards);
|
450 | return !guard || utils.evaluateGuard(machine, guard, updatedContext, _event, currentState);
|
451 | })) === null || _a === void 0 ? void 0 : _a.actions;
|
452 |
|
453 | if (!matchedActions) {
|
454 | return [];
|
455 | }
|
456 |
|
457 | var _b = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(utils.toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
|
458 | resolvedActionsFromChoose = _b[0],
|
459 | resolvedContextFromChoose = _b[1];
|
460 |
|
461 | updatedContext = resolvedContextFromChoose;
|
462 | preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
|
463 | return resolvedActionsFromChoose;
|
464 | }
|
465 |
|
466 | case actionTypes.pure:
|
467 | {
|
468 | var matchedActions = actionObject.get(updatedContext, _event.data);
|
469 |
|
470 | if (!matchedActions) {
|
471 | return [];
|
472 | }
|
473 |
|
474 | var _c = _tslib.__read(resolveActions(machine, currentState, updatedContext, _event, toActionObjects(utils.toArray(matchedActions), machine.options.actions), preserveActionOrder), 2),
|
475 | resolvedActionsFromPure = _c[0],
|
476 | resolvedContext = _c[1];
|
477 |
|
478 | updatedContext = resolvedContext;
|
479 | preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
|
480 | return resolvedActionsFromPure;
|
481 | }
|
482 |
|
483 | case actionTypes.stop:
|
484 | {
|
485 | return resolveStop(actionObject, updatedContext, _event);
|
486 | }
|
487 |
|
488 | case actionTypes.assign:
|
489 | {
|
490 | updatedContext = utils.updateContext(updatedContext, _event, [actionObject], currentState);
|
491 | preservedContexts === null || preservedContexts === void 0 ? void 0 : preservedContexts.push(updatedContext);
|
492 | break;
|
493 | }
|
494 |
|
495 | default:
|
496 | var resolvedActionObject = toActionObject(actionObject, machine.options.actions);
|
497 | var exec_1 = resolvedActionObject.exec;
|
498 |
|
499 | if (exec_1 && preservedContexts) {
|
500 | var contextIndex_1 = preservedContexts.length - 1;
|
501 | resolvedActionObject = _tslib.__assign(_tslib.__assign({}, resolvedActionObject), {
|
502 | exec: function (_ctx) {
|
503 | var args = [];
|
504 |
|
505 | for (var _i = 1; _i < arguments.length; _i++) {
|
506 | args[_i - 1] = arguments[_i];
|
507 | }
|
508 |
|
509 | exec_1.apply(void 0, _tslib.__spreadArray([preservedContexts[contextIndex_1]], _tslib.__read(args), false));
|
510 | }
|
511 | });
|
512 | }
|
513 |
|
514 | return resolvedActionObject;
|
515 | }
|
516 | }).filter(function (a) {
|
517 | return !!a;
|
518 | }));
|
519 | return [resolvedActions, updatedContext];
|
520 | }
|
521 |
|
522 | exports.actionTypes = actionTypes;
|
523 | exports.after = after;
|
524 | exports.assign = assign;
|
525 | exports.cancel = cancel;
|
526 | exports.choose = choose;
|
527 | exports.done = done;
|
528 | exports.doneInvoke = doneInvoke;
|
529 | exports.error = error;
|
530 | exports.escalate = escalate;
|
531 | exports.forwardTo = forwardTo;
|
532 | exports.getActionFunction = getActionFunction;
|
533 | exports.initEvent = initEvent;
|
534 | exports.isActionObject = isActionObject;
|
535 | exports.log = log;
|
536 | exports.pure = pure;
|
537 | exports.raise = raise;
|
538 | exports.resolveActions = resolveActions;
|
539 | exports.resolveLog = resolveLog;
|
540 | exports.resolveRaise = resolveRaise;
|
541 | exports.resolveSend = resolveSend;
|
542 | exports.resolveStop = resolveStop;
|
543 | exports.respond = respond;
|
544 | exports.send = send;
|
545 | exports.sendParent = sendParent;
|
546 | exports.sendTo = sendTo;
|
547 | exports.sendUpdate = sendUpdate;
|
548 | exports.start = start;
|
549 | exports.stop = stop;
|
550 | exports.toActionObject = toActionObject;
|
551 | exports.toActionObjects = toActionObjects;
|
552 | exports.toActivityDefinition = toActivityDefinition;
|