UNPKG

@speckle/objectloader2

Version:

This is an updated objectloader for the Speckle viewer written in typescript

51 lines 1.6 kB
import BatchingQueue from './batchingQueue.js'; export class CacheReader { #database; #defermentManager; #logger; #options; #readQueue; constructor(database, defermentManager, options) { this.#database = database; this.#defermentManager = defermentManager; this.#options = options; this.#logger = options.logger || (() => { }); } async getObject(params) { if (!this.#defermentManager.isDeferred(params.id)) { this.#getItem(params.id); } return await this.#defermentManager.defer({ id: params.id }); } #getItem(id) { if (!this.#readQueue) { this.#readQueue = new BatchingQueue({ batchSize: this.#options.maxCacheReadSize, maxWaitTime: this.#options.maxCacheBatchReadWait, processFunction: this.#processBatch }); } if (!this.#readQueue.get(id)) { this.#readQueue.add(id, id); } } async getAll(keys) { return this.#database.getAll(keys); } #processBatch = async (batch) => { const items = await this.#database.getAll(batch); for (let i = 0; i < items.length; i++) { if (items[i]) { this.#defermentManager.undefer(items[i]); } else { //this is okay! //this.#logger(`Item ${batch[i]} not found in cache`) } } }; async disposeAsync() { await this.#readQueue?.disposeAsync(); } } //# sourceMappingURL=cacheReader.js.map