-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(zip): make zip work on a variable number of source Iterables/AsyncIterables breaking change: zip selectors now take a single "values" Array argument, instead of varargs * test(zip): update zip tests for variable sources
- Loading branch information
1 parent
4098a8c
commit 0c4d513
Showing
12 changed files
with
184 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,58 @@ | ||
import { AsyncIterableX } from '../asynciterable'; | ||
import { identityAsync } from '../internal/identity'; | ||
import { returnAsyncIterator } from '../internal/returniterator'; | ||
|
||
class ZipIterable<TSource, TResult> extends AsyncIterableX<TResult> { | ||
private _left: AsyncIterable<TSource>; | ||
private _right: AsyncIterable<TSource>; | ||
private _fn: (left: TSource, right: TSource) => TResult | Promise<TResult>; | ||
class ZipAsyncIterable<TSource, TResult> extends AsyncIterableX<TResult> { | ||
private _sources: AsyncIterable<TSource>[]; | ||
private _fn: (values: any[]) => TResult | Promise<TResult>; | ||
|
||
constructor( | ||
left: AsyncIterable<TSource>, | ||
right: AsyncIterable<TSource>, | ||
fn: (left: TSource, right: TSource) => TResult | Promise<TResult>) { | ||
constructor(sources: AsyncIterable<TSource>[], fn: (values: any[]) => TResult | Promise<TResult>) { | ||
super(); | ||
this._left = left; | ||
this._right = right; | ||
this._sources = sources; | ||
this._fn = fn; | ||
} | ||
|
||
async *[Symbol.asyncIterator]() { | ||
const it1 = this._left[Symbol.asyncIterator](); | ||
const it2 = this._right[Symbol.asyncIterator](); | ||
while (1) { | ||
const xs = await Promise.all([it1.next(), it2.next()]); | ||
const [next1, next2] = xs; | ||
if (!next1.done && !next2.done) { | ||
yield await this._fn(next1.value, next2.value); | ||
} else { | ||
break; | ||
const fn = this._fn; | ||
const sourcesLength = this._sources.length; | ||
const its = this._sources.map((x) => x[Symbol.asyncIterator]()); | ||
do { | ||
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; | ||
} | ||
values[i] = result.value; | ||
} | ||
} | ||
yield await fn(values); | ||
} while (1); | ||
} | ||
} | ||
|
||
export function zip<TSource, TResult>( | ||
left: AsyncIterable<TSource>, | ||
right: AsyncIterable<TSource>, | ||
fn: (left: TSource, right: TSource) => TResult | Promise<TResult>): AsyncIterableX<TResult> { | ||
return new ZipIterable<TSource, TResult>(left, right, fn); | ||
/* tslint:disable:max-line-length */ | ||
export function zip<T, T2>(source: AsyncIterable<T>, source2: AsyncIterable<T2>): AsyncIterableX<[T, T2]>; | ||
export function zip<T, T2, T3>(source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>): AsyncIterableX<[T, T2, T3]>; | ||
export function zip<T, T2, T3, T4>(source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>, source4: AsyncIterable<T4>): AsyncIterableX<[T, T2, T3, T4]>; | ||
export function zip<T, T2, T3, T4, T5>(source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>, source4: AsyncIterable<T4>, source5: AsyncIterable<T5>): AsyncIterableX<[T, T2, T3, T4, T5]>; | ||
export function zip<T, T2, T3, T4, T5, T6>(source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>, source4: AsyncIterable<T4>, source5: AsyncIterable<T5>, source6: AsyncIterable<T6>): AsyncIterableX<[T, T2, T3, T4, T5, T6]>; | ||
|
||
export function zip<T, R>(project: (values: [T]) => R | Promise<R>, source: AsyncIterable<T>): AsyncIterableX<R>; | ||
export function zip<T, T2, R>(project: (values: [T, T2]) => R | Promise<R>, source: AsyncIterable<T>, source2: AsyncIterable<T2>): AsyncIterableX<R>; | ||
export function zip<T, T2, T3, R>(project: (values: [T, T2, T3]) => R | Promise<R>, source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>): AsyncIterableX<R>; | ||
export function zip<T, T2, T3, T4, R>(project: (values: [T, T2, T3, T4]) => R | Promise<R>, source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>, source4: AsyncIterable<T4>): AsyncIterableX<R>; | ||
export function zip<T, T2, T3, T4, T5, R>(project: (values: [T, T2,T3, T4, T5]) => R | Promise<R>, source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>, source4: AsyncIterable<T4>, source5: AsyncIterable<T5>): AsyncIterableX<R>; | ||
export function zip<T, T2, T3, T4, T5, T6, R>(project: (values: [T, T2, T3, T4, T5, T6]) => R | Promise<R>, source: AsyncIterable<T>, source2: AsyncIterable<T2>, source3: AsyncIterable<T3>, source4: AsyncIterable<T4>, source5: AsyncIterable<T5>, source6: AsyncIterable<T6>): AsyncIterableX<R>; | ||
|
||
export function zip<T>(...sources: AsyncIterable<T>[]): AsyncIterableX<T[]>; | ||
export function zip<T, R>(project: (values: T[]) => R | Promise<R>, ...sources: AsyncIterable<T>[]): AsyncIterableX<R>; | ||
/* tslint:enable:max-line-length */ | ||
export function zip<T, R>(...sources: any[]): AsyncIterableX<R> { | ||
let fn = sources.shift() as (values: any[]) => R | Promise<R>; | ||
if (typeof fn !== 'function') { | ||
sources.push(fn); | ||
fn = identityAsync; | ||
} | ||
return new ZipAsyncIterable<T, R>(sources as AsyncIterable<T>[], fn); | ||
} |
Oops, something went wrong.