UNPKG

@speckle/objectloader2

Version:

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

94 lines 3.42 kB
import AsyncGeneratorQueue from '../helpers/asyncGeneratorQueue.js'; import { DefermentManager } from '../helpers/defermentManager.js'; import { CacheReader } from '../helpers/cacheReader.js'; import { CachePump } from '../helpers/cachePump.js'; import AggregateQueue from '../helpers/aggregateQueue.js'; import { ObjectLoader2Factory } from './objectLoader2Factory.js'; export class ObjectLoader2 { #rootId; #logger; #database; #downloader; #pump; #cache; #deferments; #gathered; #root = undefined; constructor(options) { this.#rootId = options.rootId; this.#logger = options.logger || console.log; const cacheOptions = { logger: this.#logger, maxCacheReadSize: 10_000, maxCacheWriteSize: 10_000, maxWriteQueueSize: 40_000, maxCacheBatchWriteWait: 3_000, maxCacheBatchReadWait: 3_000 }; this.#gathered = new AsyncGeneratorQueue(); this.#database = options.database; this.#deferments = new DefermentManager({ maxSizeInMb: 2_000, // 2 GBs ttlms: 15_000, // 15 seconds logger: this.#logger }); this.#cache = new CacheReader(this.#database, this.#deferments, cacheOptions); this.#pump = new CachePump(this.#database, this.#gathered, this.#deferments, cacheOptions); this.#downloader = options.downloader; } async disposeAsync() { await Promise.all([ this.#downloader.disposeAsync(), this.#cache.disposeAsync(), this.#pump.disposeAsync() ]); this.#deferments.dispose(); } async getRootObject() { if (!this.#root) { this.#root = (await this.#database.getAll([this.#rootId]))[0]; if (!this.#root) { this.#root = await this.#downloader.downloadSingle(); } } return this.#root; } async getObject(params) { return await this.#cache.getObject({ id: params.id }); } async getTotalObjectCount() { const rootObj = await this.getRootObject(); const totalChildrenCount = Object.keys(rootObj?.base.__closure || {}).length; return totalChildrenCount + 1; //count the root } async *getObjectIterator() { const rootItem = await this.getRootObject(); if (rootItem === undefined) { this.#logger('No root object found!'); return; } //only for root this.#pump.add(rootItem); yield rootItem.base; if (!rootItem.base.__closure) return; //sort the closures by their values descending const sortedClosures = Object.entries(rootItem.base.__closure).sort((a, b) => b[1] - a[1]); const children = sortedClosures.map((x) => x[0]); const total = children.length; this.#downloader.initializePool({ results: new AggregateQueue(this.#gathered, this.#pump), total }); for await (const item of this.#pump.gather(children, this.#downloader)) { yield item.base; } } static createFromObjects(objects) { return ObjectLoader2Factory.createFromObjects(objects); } static createFromJSON(json) { return ObjectLoader2Factory.createFromJSON(json); } } //# sourceMappingURL=objectLoader2.js.map