| * Copyright 2022 Google LLC |
| * SPDX-License-Identifier: Apache-2.0 |
| import {Scheduler, Task} from './scheduler'; |
| export class MaxInFlightScheduler<T> implements Scheduler<T> { |
| private waiting: Array<Task<void>> = []; |
| private readonly base: Scheduler<T>, |
| private maxInflight: number = 10 |
| async schedule(task: Task<T>): Promise<T> { |
| return new Promise<T>((resolve, reject) => { |
| this.waiting.push(async () => { |
| const result = await this.base.schedule(task); |
| if (this.inflight >= this.maxInflight) return; |
| if (this.waiting.length === 0) return; |
| const task = this.waiting.shift() as Task<void>; |