redux-logic
Version:
Redux middleware for organizing all your business logic. Intercept actions and perform async processing.
201 lines (194 loc) • 9.17 kB
JavaScript
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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.symbol.to-primitive.js";
import "core-js/modules/es.error.cause.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.push.js";
import "core-js/modules/es.array.slice.js";
import "core-js/modules/es.date.to-primitive.js";
import "core-js/modules/es.function.name.js";
import "core-js/modules/es.number.constructor.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import "core-js/modules/es.object.keys.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.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import "core-js/modules/web.dom-collections.iterator.js";
import isPromise from 'is-promise';
import { Subject, from, of, throwError, isObservable } from 'rxjs';
import { defaultIfEmpty, tap, filter, map, mergeAll, takeUntil } from 'rxjs/operators';
import { identityFn, isInterceptAction, unwrapInterceptAction } from './utils';
var UNHANDLED_LOGIC_ERROR = 'UNHANDLED_LOGIC_ERROR';
// returns { dispatch, dispatch$, done };
export default function createDispatch(_ref) {
var action = _ref.action,
cancel$ = _ref.cancel$,
cancelled$ = _ref.cancelled$,
logic = _ref.logic,
monitor$ = _ref.monitor$,
store = _ref.store;
var name = logic.name,
_logic$processOptions = logic.processOptions,
dispatchMultiple = _logic$processOptions.dispatchMultiple,
successType = _logic$processOptions.successType,
failType = _logic$processOptions.failType;
var dispatchOps = [mergeAll(), cancel$ ? takeUntil(cancel$) : null,
// only takeUntil if cancel or latest
tap(mapToActionAndDispatch,
// next
mapErrorToActionAndDispatch // error
)].filter(identityFn);
var dispatch$ = new Subject();
dispatch$.pipe.apply(dispatch$, _toConsumableArray(dispatchOps)).subscribe({
error: function error(/* err */
) {
monitor$.next({
action: action,
name: name,
op: 'end'
});
// signalling complete here since error was dispatched
// accordingly, otherwise if we were to signal an error here
// then cancelled$ subscriptions would have to specifically
// handle error in subscribe otherwise it will throw. So
// it doesn't seem that it is worth it.
cancelled$.complete();
cancelled$.unsubscribe();
},
complete: function complete() {
monitor$.next({
action: action,
name: name,
op: 'end'
});
cancelled$.complete();
cancelled$.unsubscribe();
}
});
function storeDispatch(act) {
monitor$.next({
action: action,
dispAction: act,
op: 'dispatch'
});
return store.dispatch(act);
}
function mapToActionAndDispatch(actionOrValue) {
var act = isInterceptAction(actionOrValue) ? unwrapInterceptAction(actionOrValue) : successType ? mapToAction(successType, actionOrValue, false) : actionOrValue;
if (act) {
storeDispatch(act);
}
}
/* eslint-disable consistent-return */
function mapErrorToActionAndDispatch(actionOrValue) {
// action dispatched from intercept needs to be unwrapped and sent as is
/* istanbul ignore if */
if (isInterceptAction(actionOrValue)) {
var interceptAction = unwrapInterceptAction(actionOrValue);
return storeDispatch(interceptAction);
}
if (failType) {
// we have a failType, if truthy result we will use it
var act = mapToAction(failType, actionOrValue, true);
if (act) {
return storeDispatch(act);
}
return; // falsey result from failType, no dispatch
}
// no failType so must wrap values with no type
if (actionOrValue instanceof Error) {
var _act = actionOrValue.type ? actionOrValue :
// has type
{
type: UNHANDLED_LOGIC_ERROR,
payload: actionOrValue,
error: true
};
return storeDispatch(_act);
}
// dispatch objects or functions as is
var typeOfValue = _typeof(actionOrValue);
if (actionOrValue && (
// not null and is object | fn
typeOfValue === 'object' || typeOfValue === 'function')) {
return storeDispatch(actionOrValue);
}
// wasn't an error, obj, or fn, so we will wrap in unhandled
storeDispatch({
type: UNHANDLED_LOGIC_ERROR,
payload: actionOrValue,
error: true
});
}
/* eslint-enable consistent-return */
function mapToAction(type, payload, err) {
if (typeof type === 'function') {
// action creator fn
return type(payload);
}
var act = {
type: type,
payload: payload
};
if (err) {
act.error = true;
}
return act;
}
// allowMore is now deprecated in favor of variable process arity
// which sets processOptions.dispatchMultiple = true then
// expects done() cb to be called to end
// Might still be needed for internal use so keeping it for now
var DispatchDefaults = {
allowMore: false
};
function dispatch(act) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _applyDispatchDefault = applyDispatchDefaults(options),
allowMore = _applyDispatchDefault.allowMore;
if (typeof act !== 'undefined') {
// ignore empty action
dispatch$.next(
// create obs for mergeAll
// eslint-disable-next-line no-nested-ternary
isObservable(act) ? act : isPromise(act) ? from(act) : act instanceof Error ? throwError(act) : of(act));
}
if (!(dispatchMultiple || allowMore)) {
dispatch$.complete();
}
return act;
}
function applyDispatchDefaults(options) {
return _objectSpread(_objectSpread({}, DispatchDefaults), options);
}
function done() {
dispatch$.complete();
}
return {
dispatch: dispatch,
dispatch$: dispatch$,
done: done
};
}