+13
-5
@@ -517,7 +517,10 @@ import { EventEmitter } from 'eventemitter3'; | ||
| async onSizeLessThan(limit) { | ||
| // Instantly resolve if the queue is empty. | ||
| // Instantly resolve if the size is already below the limit. | ||
| if (this.#queue.size < limit) { | ||
| return; | ||
| } | ||
| await this.#onEvent('next', () => this.#queue.size < limit); | ||
| // Listen on both `'next'` (task completion, queued abort, `clear()`) and `'active'` (every dequeue), | ||
| // so waiters wake even when the queue drains without a completion (`start()`, a runtime `concurrency` | ||
| // increase, or an interval tick). | ||
| await this.#onEvent(['next', 'active'], () => this.#queue.size < limit); | ||
| } | ||
@@ -604,3 +607,4 @@ /** | ||
| } | ||
| async #onEvent(event, filter) { | ||
| async #onEvent(events, filter) { | ||
| const eventList = Array.isArray(events) ? events : [events]; | ||
| return new Promise(resolve => { | ||
@@ -611,6 +615,10 @@ const listener = () => { | ||
| } | ||
| this.off(event, listener); | ||
| for (const event of eventList) { | ||
| this.off(event, listener); | ||
| } | ||
| resolve(); | ||
| }; | ||
| this.on(event, listener); | ||
| for (const event of eventList) { | ||
| this.on(event, listener); | ||
| } | ||
| }); | ||
@@ -617,0 +625,0 @@ } |
+1
-1
| { | ||
| "name": "p-queue", | ||
| "version": "9.3.0", | ||
| "version": "9.3.1", | ||
| "description": "Promise queue with concurrency control", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
83686
0.6%1237
0.65%