mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 05:04:48 +00:00
parent
b1893e65f2
commit
490d2a5ca1
2 changed files with 44 additions and 8 deletions
|
@ -7,14 +7,15 @@ interface TaggedYieldedValue<T> {
|
|||
}
|
||||
|
||||
/** The MuxAsyncIterator class multiplexes multiple async iterators into a
|
||||
* single stream. It currently makes a few assumptions:
|
||||
* - The iterators do not throw.
|
||||
* single stream. It currently makes an assumption:
|
||||
* - The final result (the value returned and not yielded from the iterator)
|
||||
* does not matter; if there is any, it is discarded.
|
||||
*/
|
||||
export class MuxAsyncIterator<T> implements AsyncIterable<T> {
|
||||
private iteratorCount = 0;
|
||||
private yields: Array<TaggedYieldedValue<T>> = [];
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private throws: any[] = [];
|
||||
private signal: Deferred<void> = deferred();
|
||||
|
||||
add(iterator: AsyncIterableIterator<T>): void {
|
||||
|
@ -25,11 +26,15 @@ export class MuxAsyncIterator<T> implements AsyncIterable<T> {
|
|||
private async callIteratorNext(
|
||||
iterator: AsyncIterableIterator<T>
|
||||
): Promise<void> {
|
||||
const { value, done } = await iterator.next();
|
||||
if (done) {
|
||||
--this.iteratorCount;
|
||||
} else {
|
||||
this.yields.push({ iterator, value });
|
||||
try {
|
||||
const { value, done } = await iterator.next();
|
||||
if (done) {
|
||||
--this.iteratorCount;
|
||||
} else {
|
||||
this.yields.push({ iterator, value });
|
||||
}
|
||||
} catch (e) {
|
||||
this.throws.push(e);
|
||||
}
|
||||
this.signal.resolve();
|
||||
}
|
||||
|
@ -46,6 +51,12 @@ export class MuxAsyncIterator<T> implements AsyncIterable<T> {
|
|||
this.callIteratorNext(iterator);
|
||||
}
|
||||
|
||||
if (this.throws.length) {
|
||||
for (const e of this.throws) {
|
||||
throw e;
|
||||
}
|
||||
this.throws.length = 0;
|
||||
}
|
||||
// Clear the `yields` list and reset the `signal` promise.
|
||||
this.yields.length = 0;
|
||||
this.signal = deferred();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue