From 76b5964d9dd349e97f7908ded719cfe658f1955c Mon Sep 17 00:00:00 2001 From: Alexandru Popovici Date: Wed, 14 Aug 2024 14:01:04 +0300 Subject: [PATCH] Implemented a separate for object-loader which can be used autonomously (#2647) --- packages/objectloader/src/index.js | 22 ++++++++++++++++++- packages/objectloader/types/index.d.ts | 1 + packages/viewer-sandbox/src/main.ts | 4 ++-- .../modules/loaders/Speckle/SpeckleLoader.ts | 3 +-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/objectloader/src/index.js b/packages/objectloader/src/index.js index 552720c7a2..f31c9e7ae1 100644 --- a/packages/objectloader/src/index.js +++ b/packages/objectloader/src/index.js @@ -128,6 +128,18 @@ class ObjectLoader { Object.values(this.intervals).forEach((i) => clearInterval(i.interval)) } + async getTotalObjectCount() { + /** This is fine, because it gets cached */ + const rootObjJson = await this.getRawRootObject() + /** Ideally we shouldn't to a `parse` here since it's going to pointlessly allocate + * But doing string gymnastics in order to get closure length is going to be the same + * if not even more memory constly + */ + const rootObj = JSON.parse(rootObjJson) + const totalChildrenCount = Object.keys(rootObj?.__closure || {}).length + return totalChildrenCount + } + /** * Use this method to receive and construct the object. It will return the full, de-referenced and de-chunked original object. * @param {*} onProgress @@ -545,6 +557,10 @@ class ObjectLoader { return {} } + if (this.cacheDB === null) { + await this.setupCacheDb() + } + const ret = {} for (let i = 0; i < ids.length; i += 500) { @@ -575,11 +591,15 @@ class ObjectLoader { return ret } - cacheStoreObjects(objects) { + async cacheStoreObjects(objects) { if (!this.supportsCache()) { return {} } + if (this.cacheDB === null) { + await this.setupCacheDb() + } + try { const store = this.cacheDB .transaction('objects', 'readwrite') diff --git a/packages/objectloader/types/index.d.ts b/packages/objectloader/types/index.d.ts index cb82b648cf..ad70bda67e 100644 --- a/packages/objectloader/types/index.d.ts +++ b/packages/objectloader/types/index.d.ts @@ -41,6 +41,7 @@ class ObjectLoader { }> }) + async getTotalObjectCount(): Promise async getAndConstructObject( onProgress: (e: { stage: ProgressStage; current: number; total: number }) => void ): SpeckleObject | SpeckleObject[] diff --git a/packages/viewer-sandbox/src/main.ts b/packages/viewer-sandbox/src/main.ts index 4f9f6e7da0..5e2d509aaa 100644 --- a/packages/viewer-sandbox/src/main.ts +++ b/packages/viewer-sandbox/src/main.ts @@ -105,7 +105,7 @@ const getStream = () => { // prettier-ignore // 'https://speckle.xyz/streams/da9e320dad/commits/5388ef24b8?c=%5B-7.66134,10.82932,6.41935,-0.07739,-13.88552,1.8697,0,1%5D' // Revit sample house (good for bim-like stuff with many display meshes) - // 'https://speckle.xyz/streams/da9e320dad/commits/5388ef24b8' + 'https://speckle.xyz/streams/da9e320dad/commits/5388ef24b8' // 'https://latest.speckle.dev/streams/c1faab5c62/commits/ab1a1ab2b6' // 'https://speckle.xyz/streams/da9e320dad/commits/5388ef24b8' // 'https://latest.speckle.dev/streams/58b5648c4d/commits/60371ecb2d' @@ -401,7 +401,7 @@ const getStream = () => { // DUI3 Render materials // 'https://app.speckle.systems/projects/93200a735d/models/cbacd3eaeb' // 'https://latest.speckle.systems/projects/126cd4b7bb/models/7a5e738c76' - 'https://latest.speckle.systems/projects/126cd4b7bb/models/0a4181c73b' + // 'https://latest.speckle.systems/projects/126cd4b7bb/models/0a4181c73b' // 'https://latest.speckle.systems/projects/126cd4b7bb/models/bcf086cdc4' // 'https://latest.speckle.systems/projects/126cd4b7bb/models/6221c985c0' ) diff --git a/packages/viewer/src/modules/loaders/Speckle/SpeckleLoader.ts b/packages/viewer/src/modules/loaders/Speckle/SpeckleLoader.ts index bbf100b1f3..3ff4ac405c 100644 --- a/packages/viewer/src/modules/loaders/Speckle/SpeckleLoader.ts +++ b/packages/viewer/src/modules/loaders/Speckle/SpeckleLoader.ts @@ -74,7 +74,7 @@ export class SpeckleLoader extends Loader { const start = performance.now() let first = true let current = 0 - let total = 0 + const total = await this.loader.getTotalObjectCount() let viewerLoads = 0 let firstObjectPromise = null @@ -99,7 +99,6 @@ export class SpeckleLoader extends Loader { } ) first = false - total = obj.totalChildrenCount as number } current++ this.emit(LoaderEvent.LoadProgress, {