UNPKG

redux-logic

Version:

Redux middleware for organizing all your business logic. Intercept actions and perform async processing.

163 lines (159 loc) 6.9 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } import "core-js/modules/es.symbol.js"; import "core-js/modules/es.symbol.description.js"; import "core-js/modules/es.symbol.iterator.js"; import "core-js/modules/es.error.cause.js"; import "core-js/modules/es.array.concat.js"; import "core-js/modules/es.array.filter.js"; import "core-js/modules/es.array.from.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/es.array.slice.js"; import "core-js/modules/es.function.name.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.regexp.exec.js"; import "core-js/modules/es.regexp.test.js"; import "core-js/modules/es.regexp.to-string.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/esnext.iterator.constructor.js"; import "core-js/modules/esnext.iterator.filter.js"; import "core-js/modules/esnext.iterator.some.js"; import "core-js/modules/web.dom-collections.iterator.js"; import { Observable, merge, asapScheduler } from 'rxjs'; import { debounceTime, filter, map, mergeMap, share, tap, throttleTime } from 'rxjs/operators'; import createLogicAction$ from './createLogicAction$'; import { identityFn } from './utils'; import createDispatch from './createDispatch'; import execProcessFn from './execProcessFn'; import createCancelled$ from './createCancelled$'; import createDepObject from './createDepObject'; var MATCH_ALL_TYPES = '*'; export default function logicWrapper(logic, store, deps, monitor$) { var name = logic.name, type = logic.type, cancelType = logic.cancelType, latest = logic.latest, debounce = logic.debounce, throttle = logic.throttle, processFn = logic.process, dispatchReturn = logic.processOptions.dispatchReturn; var getState = store.getState; // cancel on cancelType or if take latest specified var cancelTypes = [].concat(type && latest ? type : []).concat(cancelType || []); return function wrappedLogic(actionIn$) { // we want to share the same copy amongst all here var action$ = actionIn$.pipe(share()); var cancel$ = cancelTypes.length ? action$.pipe(filter(function (action) { return matchesType(cancelTypes, action.type); })) : null; var hasIntercept = logic.validate || logic.transform; // shortcut optimization if no intercept let action fall through // and just exec the processFn var mergeMapOrTap = hasIntercept ? mergeMap(function (action) { return createLogicAction$({ action: action, logic: logic, store: store, deps: deps, cancel$: cancel$, monitor$: monitor$, action$: action$ }); }) : tap(function (action) { // mimic the events as if went through createLogicAction$ // also in createLogicAction$ monitor$.next({ action: action, name: name, op: 'begin' }); monitor$.next({ action: action, nextAction: action, name: name, shouldProcess: true, op: 'next' }); var _createCancelled$ = createCancelled$({ action: action, cancel$: cancel$, monitor$: monitor$, logic: logic }), cancelled$ = _createCancelled$.cancelled$, setInterceptComplete = _createCancelled$.setInterceptComplete; var _createDispatch = createDispatch({ action: action, cancel$: cancel$, cancelled$: cancelled$, logic: logic, monitor$: monitor$, store: store }), dispatch = _createDispatch.dispatch, dispatch$ = _createDispatch.dispatch$, done = _createDispatch.done; var ctx = {}; // no intercept, so empty ctx; var depObj = createDepObject({ deps: deps, cancelled$: cancelled$, ctx: ctx, getState: getState, action: action, action$: action$ }); asapScheduler.schedule(function () { setInterceptComplete(); execProcessFn({ depObj: depObj, dispatch: dispatch, dispatch$: dispatch$, dispatchReturn: dispatchReturn, done: done, name: name, processFn: processFn }); }); }); var matchingOps = [ // operations to perform, falsey filtered out filter(function (action) { return matchesType(type, action.type); }), debounce ? debounceTime(debounce) : null, throttle ? throttleTime(throttle) : null, mergeMapOrTap].filter(identityFn); var matchingAction$ = action$.pipe.apply(action$, _toConsumableArray(matchingOps)); // shortcut optimization // if type is match all '*', then no need to create other side of pipe if (type === MATCH_ALL_TYPES) { return matchingAction$; } // types that don't match will bypass this logic var nonMatchingAction$ = action$.pipe(filter(function (action) { return !matchesType(type, action.type); })); return merge(nonMatchingAction$, matchingAction$); }; } function matchesType(tStrArrRe, type) { /* istanbul ignore if */ if (!tStrArrRe) { return false; } // nothing matches none if (_typeof(tStrArrRe) === 'symbol') { return tStrArrRe === type; } if (typeof tStrArrRe === 'string') { return tStrArrRe === type || tStrArrRe === MATCH_ALL_TYPES; } if (Array.isArray(tStrArrRe)) { return tStrArrRe.some(function (x) { return matchesType(x, type); }); } // else assume it is a RegExp return tStrArrRe.test(type); }