|
| 1 | +/** |
| 2 | + * Merges two readable streams into a single readable stream, emitting values |
| 3 | + * from each stream as they become available. |
| 4 | + * |
| 5 | + * The first stream is prioritized over the second stream. If both streams have |
| 6 | + * values available, the first stream's value is emitted first. |
| 7 | + * |
| 8 | + * @template VALUE1 - The type of values emitted by the first stream. |
| 9 | + * @template VALUE2 - The type of values emitted by the second stream. |
| 10 | + * @param {ReadableStream<VALUE1>} stream1 - The first readable stream. |
| 11 | + * @param {ReadableStream<VALUE2>} stream2 - The second readable stream. |
| 12 | + * @returns {ReadableStream<VALUE1 | VALUE2>} A new readable stream that emits values from both input streams. |
| 13 | + */ |
| 14 | +export function mergeStreams<VALUE1, VALUE2>( |
| 15 | + stream1: ReadableStream<VALUE1>, |
| 16 | + stream2: ReadableStream<VALUE2>, |
| 17 | +): ReadableStream<VALUE1 | VALUE2> { |
| 18 | + const reader1 = stream1.getReader(); |
| 19 | + const reader2 = stream2.getReader(); |
| 20 | + |
| 21 | + let lastRead1: Promise<ReadableStreamReadResult<VALUE1>> | undefined = |
| 22 | + undefined; |
| 23 | + let lastRead2: Promise<ReadableStreamReadResult<VALUE2>> | undefined = |
| 24 | + undefined; |
| 25 | + |
| 26 | + let stream1Done = false; |
| 27 | + let stream2Done = false; |
| 28 | + |
| 29 | + // only use when stream 2 is done: |
| 30 | + async function readStream1( |
| 31 | + controller: ReadableStreamDefaultController<VALUE1 | VALUE2>, |
| 32 | + ) { |
| 33 | + try { |
| 34 | + if (lastRead1 == null) { |
| 35 | + lastRead1 = reader1.read(); |
| 36 | + } |
| 37 | + |
| 38 | + const result = await lastRead1; |
| 39 | + lastRead1 = undefined; |
| 40 | + |
| 41 | + if (!result.done) { |
| 42 | + controller.enqueue(result.value); |
| 43 | + } else { |
| 44 | + controller.close(); |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + controller.error(error); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // only use when stream 1 is done: |
| 52 | + async function readStream2( |
| 53 | + controller: ReadableStreamDefaultController<VALUE1 | VALUE2>, |
| 54 | + ) { |
| 55 | + try { |
| 56 | + if (lastRead2 == null) { |
| 57 | + lastRead2 = reader2.read(); |
| 58 | + } |
| 59 | + |
| 60 | + const result = await lastRead2; |
| 61 | + lastRead2 = undefined; |
| 62 | + |
| 63 | + if (!result.done) { |
| 64 | + controller.enqueue(result.value); |
| 65 | + } else { |
| 66 | + controller.close(); |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + controller.error(error); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return new ReadableStream<VALUE1 | VALUE2>({ |
| 74 | + async pull(controller) { |
| 75 | + try { |
| 76 | + // stream 1 is done, we can only read from stream 2: |
| 77 | + if (stream1Done) { |
| 78 | + readStream2(controller); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // stream 2 is done, we can only read from stream 1: |
| 83 | + if (stream2Done) { |
| 84 | + readStream1(controller); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // pull the next value from the stream that was read last: |
| 89 | + if (lastRead1 == null) { |
| 90 | + lastRead1 = reader1.read(); |
| 91 | + } |
| 92 | + if (lastRead2 == null) { |
| 93 | + lastRead2 = reader2.read(); |
| 94 | + } |
| 95 | + |
| 96 | + // Note on Promise.race (prioritizing stream 1 over stream 2): |
| 97 | + // If the iterable contains one or more non-promise values and/or an already settled promise, |
| 98 | + // then Promise.race() will settle to the first of these values found in the iterable. |
| 99 | + const { result, reader } = await Promise.race([ |
| 100 | + lastRead1.then(result => ({ result, reader: reader1 })), |
| 101 | + lastRead2.then(result => ({ result, reader: reader2 })), |
| 102 | + ]); |
| 103 | + |
| 104 | + if (!result.done) { |
| 105 | + controller.enqueue(result.value); |
| 106 | + } |
| 107 | + |
| 108 | + if (reader === reader1) { |
| 109 | + lastRead1 = undefined; |
| 110 | + if (result.done) { |
| 111 | + // stream 1 is done, we can only read from stream 2: |
| 112 | + readStream2(controller); |
| 113 | + stream1Done = true; |
| 114 | + } |
| 115 | + } else { |
| 116 | + lastRead2 = undefined; |
| 117 | + // stream 2 is done, we can only read from stream 1: |
| 118 | + if (result.done) { |
| 119 | + stream2Done = true; |
| 120 | + readStream1(controller); |
| 121 | + } |
| 122 | + } |
| 123 | + } catch (error) { |
| 124 | + controller.error(error); |
| 125 | + } |
| 126 | + }, |
| 127 | + cancel() { |
| 128 | + reader1.cancel(); |
| 129 | + reader2.cancel(); |
| 130 | + }, |
| 131 | + }); |
| 132 | +} |
0 commit comments