( text: string, onChunk: (chunk: string) => void, options: ReplayOptions, )
| 32 | |
| 33 | /** Replay saved HTML text chunk-by-chunk like a live API stream. */ |
| 34 | export async function replayStream( |
| 35 | text: string, |
| 36 | onChunk: (chunk: string) => void, |
| 37 | options: ReplayOptions, |
| 38 | ): Promise<void> { |
| 39 | let index = 0; |
| 40 | while (index < text.length) { |
| 41 | if (options.signal?.aborted) { |
| 42 | throw new DOMException("Aborted", "AbortError"); |
| 43 | } |
| 44 | const size = nextChunkSize(text, index, options.chunkSize); |
| 45 | const chunk = text.slice(index, index + size); |
| 46 | index += size; |
| 47 | onChunk(chunk); |
| 48 | await sleep(options.delayMs, options.signal); |
| 49 | } |
| 50 | } |
no test coverage detected