(tasks: Task<T>[])
| 25 | } |
| 26 | |
| 27 | async execute<T>(tasks: Task<T>[]): Promise<TaskResult<T>[]> { |
| 28 | // Add tasks to queue |
| 29 | this.queue.push(...tasks); |
| 30 | |
| 31 | // Sort by priority (higher first) |
| 32 | this.queue.sort((a, b) => b.priority - a.priority); |
| 33 | |
| 34 | // Execute tasks |
| 35 | const activeTasks = new Set<Promise<void>>(); |
| 36 | |
| 37 | while (this.queue.length > 0 || this.running > 0) { |
| 38 | while (this.running < this.maxConcurrent && this.queue.length > 0) { |
| 39 | const task = this.queue.shift()!; |
| 40 | let taskPromise!: Promise<void>; |
| 41 | taskPromise = this.executeTask(task).finally(() => { |
| 42 | activeTasks.delete(taskPromise); |
| 43 | }); |
| 44 | activeTasks.add(taskPromise); |
| 45 | } |
| 46 | |
| 47 | // Wait for at least one task to complete |
| 48 | if (activeTasks.size > 0) { |
| 49 | await Promise.race(activeTasks); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Return results in original order |
| 54 | return tasks.map(task => this.results.get(task.id)!); |
| 55 | } |
| 56 | |
| 57 | private async executeTask<T>(task: Task<T>): Promise<void> { |
| 58 | this.running++; |
nothing calls this directly
no test coverage detected