UNPKG

@gooddata/react-components

Version:

GoodData.UI - A powerful JavaScript library for building analytical applications

45 lines 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // (C) 2007-2019 GoodData Corporation var get = require("lodash/get"); var set = require("lodash/set"); var gdcSdkCache = {}; function getSdkCache() { return gdcSdkCache; } /** * Clears whole cache * Needs to be called manually on-demand * * @method clearSdkCache */ function clearSdkCache() { if (gdcSdkCache) { gdcSdkCache = {}; } } exports.clearSdkCache = clearSdkCache; /** * Wraps some async call defined by onCacheMiss param and caches its result for next re-use. * If cached result is rejected it is removed from cache to unblock future calls * * @method getCachedOrLoad * @param {String} cacheKey - unique key to identify cached value * @param {Function} onCacheMiss - async function returning promise to call when value is not found in cache * @return {Promise} value from cache or result of onCacheMiss invocation */ function getCachedOrLoad(cacheKey, onCacheMiss) { var sdkCache = getSdkCache(); var cachedResponse = get(sdkCache, cacheKey); if (cachedResponse) { return cachedResponse; } var promise = onCacheMiss(); set(sdkCache, cacheKey, promise); promise.catch(function () { delete sdkCache.cacheKey; }); return promise; } exports.getCachedOrLoad = getCachedOrLoad; //# sourceMappingURL=sdkCache.js.map