1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 13x 13x 13x 13x 13x 13x 28x 28x 28x 3x 3x 13x 13x 13x 34x 34x 34x 34x 33x 33x 33x 30x 29x 29x 21x 8x 3x 3x 5x 5x 5x 5x 1x 1x 28x 28x 28x | "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPromise = createPromise; exports.default = promise; exports.ActionTypes = void 0; var _isPromise = _interopRequireDefault(require("./isPromise")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { Iif (!_n && _i["return"] != null) _i["return"](); } finally { Eif (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { Iif (Array.isArray(arr)) return arr; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); Eif (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * @public ActionType * Promise types */ var DefaultActionTypes = { Pending: 'PENDING', Fulfilled: 'FULFILLED', Rejected: 'REJECTED' }; exports.ActionTypes = DefaultActionTypes; var DefaultMiddlewareConfig = { types: DefaultActionTypes, typeDelimiter: '_' /** * @public createPromise * @tutorial Get started [with an introduction to the middleware on GitBook](https://docs.psb.codes/redux-promise-middleware/getting-started/introduction). * @remarks For bug reports and feature requests, [file an issue on GitHub](https://github.com/pburtchaell/redux-promise-middleware/issues/new). * For help, [ask a question on StackOverflow](https://stackoverflow.com/questions/tagged/redux-promise-middleware). * @returns The Pr */ }; function createPromise() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DefaultMiddlewareConfig; var PROMISE_TYPE_SUFFIXES = config.types; var PROMISE_TYPE_DELIMITER = config.typeDelimiter; return function (_ref) { var dispatch = _ref.dispatch; return function (next) { return function (action) { /** * Instantiate variables to hold: * (1) the promise * (2) the data for optimistic updates */ var promiseFromAction; var data; /** * There are multiple ways to dispatch a promise. The first step is to * determine if the promise is defined: * * (a) explicitly (action.payload.promise is the promise) * (b) implicitly (action.payload is the promise) * (c) as an async function (returns a promise when called) * * If the promise is not defined in one of these three ways, we don't do * anything and move on to the next middleware in the middleware chain. */ // Step 1a: Is there a payload? if (action.payload) { var PAYLOAD = action.payload; // Step 1.1: Is the promise implicitly defined? if ((0, _isPromise.default)(PAYLOAD)) { promiseFromAction = PAYLOAD; } // Step 1.2: Is the promise explicitly defined? else if ((0, _isPromise.default)(PAYLOAD.promise)) { promiseFromAction = PAYLOAD.promise; data = PAYLOAD.data; } // Step 1.3: Is the promise returned by an async function? else Eif (typeof PAYLOAD === 'function' || typeof PAYLOAD.promise === 'function') { promiseFromAction = PAYLOAD.promise ? PAYLOAD.promise() : PAYLOAD(); data = PAYLOAD.promise ? PAYLOAD.data : undefined; // Step 1.3.1: Is the return of action.payload a promise? if (!(0, _isPromise.default)(promiseFromAction)) { // If not, move on to the next middleware. return next(_objectSpread({}, action, { payload: promiseFromAction })); } } // Step 1.4: If there's no promise, move on to the next middleware. else { return next(action); } // Step 1b: If there's no payload, move on to the next middleware. } else { return next(action); } /** * Instantiate and define constants for: * (1) the action type * (2) the action meta */ var TYPE = action.type; var META = action.meta; /** * Instantiate and define constants for the action type suffixes. * These are appended to the end of the action type. */ var _PROMISE_TYPE_SUFFIXE = _slicedToArray(PROMISE_TYPE_SUFFIXES, 3), PENDING = _PROMISE_TYPE_SUFFIXE[0], FULFILLED = _PROMISE_TYPE_SUFFIXE[1], REJECTED = _PROMISE_TYPE_SUFFIXE[2]; /** * Function: getAction * Description: This function constructs and returns a rejected * or fulfilled action object. The action object is based off the Flux * Standard Action (FSA). * * Given an original action with the type FOO: * * The rejected object model will be: * { * error: true, * type: 'FOO_REJECTED', * payload: ..., * meta: ... (optional) * } * * The fulfilled object model will be: * { * type: 'FOO_FULFILLED', * payload: ..., * meta: ... (optional) * } */ var getAction = function getAction(newPayload, isRejected) { return _objectSpread({ // Concatentate the type string property. type: [TYPE, isRejected ? REJECTED : FULFILLED].join(PROMISE_TYPE_DELIMITER) }, newPayload === null || typeof newPayload === 'undefined' ? {} : { payload: newPayload }, META !== undefined ? { meta: META } : {}, isRejected ? { error: true } : {}); }; /** * Function: handleReject * Calls: getAction to construct the rejected action * Description: This function dispatches the rejected action and returns * the original Error object. Please note the developer is responsible * for constructing and throwing an Error object. The middleware does not * construct any Errors. */ var handleReject = function handleReject(reason) { var rejectedAction = getAction(reason, true); dispatch(rejectedAction); throw reason; }; /** * Function: handleFulfill * Calls: getAction to construct the fullfilled action * Description: This function dispatches the fulfilled action and * returns the success object. The success object should * contain the value and the dispatched action. */ var handleFulfill = function handleFulfill() { var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; var resolvedAction = getAction(value, false); dispatch(resolvedAction); return { value: value, action: resolvedAction }; }; /** * First, dispatch the pending action: * This object describes the pending state of a promise and will include * any data (for optimistic updates) and/or meta from the original action. */ next(_objectSpread({ // Concatentate the type string. type: [TYPE, PENDING].join(PROMISE_TYPE_DELIMITER) }, data !== undefined ? { payload: data } : {}, META !== undefined ? { meta: META } : {})); /** * Second, dispatch a rejected or fulfilled action and move on to the * next middleware. */ return promiseFromAction.then(handleFulfill, handleReject); }; }; }; } /** * @public promise * The default export of the module. */ function promise() { var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, dispatch = _ref2.dispatch; if (typeof dispatch === 'function') { return createPromise()({ dispatch: dispatch }); } return null; } |