/** How long after we've seen a hash in the server, we consider it to be still in the server. */
const CACHE_DURATION_MS = 1_000 * 60 * 60 * 6; // 6 hours
// TODO(george): We are not currently removing old entries from the cache after they expire. We only do this when one of the
// `check_*` methods is called. If a hash is added to the cache but never checked, it will never be removed from the cache.
// This causes a memory leak. This can be a problem if the user has a large number of files in their project and/or if they are
// keeping the dev server running for a long time.
/**
 * A cache of hashes of DBFS objects that we know that are already stored in the server.
 */
export class HashCache {
    // for both maps, the values are timestamps from the monotonic clock (`performance.now()`) that represent the time
    // when we added the hash to the respective cache
    directories = new Map();
    file_contents = new Map();
    now() {
        return performance.now();
    }
    add_directory(hash) {
        this.directories.set(hash, this.now());
    }
    add_file_content(hash) {
        this.file_contents.set(hash, this.now());
    }
    add_directory_recursive(directory) {
        this.add_directory(directory.hash);
        for (const entry of directory.contents) {
            if (entry.type === "d") {
                this.add_directory_recursive(entry);
            }
            else if (entry.type === "-") {
                this.add_file_content(entry.hash);
            }
        }
    }
    check_directory(hash) {
        const timestamp = this.directories.get(hash);
        if (timestamp === undefined) {
            return false;
        }
        if (this.now() - timestamp > CACHE_DURATION_MS) {
            this.directories.delete(hash);
            return false;
        }
        return true;
    }
    check_file_content(hash) {
        const timestamp = this.file_contents.get(hash);
        if (timestamp === undefined) {
            return false;
        }
        if (this.now() - timestamp > CACHE_DURATION_MS) {
            this.file_contents.delete(hash);
            return false;
        }
        return true;
    }
    empty() {
        this.directories.clear();
        this.file_contents.clear();
    }
}
//# sourceMappingURL=hash-cache.js.map