| 2745 | |
| 2746 | // Helper to race iterator.next() with abort signal |
| 2747 | const nextChunkWithAbort = async () => { |
| 2748 | const nextPromise = iterator.next() |
| 2749 | |
| 2750 | // If we have an abort controller, race it with the next chunk |
| 2751 | if (this.currentRequestAbortController) { |
| 2752 | const abortPromise = new Promise<never>((_, reject) => { |
| 2753 | const signal = this.currentRequestAbortController!.signal |
| 2754 | if (signal.aborted) { |
| 2755 | reject(new Error("Request cancelled by user")) |
| 2756 | } else { |
| 2757 | signal.addEventListener( |
| 2758 | "abort", |
| 2759 | () => { |
| 2760 | reject(new Error("Request cancelled by user")) |
| 2761 | }, |
| 2762 | { once: true }, |
| 2763 | ) |
| 2764 | } |
| 2765 | }) |
| 2766 | return await Promise.race([nextPromise, abortPromise]) |
| 2767 | } |
| 2768 | |
| 2769 | // No abort controller, just return the next chunk normally |
| 2770 | return await nextPromise |
| 2771 | } |
| 2772 | |
| 2773 | let item = await nextChunkWithAbort() |
| 2774 | while (!item.done) { |