var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; }; import { ActionTypes, SpecialTargets } from './types'; import * as actionTypes from './actionTypes'; import { getEventType, isFunction, isString } from './utils'; import { isArray } from './utils'; export { actionTypes }; export var initEvent = { type: actionTypes.init }; export function toEventObject(event, payload // id?: TEvent['type'] ) { if (isString(event) || typeof event === 'number') { var eventObject = { type: event }; if (payload) { Object.assign(eventObject, payload); } return eventObject; } return event; } export function getActionFunction(actionType, actionFunctionMap) { return actionFunctionMap ? actionFunctionMap[actionType] || undefined : undefined; } export function toActionObject(action, actionFunctionMap) { var actionObject; if (isString(action) || typeof action === 'number') { var exec = getActionFunction(action, actionFunctionMap); if (isFunction(exec)) { actionObject = { type: action, exec: exec }; } else if (exec) { actionObject = exec; } else { actionObject = { type: action, exec: undefined }; } } else if (isFunction(action)) { actionObject = { // Convert action to string if unnamed type: action.name || action.toString(), exec: action }; } else { var exec = getActionFunction(action.type, actionFunctionMap); if (isFunction(exec)) { actionObject = __assign({}, action, { exec: exec }); } else if (exec) { var type = action.type, other = __rest(action, ["type"]); actionObject = __assign({ type: type }, exec, other); } else { actionObject = action; } } Object.defineProperty(actionObject, 'toString', { value: function () { return actionObject.type; }, enumerable: false, configurable: true }); return actionObject; } export var toActionObjects = function (action, actionFunctionMap) { if (!action) { return []; } var actions = isArray(action) ? action : [action]; return actions.map(function (subAction) { return toActionObject(subAction, actionFunctionMap); }); }; export function toActivityDefinition(action) { var actionObject = toActionObject(action); return __assign({ id: isString(action) ? action : actionObject.id }, actionObject, { type: actionObject.type }); } /** * Raises an event. This places the event in the internal event queue, so that * the event is immediately consumed by the machine in the current step. * * @param eventType The event to raise. */ export function raise(event) { return { type: actionTypes.raise, event: event }; } /** * Sends an event. This returns an action that will be read by an interpreter to * send the event in the next step, after the current step is finished executing. * * @param event The event to send. * @param options Options to pass into the send event: * - `id` - The unique send event identifier (used with `cancel()`). * - `delay` - The number of milliseconds to delay the sending of the event. * - `target` - The target of this event (by default, the machine the event was sent from). */ export function send(event, options) { return { to: options ? options.to : undefined, type: actionTypes.send, event: isFunction(event) ? event : toEventObject(event), delay: options ? options.delay : undefined, id: options && options.id !== undefined ? options.id : isFunction(event) ? event.name : getEventType(event) }; } export function resolveSend(action, ctx, event) { // TODO: helper function for resolving Expr var resolvedEvent = isFunction(action.event) ? toEventObject(action.event(ctx, event)) : toEventObject(action.event); var resolvedDelay = isFunction(action.delay) ? action.delay(ctx, event) : action.delay; var resolvedTarget = isFunction(action.to) ? action.to(ctx, event) : action.to; return __assign({}, action, { to: resolvedTarget, event: resolvedEvent, delay: resolvedDelay }); } /** * Sends an event to this machine's parent machine. * * @param event The event to send to the parent machine. * @param options Options to pass into the send event. */ export function sendParent(event, options) { return send(event, __assign({}, options, { to: SpecialTargets.Parent })); } /** * * @param expr The expression function to evaluate which will be logged. * Takes in 2 arguments: * - `ctx` - the current state context * - `event` - the event that caused this action to be executed. * @param label The label to give to the logged expression. */ export function log(expr, label) { if (expr === void 0) { expr = function (context, event) { return { context: context, event: event }; }; } return { type: actionTypes.log, label: label, expr: expr }; } /** * Cancels an in-flight `send(...)` action. A canceled sent action will not * be executed, nor will its event be sent, unless it has already been sent * (e.g., if `cancel(...)` is called after the `send(...)` action's `delay`). * * @param sendId The `id` of the `send(...)` action to cancel. */ export var cancel = function (sendId) { return { type: actionTypes.cancel, sendId: sendId }; }; /** * Starts an activity. * * @param activity The activity to start. */ export function start(activity) { var activityDef = toActivityDefinition(activity); return { type: ActionTypes.Start, activity: activityDef, exec: undefined }; } /** * Stops an activity. * * @param activity The activity to stop. */ export function stop(activity) { var activityDef = toActivityDefinition(activity); return { type: ActionTypes.Stop, activity: activityDef, exec: undefined }; } /** * Updates the current context of the machine. * * @param assignment An object that represents the partial context to update. */ export var assign = function (assignment) { return { type: actionTypes.assign, assignment: assignment }; }; export function isActionObject(action) { return typeof action === 'object' && 'type' in action; } /** * Returns an event type that represents an implicit event that * is sent after the specified `delay`. * * @param delayRef The delay in milliseconds * @param id The state node ID where this event is handled */ export function after(delayRef, id) { var idSuffix = id ? "#" + id : ''; return ActionTypes.After + "(" + delayRef + ")" + idSuffix; } /** * Returns an event that represents that a final state node * has been reached in the parent state node. * * @param id The final state node's parent state node `id` * @param data The data to pass into the event */ export function done(id, data) { var type = ActionTypes.DoneState + "." + id; var eventObject = { type: type, data: data }; eventObject.toString = function () { return type; }; return eventObject; } /** * Returns an event that represents that an invoked service has terminated. * * An invoked service is terminated when it has reached a top-level final state node, * but not when it is canceled. * * @param id The final state node ID * @param data The data to pass into the event */ export function doneInvoke(id, data) { var type = ActionTypes.DoneInvoke + "." + id; var eventObject = { type: type, data: data }; eventObject.toString = function () { return type; }; return eventObject; } export function error(id, data) { var type = ActionTypes.ErrorPlatform + "." + id; var eventObject = { type: type, data: data }; eventObject.toString = function () { return type; }; return eventObject; } export function pure(getActions) { return { type: ActionTypes.Pure, get: getActions }; } //# sourceMappingURL=actions.js.map