修复点位楼层跳转并优化模型加载缓存
This commit is contained in:
20
src/utils/concurrency.ts
Normal file
20
src/utils/concurrency.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export const mapWithConcurrency = async <T, R>(
|
||||
items: T[],
|
||||
concurrency: number,
|
||||
mapper: (item: T, index: number) => Promise<R>
|
||||
): Promise<R[]> => {
|
||||
const results: R[] = new Array(items.length)
|
||||
const limit = Math.max(1, Math.floor(concurrency))
|
||||
let nextIndex = 0
|
||||
|
||||
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
||||
while (nextIndex < items.length) {
|
||||
const currentIndex = nextIndex
|
||||
nextIndex += 1
|
||||
results[currentIndex] = await mapper(items[currentIndex], currentIndex)
|
||||
}
|
||||
})
|
||||
|
||||
await Promise.all(workers)
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user