Files
Folo/packages/internal/shared/src/queue.ts
T
Innei 4d746522f1 refactor: remove unused files and clean up project structure
- Deleted obsolete configuration files and unused components across various packages.
- Updated package.json files to reflect the removal of dependencies.
- Cleaned up code by removing unnecessary exports and unused imports in multiple modules.

Signed-off-by: Innei <tukon479@gmail.com>
2025-04-28 20:58:51 +08:00

40 lines
880 B
TypeScript

export class AsyncQueue {
private maxConcurrent: number
private queue: (() => Promise<any>)[]
private activeCount: number
constructor(maxConcurrent: number) {
this.maxConcurrent = maxConcurrent
this.queue = []
this.activeCount = 0
}
private async runNext() {
if (this.activeCount >= this.maxConcurrent || this.queue.length === 0) {
return
}
this.activeCount++
const request = this.queue.shift()!
try {
return await request()
} catch (error) {
console.error("Request failed", error)
} finally {
this.activeCount--
this.runNext() // Start the next request after this one finishes
}
}
add(request: () => Promise<any>) {
this.queue.push(request)
return this.runNext()
}
addMultiple(requests: (() => Promise<any>)[]) {
this.queue.push(...requests)
this.runNext()
}
}