export class SwappingLru {
    constructor(maxSize) {
        Object.defineProperty(this, "cacheA", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: new Map()
        });
        Object.defineProperty(this, "cacheB", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: new Map()
        });
        Object.defineProperty(this, "maxSize", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "hits", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 0
        });
        Object.defineProperty(this, "misses", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 0
        });
        Object.defineProperty(this, "resets", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 0
        });
        Object.defineProperty(this, "clears", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 0
        });
        Object.defineProperty(this, "_lastCheckedAt", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: -1
        });
        this.maxSize = maxSize;
    }
    get(id) {
        const cacheA = this.cacheA.get(id);
        if (cacheA) {
            this.hits++;
            return cacheA;
        }
        const cacheB = this.cacheB.get(id);
        if (cacheB == null) {
            this.misses++;
            return null;
        }
        this.hits++;
        // If a object is still useful move it into the main cache
        this.cacheA.set(id, cacheB);
        this.cacheB.delete(id);
        return cacheB;
    }
    /** Reset the cache */
    clear() {
        this.cacheA.clear();
        this.cacheB.clear();
        this.clears++;
    }
    delete(id) {
        this.cacheA.delete(id);
        this.cacheB.delete(id);
    }
    set(id, tiff) {
        this.cacheA.set(id, tiff);
        this.check();
    }
    /** Validate the size of the cache has not exploded */
    check() {
        this._lastCheckedAt = Date.now();
        if (this.maxSize <= 0)
            return;
        if (this.currentSize <= this.maxSize)
            return;
        this.resets++;
        // Paranoia if we are resetting too often something is wrong, reset the entire cache.
        if (this.resets > 100) {
            this.resets = 0;
            return this.clear();
        }
        this.cacheB = this.cacheA;
        this.cacheA = new Map();
    }
    /** Calculate the total number of bytes used by this cache */
    get currentSize() {
        let size = 0;
        for (const value of this.cacheA.values())
            size += value.size;
        return size;
    }
}
//# sourceMappingURL=swapping.lru.js.map