choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
81 lines (68 loc) • 2.86 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import { toJS } from 'mobx';
import { getConfig } from '../../../es/configure';
import Cache, { refreshCacheOptions } from '../_util/Cache';
import { buildURLWithAxiosConfig, isCacheLike } from './utils';
function getDefaultCache() {
var lookupCache = toJS(getConfig('lookupCache'));
var cache = new Cache(lookupCache);
refreshCacheOptions(cache);
return cache;
}
export default function cacheAdapterEnhancer(adapter) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$enabledByDef = options.enabledByDefault,
enabledByDefault = _options$enabledByDef === void 0 ? true : _options$enabledByDef,
_options$cacheFlag = options.cacheFlag,
cacheFlag = _options$cacheFlag === void 0 ? 'cache' : _options$cacheFlag,
_options$defaultCache = options.defaultCache,
defaultCache = _options$defaultCache === void 0 ? getDefaultCache() : _options$defaultCache;
return function (config) {
var useCache = config[cacheFlag] !== undefined && config[cacheFlag] !== null ? config[cacheFlag] : enabledByDefault;
if (useCache) {
var cache = isCacheLike(useCache) ? useCache : defaultCache;
var index = buildURLWithAxiosConfig(config);
var responsePromise = cache.get(index);
if (!responsePromise) {
responsePromise = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return adapter(config);
case 3:
return _context.abrupt("return", _context.sent);
case 6:
_context.prev = 6;
_context.t0 = _context["catch"](0);
cache.del(index);
throw _context.t0;
case 10:
case "end":
return _context.stop();
}
}
}, _callee, null, [[0, 6]]);
}))();
if (process.env.LOGGER_LEVEL === 'info') {
// eslint-disable-next-line no-console
console.info("request: ".concat(index));
}
cache.set(index, responsePromise);
return responsePromise;
}
if (process.env.LOGGER_LEVEL === 'info') {
// eslint-disable-next-line no-console
console.info("request cached by cache adapter: ".concat(index));
}
return responsePromise;
}
return adapter(config);
};
}
//# sourceMappingURL=cacheAdapterEnhancer.js.map