import { hashCodeQuery } from "../Hash.js"; import { Options } from "../Storage/mod.js"; import type { Document, Filter, WithId } from "../Types.js"; export class IndexedDbCache { readonly #cache = new Map(); readonly #documents = new Map>(); hash(filter: Filter>, options: Options): number { return hashCodeQuery(filter, options); } set(hashCode: number, documents: WithId[]) { this.#cache.set( hashCode, documents.map((document) => document.id) ); for (const document of documents) { this.#documents.set(document.id, document); } } get(hashCode: number): WithId[] | undefined { const ids = this.#cache.get(hashCode); if (ids !== undefined) { return ids.map((id) => this.#documents.get(id) as WithId); } } flush() { this.#cache.clear(); this.#documents.clear(); } }