recharts
Version:
React charts
50 lines (49 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.LRUCache = void 0;
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* Simple LRU (Least Recently Used) cache implementation
*/
class LRUCache {
constructor(maxSize) {
_defineProperty(this, "cache", new Map());
this.maxSize = maxSize;
}
get(key) {
// `has` rather than an `undefined` check on the value: `undefined` is a storable
// value, and treating it as a miss would leave that entry's recency unchanged.
if (!this.cache.has(key)) {
return undefined;
}
var value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
set(key, value) {
if (this.cache.has(key)) {
this.cache.delete(key);
} else if (this.cache.size >= this.maxSize) {
// Ask the iterator whether it produced a key, instead of testing the key itself:
// `null` and `undefined` are valid Map keys, so a nullish check would skip the
// eviction and let the cache grow past `maxSize`.
var oldest = this.cache.keys().next();
if (!oldest.done) {
this.cache.delete(oldest.value);
}
}
this.cache.set(key, value);
}
clear() {
this.cache.clear();
}
size() {
return this.cache.size;
}
}
exports.LRUCache = LRUCache;