Files
30-seconds-of-code/node_modules/xstate/lib/actions.js
2019-08-20 15:52:05 +02:00

304 lines
9.4 KiB
JavaScript

"use strict";
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("./types");
var actionTypes = require("./actionTypes");
exports.actionTypes = actionTypes;
var utils_1 = require("./utils");
var utils_2 = require("./utils");
exports.initEvent = { type: actionTypes.init };
function toEventObject(event, payload
// id?: TEvent['type']
) {
if (utils_1.isString(event) || typeof event === 'number') {
var eventObject = { type: event };
if (payload) {
Object.assign(eventObject, payload);
}
return eventObject;
}
return event;
}
exports.toEventObject = toEventObject;
function getActionFunction(actionType, actionFunctionMap) {
return actionFunctionMap
? actionFunctionMap[actionType] || undefined
: undefined;
}
exports.getActionFunction = getActionFunction;
function toActionObject(action, actionFunctionMap) {
var actionObject;
if (utils_1.isString(action) || typeof action === 'number') {
var exec = getActionFunction(action, actionFunctionMap);
if (utils_1.isFunction(exec)) {
actionObject = {
type: action,
exec: exec
};
}
else if (exec) {
actionObject = exec;
}
else {
actionObject = { type: action, exec: undefined };
}
}
else if (utils_1.isFunction(action)) {
actionObject = {
// Convert action to string if unnamed
type: action.name || action.toString(),
exec: action
};
}
else {
var exec = getActionFunction(action.type, actionFunctionMap);
if (utils_1.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;
}
exports.toActionObject = toActionObject;
exports.toActionObjects = function (action, actionFunctionMap) {
if (!action) {
return [];
}
var actions = utils_2.isArray(action) ? action : [action];
return actions.map(function (subAction) { return toActionObject(subAction, actionFunctionMap); });
};
function toActivityDefinition(action) {
var actionObject = toActionObject(action);
return __assign({ id: utils_1.isString(action) ? action : actionObject.id }, actionObject, { type: actionObject.type });
}
exports.toActivityDefinition = toActivityDefinition;
/**
* 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.
*/
function raise(event) {
return {
type: actionTypes.raise,
event: event
};
}
exports.raise = raise;
/**
* 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).
*/
function send(event, options) {
return {
to: options ? options.to : undefined,
type: actionTypes.send,
event: utils_1.isFunction(event) ? event : toEventObject(event),
delay: options ? options.delay : undefined,
id: options && options.id !== undefined
? options.id
: utils_1.isFunction(event)
? event.name
: utils_1.getEventType(event)
};
}
exports.send = send;
function resolveSend(action, ctx, event) {
// TODO: helper function for resolving Expr
var resolvedEvent = utils_1.isFunction(action.event)
? toEventObject(action.event(ctx, event))
: toEventObject(action.event);
var resolvedDelay = utils_1.isFunction(action.delay)
? action.delay(ctx, event)
: action.delay;
var resolvedTarget = utils_1.isFunction(action.to)
? action.to(ctx, event)
: action.to;
return __assign({}, action, { to: resolvedTarget, event: resolvedEvent, delay: resolvedDelay });
}
exports.resolveSend = resolveSend;
/**
* 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.
*/
function sendParent(event, options) {
return send(event, __assign({}, options, { to: types_1.SpecialTargets.Parent }));
}
exports.sendParent = sendParent;
/**
*
* @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.
*/
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
};
}
exports.log = log;
/**
* 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.
*/
exports.cancel = function (sendId) {
return {
type: actionTypes.cancel,
sendId: sendId
};
};
/**
* Starts an activity.
*
* @param activity The activity to start.
*/
function start(activity) {
var activityDef = toActivityDefinition(activity);
return {
type: types_1.ActionTypes.Start,
activity: activityDef,
exec: undefined
};
}
exports.start = start;
/**
* Stops an activity.
*
* @param activity The activity to stop.
*/
function stop(activity) {
var activityDef = toActivityDefinition(activity);
return {
type: types_1.ActionTypes.Stop,
activity: activityDef,
exec: undefined
};
}
exports.stop = stop;
/**
* Updates the current context of the machine.
*
* @param assignment An object that represents the partial context to update.
*/
exports.assign = function (assignment) {
return {
type: actionTypes.assign,
assignment: assignment
};
};
function isActionObject(action) {
return typeof action === 'object' && 'type' in action;
}
exports.isActionObject = isActionObject;
/**
* 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
*/
function after(delayRef, id) {
var idSuffix = id ? "#" + id : '';
return types_1.ActionTypes.After + "(" + delayRef + ")" + idSuffix;
}
exports.after = after;
/**
* 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
*/
function done(id, data) {
var type = types_1.ActionTypes.DoneState + "." + id;
var eventObject = {
type: type,
data: data
};
eventObject.toString = function () { return type; };
return eventObject;
}
exports.done = done;
/**
* 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
*/
function doneInvoke(id, data) {
var type = types_1.ActionTypes.DoneInvoke + "." + id;
var eventObject = {
type: type,
data: data
};
eventObject.toString = function () { return type; };
return eventObject;
}
exports.doneInvoke = doneInvoke;
function error(id, data) {
var type = types_1.ActionTypes.ErrorPlatform + "." + id;
var eventObject = { type: type, data: data };
eventObject.toString = function () { return type; };
return eventObject;
}
exports.error = error;
function pure(getActions) {
return {
type: types_1.ActionTypes.Pure,
get: getActions
};
}
exports.pure = pure;
//# sourceMappingURL=actions.js.map