moltres-utils
Version:
Utils for Moltres apps
63 lines (48 loc) • 1.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _ramda = require("ramda");
const makeCacheChain = () => ({
strongMap: new Map(),
weakMap: new WeakMap()
});
const makeCacheLink = () => ({
ref: {},
cacheChain: makeCacheChain()
});
const isWeakKey = (0, _ramda.is)(Object);
const setCacheKey = (cacheChain, key, value) => {
if (isWeakKey(key)) {
return cacheChain.weakMap.set(key, value);
}
return cacheChain.strongMap.set(key, value);
};
const getCacheKey = (cacheChain, key) => {
if (isWeakKey(key)) {
return cacheChain.weakMap.get(key);
}
return cacheChain.strongMap.get(key);
};
const linkCacheKey = (cacheChain, key) => {
let link = getCacheKey(cacheChain, key);
if (!link) {
link = makeCacheLink();
setCacheKey(cacheChain, key, link);
}
return link;
};
const cache = makeCacheChain();
const cacheChain = (...args) => {
let chain = cache;
let link = linkCacheKey(chain);
(0, _ramda.forEach)(arg => {
link = linkCacheKey(chain, arg);
chain = link.cacheChain;
}, args);
return link.ref;
};
var _default = cacheChain;
exports.default = _default;
//# sourceMappingURL=cacheChain.js.map
;