@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
39 lines (37 loc) • 1.3 kB
JavaScript
import { AsyncIterableX } from './asynciterablex';
import { identityAsync } from '../util/identity';
import { returnAsyncIterator } from '../util/returniterator';
export class ZipAsyncIterable extends AsyncIterableX {
constructor(sources, fn) {
super();
this._sources = sources;
this._fn = fn;
}
// eslint-disable-next-line consistent-return
async *[Symbol.asyncIterator]() {
const fn = this._fn;
const sourcesLength = this._sources.length;
const its = this._sources.map(x => x[Symbol.asyncIterator]());
while (sourcesLength > 0) {
const values = new Array(sourcesLength);
for (let i = -1; ++i < sourcesLength;) {
const result = await its[i].next();
if (result.done) {
await Promise.all(its.map(returnAsyncIterator));
return undefined;
}
values[i] = result.value;
}
yield await fn(values);
}
}
}
export function zip(...sources) {
let fn = (sources.shift() || identityAsync);
if (fn && typeof fn !== 'function') {
sources.unshift(fn);
fn = identityAsync;
}
return new ZipAsyncIterable(sources, fn);
}
//# sourceMappingURL=zip.mjs.map