( stream: AsyncGenerator<string>, maxTimeMs: number, fullStop: () => void, )
| 1 | export async function* stopAfterMaxProcessingTime( |
| 2 | stream: AsyncGenerator<string>, |
| 3 | maxTimeMs: number, |
| 4 | fullStop: () => void, |
| 5 | ): AsyncGenerator<string> { |
| 6 | const startTime = Date.now(); |
| 7 | /** |
| 8 | * Check every 10 chunks to avoid performance overhead. |
| 9 | */ |
| 10 | const checkInterval = 10; |
| 11 | let chunkCount = 0; |
| 12 | let totalCharCount = 0; |
| 13 | |
| 14 | for await (const chunk of stream) { |
| 15 | yield chunk; |
| 16 | |
| 17 | chunkCount++; |
| 18 | totalCharCount += chunk.length; |
| 19 | |
| 20 | if (chunkCount % checkInterval === 0) { |
| 21 | if (Date.now() - startTime > maxTimeMs) { |
| 22 | fullStop(); |
| 23 | return; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
no outgoing calls
no test coverage detected