532 lines
16 KiB
JavaScript
532 lines
16 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 __values = (this && this.__values) || function (o) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
|
|
if (m) return m.call(o);
|
|
return {
|
|
next: function () {
|
|
if (o && i >= o.length) o = void 0;
|
|
return { value: o && o[i++], done: !o };
|
|
}
|
|
};
|
|
};
|
|
var __read = (this && this.__read) || function (o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m) return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
}
|
|
catch (error) { e = { error: error }; }
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
}
|
|
finally { if (e) throw e.error; }
|
|
}
|
|
return ar;
|
|
};
|
|
var __spread = (this && this.__spread) || function () {
|
|
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
return ar;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var types_1 = require("./types");
|
|
var constants_1 = require("./constants");
|
|
var environment_1 = require("./environment");
|
|
function isState(state) {
|
|
if (isString(state)) {
|
|
return false;
|
|
}
|
|
return 'value' in state && 'tree' in state && 'history' in state;
|
|
}
|
|
function keys(value) {
|
|
return Object.keys(value);
|
|
}
|
|
exports.keys = keys;
|
|
function matchesState(parentStateId, childStateId, delimiter) {
|
|
if (delimiter === void 0) { delimiter = constants_1.STATE_DELIMITER; }
|
|
var parentStateValue = toStateValue(parentStateId, delimiter);
|
|
var childStateValue = toStateValue(childStateId, delimiter);
|
|
if (isString(childStateValue)) {
|
|
if (isString(parentStateValue)) {
|
|
return childStateValue === parentStateValue;
|
|
}
|
|
// Parent more specific than child
|
|
return false;
|
|
}
|
|
if (isString(parentStateValue)) {
|
|
return parentStateValue in childStateValue;
|
|
}
|
|
return keys(parentStateValue).every(function (key) {
|
|
if (!(key in childStateValue)) {
|
|
return false;
|
|
}
|
|
return matchesState(parentStateValue[key], childStateValue[key]);
|
|
});
|
|
}
|
|
exports.matchesState = matchesState;
|
|
function getEventType(event) {
|
|
try {
|
|
return isString(event) || typeof event === 'number'
|
|
? "" + event
|
|
: event.type;
|
|
}
|
|
catch (e) {
|
|
throw new Error('Events must be strings or objects with a string event.type property.');
|
|
}
|
|
}
|
|
exports.getEventType = getEventType;
|
|
function getActionType(action) {
|
|
try {
|
|
return isString(action) || typeof action === 'number'
|
|
? "" + action
|
|
: isFunction(action)
|
|
? action.name
|
|
: action.type;
|
|
}
|
|
catch (e) {
|
|
throw new Error('Actions must be strings or objects with a string action.type property.');
|
|
}
|
|
}
|
|
exports.getActionType = getActionType;
|
|
function toStatePath(stateId, delimiter) {
|
|
try {
|
|
if (isArray(stateId)) {
|
|
return stateId;
|
|
}
|
|
return stateId.toString().split(delimiter);
|
|
}
|
|
catch (e) {
|
|
throw new Error("'" + stateId + "' is not a valid state path.");
|
|
}
|
|
}
|
|
exports.toStatePath = toStatePath;
|
|
function toStateValue(stateValue, delimiter) {
|
|
if (isState(stateValue)) {
|
|
return stateValue.value;
|
|
}
|
|
if (isArray(stateValue)) {
|
|
return pathToStateValue(stateValue);
|
|
}
|
|
if (typeof stateValue !== 'string' && !isState(stateValue)) {
|
|
return stateValue;
|
|
}
|
|
var statePath = toStatePath(stateValue, delimiter);
|
|
return pathToStateValue(statePath);
|
|
}
|
|
exports.toStateValue = toStateValue;
|
|
function pathToStateValue(statePath) {
|
|
if (statePath.length === 1) {
|
|
return statePath[0];
|
|
}
|
|
var value = {};
|
|
var marker = value;
|
|
for (var i = 0; i < statePath.length - 1; i++) {
|
|
if (i === statePath.length - 2) {
|
|
marker[statePath[i]] = statePath[i + 1];
|
|
}
|
|
else {
|
|
marker[statePath[i]] = {};
|
|
marker = marker[statePath[i]];
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
exports.pathToStateValue = pathToStateValue;
|
|
function mapValues(collection, iteratee) {
|
|
var result = {};
|
|
var collectionKeys = keys(collection);
|
|
for (var i = 0; i < collectionKeys.length; i++) {
|
|
var key = collectionKeys[i];
|
|
result[key] = iteratee(collection[key], key, collection, i);
|
|
}
|
|
return result;
|
|
}
|
|
exports.mapValues = mapValues;
|
|
function mapFilterValues(collection, iteratee, predicate) {
|
|
var e_1, _a;
|
|
var result = {};
|
|
try {
|
|
for (var _b = __values(keys(collection)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var key = _c.value;
|
|
var item = collection[key];
|
|
if (!predicate(item)) {
|
|
continue;
|
|
}
|
|
result[key] = iteratee(item, key, collection);
|
|
}
|
|
}
|
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_1) throw e_1.error; }
|
|
}
|
|
return result;
|
|
}
|
|
exports.mapFilterValues = mapFilterValues;
|
|
/**
|
|
* Retrieves a value at the given path.
|
|
* @param props The deep path to the prop of the desired value
|
|
*/
|
|
exports.path = function (props) { return function (object) {
|
|
var e_2, _a;
|
|
var result = object;
|
|
try {
|
|
for (var props_1 = __values(props), props_1_1 = props_1.next(); !props_1_1.done; props_1_1 = props_1.next()) {
|
|
var prop = props_1_1.value;
|
|
result = result[prop];
|
|
}
|
|
}
|
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
finally {
|
|
try {
|
|
if (props_1_1 && !props_1_1.done && (_a = props_1.return)) _a.call(props_1);
|
|
}
|
|
finally { if (e_2) throw e_2.error; }
|
|
}
|
|
return result;
|
|
}; };
|
|
/**
|
|
* Retrieves a value at the given path via the nested accessor prop.
|
|
* @param props The deep path to the prop of the desired value
|
|
*/
|
|
function nestedPath(props, accessorProp) {
|
|
return function (object) {
|
|
var e_3, _a;
|
|
var result = object;
|
|
try {
|
|
for (var props_2 = __values(props), props_2_1 = props_2.next(); !props_2_1.done; props_2_1 = props_2.next()) {
|
|
var prop = props_2_1.value;
|
|
result = result[accessorProp][prop];
|
|
}
|
|
}
|
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
finally {
|
|
try {
|
|
if (props_2_1 && !props_2_1.done && (_a = props_2.return)) _a.call(props_2);
|
|
}
|
|
finally { if (e_3) throw e_3.error; }
|
|
}
|
|
return result;
|
|
};
|
|
}
|
|
exports.nestedPath = nestedPath;
|
|
function toStatePaths(stateValue) {
|
|
if (!stateValue) {
|
|
return [[]];
|
|
}
|
|
if (isString(stateValue)) {
|
|
return [[stateValue]];
|
|
}
|
|
var result = flatten(keys(stateValue).map(function (key) {
|
|
var subStateValue = stateValue[key];
|
|
if (typeof subStateValue !== 'string' &&
|
|
(!subStateValue || !Object.keys(subStateValue).length)) {
|
|
return [[key]];
|
|
}
|
|
return toStatePaths(stateValue[key]).map(function (subPath) {
|
|
return [key].concat(subPath);
|
|
});
|
|
}));
|
|
return result;
|
|
}
|
|
exports.toStatePaths = toStatePaths;
|
|
exports.pathsToStateValue = function (paths) {
|
|
var e_4, _a;
|
|
var result = {};
|
|
if (paths && paths.length === 1 && paths[0].length === 1) {
|
|
return paths[0][0];
|
|
}
|
|
try {
|
|
for (var paths_1 = __values(paths), paths_1_1 = paths_1.next(); !paths_1_1.done; paths_1_1 = paths_1.next()) {
|
|
var currentPath = paths_1_1.value;
|
|
var marker = result;
|
|
// tslint:disable-next-line:prefer-for-of
|
|
for (var i = 0; i < currentPath.length; i++) {
|
|
var subPath = currentPath[i];
|
|
if (i === currentPath.length - 2) {
|
|
marker[subPath] = currentPath[i + 1];
|
|
break;
|
|
}
|
|
marker[subPath] = marker[subPath] || {};
|
|
marker = marker[subPath];
|
|
}
|
|
}
|
|
}
|
|
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
finally {
|
|
try {
|
|
if (paths_1_1 && !paths_1_1.done && (_a = paths_1.return)) _a.call(paths_1);
|
|
}
|
|
finally { if (e_4) throw e_4.error; }
|
|
}
|
|
return result;
|
|
};
|
|
function flatten(array) {
|
|
var _a;
|
|
return (_a = []).concat.apply(_a, __spread(array));
|
|
}
|
|
exports.flatten = flatten;
|
|
function toArray(value) {
|
|
if (isArray(value)) {
|
|
return value;
|
|
}
|
|
if (value === undefined) {
|
|
return [];
|
|
}
|
|
return [value];
|
|
}
|
|
exports.toArray = toArray;
|
|
function mapContext(mapper, context, event) {
|
|
var e_5, _a;
|
|
if (isFunction(mapper)) {
|
|
return mapper(context, event);
|
|
}
|
|
var result = {};
|
|
try {
|
|
for (var _b = __values(keys(mapper)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var key = _c.value;
|
|
var subMapper = mapper[key];
|
|
if (isFunction(subMapper)) {
|
|
result[key] = subMapper(context, event);
|
|
}
|
|
else {
|
|
result[key] = subMapper;
|
|
}
|
|
}
|
|
}
|
|
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_5) throw e_5.error; }
|
|
}
|
|
return result;
|
|
}
|
|
exports.mapContext = mapContext;
|
|
function isBuiltInEvent(eventType) {
|
|
// check if event is a "done" event
|
|
if (eventType.indexOf(types_1.ActionTypes.DoneState) === 0 ||
|
|
eventType.indexOf(types_1.ActionTypes.DoneInvoke) === 0) {
|
|
return true;
|
|
}
|
|
// check if event is an "error" event
|
|
if (eventType === types_1.ActionTypes.ErrorCommunication ||
|
|
eventType === types_1.ActionTypes.ErrorExecution ||
|
|
eventType.indexOf(types_1.ActionTypes.ErrorPlatform) === 0) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
exports.isBuiltInEvent = isBuiltInEvent;
|
|
function isPromiseLike(value) {
|
|
if (value instanceof Promise) {
|
|
return true;
|
|
}
|
|
// Check if shape matches the Promise/A+ specification for a "thenable".
|
|
if (value !== null &&
|
|
(isFunction(value) || typeof value === 'object') &&
|
|
isFunction(value.then)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
exports.isPromiseLike = isPromiseLike;
|
|
function partition(items, predicate) {
|
|
var e_6, _a;
|
|
var _b = __read([[], []], 2), truthy = _b[0], falsy = _b[1];
|
|
try {
|
|
for (var items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
var item = items_1_1.value;
|
|
if (predicate(item)) {
|
|
truthy.push(item);
|
|
}
|
|
else {
|
|
falsy.push(item);
|
|
}
|
|
}
|
|
}
|
|
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
finally {
|
|
try {
|
|
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
}
|
|
finally { if (e_6) throw e_6.error; }
|
|
}
|
|
return [truthy, falsy];
|
|
}
|
|
exports.partition = partition;
|
|
function updateHistoryStates(hist, stateValue) {
|
|
return mapValues(hist.states, function (subHist, key) {
|
|
if (!subHist) {
|
|
return undefined;
|
|
}
|
|
var subStateValue = (isString(stateValue) ? undefined : stateValue[key]) ||
|
|
(subHist ? subHist.current : undefined);
|
|
if (!subStateValue) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
current: subStateValue,
|
|
states: updateHistoryStates(subHist, subStateValue)
|
|
};
|
|
});
|
|
}
|
|
exports.updateHistoryStates = updateHistoryStates;
|
|
function updateHistoryValue(hist, stateValue) {
|
|
return {
|
|
current: stateValue,
|
|
states: updateHistoryStates(hist, stateValue)
|
|
};
|
|
}
|
|
exports.updateHistoryValue = updateHistoryValue;
|
|
function updateContext(context, event, assignActions) {
|
|
var updatedContext = context
|
|
? assignActions.reduce(function (acc, assignAction) {
|
|
var e_7, _a;
|
|
var assignment = assignAction.assignment;
|
|
var partialUpdate = {};
|
|
if (isFunction(assignment)) {
|
|
partialUpdate = assignment(acc, event || { type: types_1.ActionTypes.Init });
|
|
}
|
|
else {
|
|
try {
|
|
for (var _b = __values(keys(assignment)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
var key = _c.value;
|
|
var propAssignment = assignment[key];
|
|
partialUpdate[key] = isFunction(propAssignment)
|
|
? propAssignment(acc, event)
|
|
: propAssignment;
|
|
}
|
|
}
|
|
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
finally {
|
|
try {
|
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
}
|
|
finally { if (e_7) throw e_7.error; }
|
|
}
|
|
}
|
|
return Object.assign({}, acc, partialUpdate);
|
|
}, context)
|
|
: context;
|
|
return updatedContext;
|
|
}
|
|
exports.updateContext = updateContext;
|
|
function bindActionToState(action, state) {
|
|
var exec = action.exec;
|
|
var boundAction = __assign({}, action, { exec: exec !== undefined
|
|
? function () {
|
|
return exec(state.context, state.event, {
|
|
action: action,
|
|
state: state
|
|
});
|
|
}
|
|
: undefined });
|
|
return boundAction;
|
|
}
|
|
exports.bindActionToState = bindActionToState;
|
|
// tslint:disable-next-line:no-empty
|
|
var warn = function () { };
|
|
exports.warn = warn;
|
|
if (!environment_1.IS_PRODUCTION) {
|
|
exports.warn = warn = function (condition, message) {
|
|
var error = condition instanceof Error ? condition : undefined;
|
|
if (!error && condition) {
|
|
return;
|
|
}
|
|
if (console !== undefined) {
|
|
var args = ["Warning: " + message];
|
|
if (error) {
|
|
args.push(error);
|
|
}
|
|
// tslint:disable-next-line:no-console
|
|
console.warn.apply(console, args);
|
|
}
|
|
};
|
|
}
|
|
function isArray(value) {
|
|
return Array.isArray(value);
|
|
}
|
|
exports.isArray = isArray;
|
|
// tslint:disable-next-line:ban-types
|
|
function isFunction(value) {
|
|
return typeof value === 'function';
|
|
}
|
|
exports.isFunction = isFunction;
|
|
function isString(value) {
|
|
return typeof value === 'string';
|
|
}
|
|
exports.isString = isString;
|
|
// export function memoizedGetter<T, TP extends { prototype: object }>(
|
|
// o: TP,
|
|
// property: string,
|
|
// getter: () => T
|
|
// ): void {
|
|
// Object.defineProperty(o.prototype, property, {
|
|
// get: getter,
|
|
// enumerable: false,
|
|
// configurable: false
|
|
// });
|
|
// }
|
|
function toGuard(condition, guardMap) {
|
|
if (!condition) {
|
|
return undefined;
|
|
}
|
|
if (isString(condition)) {
|
|
return {
|
|
type: constants_1.DEFAULT_GUARD_TYPE,
|
|
name: condition,
|
|
predicate: guardMap ? guardMap[condition] : undefined
|
|
};
|
|
}
|
|
if (isFunction(condition)) {
|
|
return {
|
|
type: constants_1.DEFAULT_GUARD_TYPE,
|
|
name: condition.name,
|
|
predicate: condition
|
|
};
|
|
}
|
|
return condition;
|
|
}
|
|
exports.toGuard = toGuard;
|
|
function isObservable(value) {
|
|
try {
|
|
return 'subscribe' in value && isFunction(value.subscribe);
|
|
}
|
|
catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
exports.isObservable = isObservable;
|
|
function isMachine(value) {
|
|
try {
|
|
return '__xstatenode' in value;
|
|
}
|
|
catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
exports.isMachine = isMachine;
|
|
exports.uniqueId = (function () {
|
|
var currentId = 0;
|
|
return function () {
|
|
currentId++;
|
|
return currentId.toString(16);
|
|
};
|
|
})();
|
|
//# sourceMappingURL=utils.js.map
|