1 | import { wrapWithAbort } from './operators/withabort';
|
2 | import { AsyncIterableX } from './asynciterablex';
|
3 | import { returnAsyncIterator } from '../util/returniterator';
|
4 | import { throwIfAborted } from '../aborterror';
|
5 | export class ZipAsyncIterable extends AsyncIterableX {
|
6 | constructor(sources) {
|
7 | super();
|
8 | this._sources = sources;
|
9 | }
|
10 |
|
11 | async *[Symbol.asyncIterator](signal) {
|
12 | throwIfAborted(signal);
|
13 | const sourcesLength = this._sources.length;
|
14 | const its = this._sources.map((x) => wrapWithAbort(x, signal)[Symbol.asyncIterator]());
|
15 | while (sourcesLength > 0) {
|
16 | const values = new Array(sourcesLength);
|
17 | for (let i = -1; ++i < sourcesLength;) {
|
18 | const { value, done } = await its[i].next();
|
19 | if (done) {
|
20 | await Promise.all(its.map(returnAsyncIterator));
|
21 | return undefined;
|
22 | }
|
23 | values[i] = value;
|
24 | }
|
25 | yield values;
|
26 | }
|
27 | }
|
28 | }
|
29 | export function zip(...sources) {
|
30 | return new ZipAsyncIterable(sources);
|
31 | }
|
32 |
|
33 |
|