c5967a9616
* Rename to saveBatch * forgot a file * first pass of cacheReader * OL2 tests have infinite timeout * OL2 refactor works * fix for tests * moved/removed types to make a more logical structure * fixed imports * rework loop to be in async generator for the expected count * get rid of pumps and fix test * lint fix * redo mermaid diagrams * add readme section on deferment * always return root first * fix linting * revert the counting * merge fixes * remove unused var
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
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<Item> {
|
|
#writeQueue: BatchingQueue<Item> | 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<void> =>
|
|
this.#database.saveBatch({ batch })
|
|
})
|
|
}
|
|
this.#defermentManager.undefer(item)
|
|
this.#writeQueue.add(item.baseId, item)
|
|
}
|
|
|
|
async disposeAsync(): Promise<void> {
|
|
await this.#writeQueue?.disposeAsync()
|
|
this.#disposed = true
|
|
}
|
|
|
|
get isDisposed(): boolean {
|
|
return this.#disposed
|
|
}
|
|
}
|