UNPKG

movex-core-util

Version:

Movex Core Util is the library of utilities for Movex

59 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toMasterActionFromActionOrTuple = exports.toMasterAction = exports.isMasterAction = exports.isAction = exports.createActionCreator = exports.createAction = void 0; const misc_1 = require("./misc"); /** * Flux standard action factory * @example * const clearTodos = action('[Todo] truncate'); * @example * const fetchTodosRejected = (payload: Error) => action('[Todo] fetch rejected', payload); * @example * const addTodo = ({ name, completed = false }: Todo) => action('[Todo] add', { name, completed }); * @example * const fetchTodosRejected = (payload: Error, meta?: Meta) => action('[Todo] fetch rejected', payload, meta); * @example * const addTodo = ({ name, completed = false }: Todo, meta?: Meta) => action('[Todo] add', { name, completed }, meta); */ function createAction(type, payload) { return Object.assign(Object.assign({ type }, (payload !== undefined ? { payload } : {})), (payload instanceof Error ? { error: true } : {})); } exports.createAction = createAction; function createActionCreator(type, executor = (resolve) => (() => resolve())) { const callable = executor((payload) => createAction(type, payload)); return Object.assign(callable, { type, toString() { return type; }, }); } exports.createActionCreator = createActionCreator; const isAction = (a) => { // TODO: This isn't a super thorough check, but if the input is limited // to only action or ActionTuple should be enough return (0, misc_1.isObject)(a) && (0, misc_1.keyInObject)(a, 'type'); }; exports.isAction = isAction; const isMasterAction = (a) => { // TODO: This isn't a super thorough check, but if the input is limited // to only action or ActionTuple should be enough return (0, exports.isAction)(a) && (0, misc_1.keyInObject)(a, '_isMaster') && a._isMaster === true; }; exports.isMasterAction = isMasterAction; const toMasterAction = (action) => (Object.assign(Object.assign({}, action), { _isMaster: true })); exports.toMasterAction = toMasterAction; const toMasterActionFromActionOrTuple = (actionOrActionTuple) => { return ((0, exports.isAction)(actionOrActionTuple) ? (0, exports.toMasterAction)(actionOrActionTuple) : // TODO: In the case of a tuple, there is not enough information // so I'm just making them both a masterAction, but I should only make the one that is needed actually // - this isn't the worst possible thing as if there's nothing to parse nothing will parse // and the client will simply attempt to run again with the same [ (0, exports.toMasterAction)(actionOrActionTuple[0]), (0, exports.toMasterAction)(actionOrActionTuple[1]), ]); }; exports.toMasterActionFromActionOrTuple = toMasterActionFromActionOrTuple; //# sourceMappingURL=action.js.map