* Push an item to the queue * * If there's a waiting consumer, deliver directly (no buffering). * Otherwise, buffer the item for later consumption.
(item: T)
| 42 | * Otherwise, buffer the item for later consumption. |
| 43 | */ |
| 44 | push(item: T): void { |
| 45 | if (this.closed) return; |
| 46 | |
| 47 | // Key: Check if there's a waiting consumer FIRST |
| 48 | if (this.waiting.length > 0) { |
| 49 | // Deliver directly to waiting consumer (no race condition) |
| 50 | const resolve = this.waiting.shift()!; |
| 51 | resolve({ done: false, value: item }); |
| 52 | } else { |
| 53 | // No waiting consumer, buffer the item |
| 54 | this.buffer.push(item); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Close the queue |
no outgoing calls