import { Database } from '../core/interfaces.js' import { CacheOptions } from '../core/options.js' import { DefermentManager } from '../deferment/defermentManager.js' import { Item } from '../types/types.js' import BatchingQueue from './batchingQueue.js' import Queue from './queue.js' export class CacheWriter implements Queue { #writeQueue: BatchingQueue | undefined #database: Database #defermentManager: DefermentManager #options: CacheOptions #disposed = false constructor( database: Database, defermentManager: DefermentManager, options: CacheOptions ) { this.#database = database this.#defermentManager = defermentManager this.#options = options } add(item: Item): void { if (!this.#writeQueue) { this.#writeQueue = new BatchingQueue({ batchSize: this.#options.maxCacheWriteSize, maxWaitTime: this.#options.maxCacheBatchWriteWait, processFunction: (batch: Item[]): Promise => this.#database.saveBatch({ batch }) }) } this.#defermentManager.undefer(item) this.#writeQueue.add(item.baseId, item) } async disposeAsync(): Promise { await this.#writeQueue?.disposeAsync() this.#disposed = true } get isDisposed(): boolean { return this.#disposed } }