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