@speckle/objectloader2
Version:
This is an updated objectloader for the Speckle viewer written in typescript
108 lines (96 loc) • 3.16 kB
text/typescript
import { TIME } from '@speckle/shared'
import { Database } from '../operations/interfaces.js'
import { CacheOptions } from '../operations/options.js'
import { CustomLogger, Item } from '../types/types.js'
import BatchingQueue from './batchingQueue.js'
import Queue from './queue.js'
import { Downloader } from '../operations/interfaces.js'
import { DefermentManager } from './defermentManager.js'
import AsyncGeneratorQueue from './asyncGeneratorQueue.js'
import { Pump } from './pump.js'
export class CachePump implements Pump {
constructor(
database: Database,
gathered: AsyncGeneratorQueue<Item>,
deferments: DefermentManager,
options: CacheOptions
) {
this.
this.
this.
this.
this.
}
add(item: Item): void {
if (!this.
this.
batchSize: this.
maxWaitTime: this.
processFunction: (batch: Item[]): Promise<void> =>
this.
})
}
this.
}
async disposeAsync(): Promise<void> {
await this.
await this.
this.
}
get isDisposed(): boolean {
return this.
}
async pumpItems(params: {
ids: string[]
foundItems: Queue<Item>
notFoundItems: Queue<string>
}): Promise<void> {
const { ids, foundItems, notFoundItems } = params
const maxCacheReadSize = this.
for (let i = 0; i < ids.length; ) {
if (this.isDisposed) break
if ((this.#writeQueue?.count() ?? 0) > this.#options.maxWriteQueueSize) {
this.
'pausing reads (# in write queue: ' + this.
)
await new Promise((resolve) => setTimeout(resolve, TIME.second)) // Pause for 1 second, protects against out of memory
continue
}
const batch = ids.slice(i, i + maxCacheReadSize)
const cachedData = await this.
for (let i = 0; i < cachedData.length; i++) {
if (cachedData[i]) {
foundItems.add(cachedData[i]!)
} else {
notFoundItems.add(batch[i])
}
}
i += maxCacheReadSize
}
}
async *gather(ids: string[], downloader: Downloader): AsyncGenerator<Item> {
const total = ids.length
const pumpPromise = this.pumpItems({
ids,
foundItems: this.
notFoundItems: downloader
})
let count = 0
for await (const item of this.#gathered.consume()) {
this.
yield item
count++
if (count >= total) {
this.
}
}
await pumpPromise
}
}