| 172 | // Downstream backpressure: a fast generator emitting many chunks, with a |
| 173 | // slow reader, must not flood the readable buffer unboundedly. |
| 174 | export async function asyncgenReaderBackpressure () { |
| 175 | const ts = transform(async function * (source) { |
| 176 | // eslint-disable-next-line no-unused-vars |
| 177 | for await (const _ of source) for (let i = 0; i < 50; i++) yield i |
| 178 | }) |
| 179 | const r = new ReadableStream({ |
| 180 | start (controller) { controller.enqueue('go'); controller.close() } |
| 181 | }) |
| 182 | const reader = r.pipeThrough(ts).getReader() |
| 183 | const out = [] |
| 184 | while (true) { |
| 185 | const { value, done } = await reader.read() |
| 186 | if (done) break |
| 187 | out.push(value) |
| 188 | await new Promise((resolve) => setTimeout(resolve, 1)) // slow reader |
| 189 | } |
| 190 | deepEq(out, Array.from({ length: 50 }, (_, i) => i)) |
| 191 | } |