@xlink-network/xlink-sdk
Version:
37 lines (36 loc) • 961 B
JavaScript
// src/utils/pMemoize.ts
function pMemoize(options, fn) {
const cache = /* @__PURE__ */ new Map();
const skipCache = options.skipCache == null ? async () => false : typeof options.skipCache === "function" ? options.skipCache : async () => options.skipCache;
return async function(...args) {
const key = options.cacheKey(args);
if (cache.has(key)) {
return cache.get(key);
}
const promise = (async () => {
const cleanCache = () => {
queueMicrotask(() => {
if (cache.get(key) === promise) {
cache.delete(key);
}
});
};
try {
const result = await fn(...args);
if (await skipCache(args, result)) {
cleanCache();
}
return result;
} catch (e) {
cleanCache();
throw e;
}
})();
cache.set(key, promise);
return promise;
};
}
export {
pMemoize
};
//# sourceMappingURL=chunk-OQHA7TS3.mjs.map