markbello-iguazu-rest
Version:
A Redux REST caching library that follows Iguazu patterns
92 lines (77 loc) • 5.58 kB
JavaScript
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleQueryPromiseRejection = handleQueryPromiseRejection;
exports.waitAndDispatchFinished = waitAndDispatchFinished;
var types = _interopRequireWildcard(require("../types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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; }
/**
* Some actions which are asynchronous need to act on a promise, but also return the original
* promise to achieve desired behavior. Moving these asynchronous side effects to their own
* functions in a different file makes it easier to mock them and make sure they were called
* with the correct inputs in one test script and test their behavior in another test script.
*/
/**
* The promise in queryResource and queryCollection is exclusively used to wait on asynchronous
* data before server-side rendering. The promise should reject if the load failed, so that iguazu
* doesn't continue to load data that it does not need when it will just render an error state. On
* the client, nothing waits on the promise via `.then` or `await`. Instead, a redux action is fired
* when the async task is finished, which updates the store, which then triggers loadDataAsProps to
* run. Whether the async task finished successfully or not will be made apparent by what is stored
* in state. So it seems like the promise rejection is not being handled, but ultimately it is. This
* is a simple catch to assure the browser that the promise has indeed been dealt with so it will
* not emit a `unhandledrejection` event.
*/
function handleQueryPromiseRejection(promise) {
return promise.then(null, function () {
/* Swallow */
});
}
/**
* The promise from executeFetch will be caught by the handleQueryPromiseRejection function above.
* Its job is to catch if the network job failed, because nothing is broken if it did. It should
* not catch any errors that happen as a result of the network call failing which is what happens
* if the promise chain returned from executeFetch dispatches redux actions inside of it. The redux
* actions can cause a rerender and a render error would be swallowed making it hard to debug what
* went wrong.
*/
function waitAndDispatchFinished(promise, action) {
return function _callee(dispatch) {
var data;
return regeneratorRuntime.async(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return regeneratorRuntime.awrap(promise);
case 3:
data = _context.sent;
dispatch(_objectSpread({}, action, {
type: types["".concat(action.type, "_FINISHED")],
data: data
}));
_context.next = 11;
break;
case 7:
_context.prev = 7;
_context.t0 = _context["catch"](0);
data = _context.t0;
dispatch(_objectSpread({}, action, {
type: types["".concat(action.type, "_ERROR")],
data: data
}));
case 11:
case "end":
return _context.stop();
}
}
}, null, null, [[0, 7]]);
};
}