1 | import { AsyncIterableX } from './asynciterablex';
|
2 | import { identityAsync } from '../internal/identity';
|
3 | import { returnAsyncIterator } from '../internal/returniterator';
|
4 | export class ZipAsyncIterable extends AsyncIterableX {
|
5 | constructor(sources, fn) {
|
6 | super();
|
7 | this._sources = sources;
|
8 | this._fn = fn;
|
9 | }
|
10 | async *[Symbol.asyncIterator]() {
|
11 | const fn = this._fn;
|
12 | const sourcesLength = this._sources.length;
|
13 | const its = this._sources.map(x => x[Symbol.asyncIterator]());
|
14 | do {
|
15 | const values = new Array(sourcesLength);
|
16 | for (let i = -1; ++i < sourcesLength;) {
|
17 | const result = await its[i].next();
|
18 | if (result.done) {
|
19 | await Promise.all(its.map(returnAsyncIterator));
|
20 | return undefined;
|
21 | }
|
22 | values[i] = result.value;
|
23 | }
|
24 | yield await fn(values);
|
25 | } while (1);
|
26 | }
|
27 | }
|
28 |
|
29 | export function zip(...sources) {
|
30 | let fn = sources.shift();
|
31 | if (typeof fn !== 'function') {
|
32 | sources.unshift(fn);
|
33 | fn = identityAsync;
|
34 | }
|
35 | return new ZipAsyncIterable(sources, fn);
|
36 | }
|
37 |
|
38 |
|