@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
34 lines • 908 B
JavaScript
/**
* Map with a LRU (Least recently used) policy.
*
* https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
*/
export class LruMap extends Map {
maxSize;
constructor(size) {
super();
this.maxSize = size;
}
set(key, value) {
super.set(key, value);
if (this.maxSize && this.size > this.maxSize) {
this.delete(this.keys().next().value);
}
return this;
}
}
/** @internal */
export const promiseCache = /*#__PURE__*/ new LruMap(8192);
/** Deduplicates in-flight promises. */
export function withDedupe(fn, { enabled = true, id }) {
if (!enabled || !id) {
return fn();
}
if (promiseCache.get(id)) {
return promiseCache.get(id);
}
const promise = fn().finally(() => promiseCache.delete(id));
promiseCache.set(id, promise);
return promise;
}
//# sourceMappingURL=withDedupe.js.map