* Apply a fused run of stateless async transforms to a source. * All transforms in the run are applied in a tight synchronous loop per batch, * avoiding the overhead of N async generator ticks for N transforms. * * INVARIANT: This function accepts a signal, NOT a pre-built options object. * A f
(source, run, signal)
| 571 | * @yields {Uint8Array[]} |
| 572 | */ |
| 573 | async function* applyFusedStatelessAsyncTransforms(source, run, signal) { |
| 574 | for await (const chunks of source) { |
| 575 | let current = chunks; |
| 576 | for (let i = 0; i < run.length; i++) { |
| 577 | const result = run[i](current, { __proto__: null, signal }); |
| 578 | if (result === null) { |
| 579 | current = null; |
| 580 | break; |
| 581 | } |
| 582 | if (isPromise(result)) { |
| 583 | const resolved = await result; |
| 584 | if (resolved === null) { |
| 585 | current = null; |
| 586 | break; |
| 587 | } |
| 588 | current = resolved; |
| 589 | } else { |
| 590 | current = result; |
| 591 | } |
| 592 | } |
| 593 | if (current === null) continue; |
| 594 | // Normalize the final output |
| 595 | if (isUint8ArrayBatch(current)) { |
| 596 | if (current.length > 0) yield current; |
| 597 | } else if (isUint8Array(current)) { |
| 598 | yield [current]; |
| 599 | } else if (typeof current === 'string') { |
| 600 | yield [toUint8Array(current)]; |
| 601 | } else if (isAnyArrayBuffer(current)) { |
| 602 | yield [new Uint8Array(current)]; |
| 603 | } else if (ArrayBufferIsView(current)) { |
| 604 | yield [arrayBufferViewToUint8Array(current)]; |
| 605 | } else { |
| 606 | yield* processTransformResultAsync(current); |
| 607 | } |
| 608 | } |
| 609 | // Flush each transform after all upstream data, including data emitted by |
| 610 | // earlier flushes, has been processed by that transform. |
| 611 | let pending = []; |
| 612 | for (let i = 0; i < run.length; i++) { |
| 613 | const next = []; |
| 614 | for (let j = 0; j < pending.length; j++) { |
| 615 | const pendingResult = appendTransformResultAsync( |
| 616 | next, |
| 617 | run[i](pending[j], { __proto__: null, signal })); |
| 618 | if (pendingResult !== undefined) { |
| 619 | await pendingResult; |
| 620 | } |
| 621 | } |
| 622 | const flushResult = appendTransformResultAsync( |
| 623 | next, |
| 624 | run[i](null, { __proto__: null, signal })); |
| 625 | if (flushResult !== undefined) { |
| 626 | await flushResult; |
| 627 | } |
| 628 | pending = next; |
| 629 | } |
| 630 | for (let i = 0; i < pending.length; i++) { |
no test coverage detected