Skip to content

Commit

Permalink
Implemented a separate for object-loader which can be used autonomous…
Browse files Browse the repository at this point in the history
…ly (#2647)
  • Loading branch information
AlexandruPopovici authored Aug 14, 2024
1 parent 09c2961 commit 76b5964
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
22 changes: 21 additions & 1 deletion packages/objectloader/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions packages/objectloader/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ObjectLoader {
}>
})

async getTotalObjectCount(): Promise<number>
async getAndConstructObject(
onProgress: (e: { stage: ProgressStage; current: number; total: number }) => void
): SpeckleObject | SpeckleObject[]
Expand Down
4 changes: 2 additions & 2 deletions packages/viewer-sandbox/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
)
Expand Down
3 changes: 1 addition & 2 deletions packages/viewer/src/modules/loaders/Speckle/SpeckleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -99,7 +99,6 @@ export class SpeckleLoader extends Loader {
}
)
first = false
total = obj.totalChildrenCount as number
}
current++
this.emit(LoaderEvent.LoadProgress, {
Expand Down

0 comments on commit 76b5964

Please sign in to comment.