import { Observable, ObservableInput } from '../Observable'; import { ArrayObservable } from '../observable/ArrayObservable'; import { isArray } from '../util/isArray'; import { Operator } from '../Operator'; import { PartialObserver } from '../Observer'; import { Subscriber } from '../Subscriber'; import { OuterSubscriber } from '../OuterSubscriber'; import { InnerSubscriber } from '../InnerSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; import { $$iterator } from '../symbol/iterator'; /* tslint:disable:max-line-length */ export function zipProto(this: Observable, project: (v1: T) => R): Observable; export function zipProto(this: Observable, v2: ObservableInput, project: (v1: T, v2: T2) => R): Observable; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, project: (v1: T, v2: T2, v3: T3) => R): Observable; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4) => R): Observable; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => R): Observable; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => R): Observable ; export function zipProto(this: Observable, v2: ObservableInput): Observable<[T, T2]>; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput): Observable<[T, T2, T3]>; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput): Observable<[T, T2, T3, T4]>; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput): Observable<[T, T2, T3, T4, T5]>; export function zipProto(this: Observable, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput): Observable<[T, T2, T3, T4, T5, T6]> ; export function zipProto(this: Observable, ...observables: Array | ((...values: Array) => R)>): Observable; export function zipProto(this: Observable, array: Array>): Observable; export function zipProto(this: Observable, array: Array>, project: (v1: T, ...values: Array) => R): Observable; /* tslint:disable:max-line-length */ /** * @param observables * @return {Observable} * @method zip * @owner Observable */ export function zipProto(this: Observable, ...observables: Array | ((...values: Array) => R)>): Observable { return this.lift.call(zipStatic(this, ...observables)); } /* tslint:disable:max-line-length */ export function zipStatic(v1: ObservableInput, v2: ObservableInput): Observable<[T, T2]>; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput): Observable<[T, T2, T3]>; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput): Observable<[T, T2, T3, T4]>; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput): Observable<[T, T2, T3, T4, T5]>; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput): Observable<[T, T2, T3, T4, T5, T6]>; export function zipStatic(v1: ObservableInput, project: (v1: T) => R): Observable; export function zipStatic(v1: ObservableInput, v2: ObservableInput, project: (v1: T, v2: T2) => R): Observable; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, project: (v1: T, v2: T2, v3: T3) => R): Observable; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4) => R): Observable; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => R): Observable; export function zipStatic(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => R): Observable; export function zipStatic(array: ObservableInput[]): Observable; export function zipStatic(array: ObservableInput[]): Observable; export function zipStatic(array: ObservableInput[], project: (...values: Array) => R): Observable; export function zipStatic(array: ObservableInput[], project: (...values: Array) => R): Observable; export function zipStatic(...observables: Array>): Observable; export function zipStatic(...observables: Array | ((...values: Array) => R)>): Observable; export function zipStatic(...observables: Array | ((...values: Array) => R)>): Observable; /* tslint:enable:max-line-length */ /** * @param observables * @return {Observable} * @static true * @name zip * @owner Observable */ export function zipStatic(...observables: Array | ((...values: Array) => R)>): Observable { const project = <((...ys: Array) => R)> observables[observables.length - 1]; if (typeof project === 'function') { observables.pop(); } return new ArrayObservable(observables).lift(new ZipOperator(project)); } export class ZipOperator implements Operator { project: (...values: Array) => R; constructor(project?: (...values: Array) => R) { this.project = project; } call(subscriber: Subscriber, source: any): any { return source.subscribe(new ZipSubscriber(subscriber, this.project)); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ export class ZipSubscriber extends Subscriber { private values: any; private project: (...values: Array) => R; private iterators: LookAheadIterator[] = []; private active = 0; constructor(destination: Subscriber, project?: (...values: Array) => R, values: any = Object.create(null)) { super(destination); this.project = (typeof project === 'function') ? project : null; this.values = values; } protected _next(value: any) { const iterators = this.iterators; if (isArray(value)) { iterators.push(new StaticArrayIterator(value)); } else if (typeof value[$$iterator] === 'function') { iterators.push(new StaticIterator(value[$$iterator]())); } else { iterators.push(new ZipBufferIterator(this.destination, this, value)); } } protected _complete() { const iterators = this.iterators; const len = iterators.length; this.active = len; for (let i = 0; i < len; i++) { let iterator: ZipBufferIterator = iterators[i]; if (iterator.stillUnsubscribed) { this.add(iterator.subscribe(iterator, i)); } else { this.active--; // not an observable } } } notifyInactive() { this.active--; if (this.active === 0) { this.destination.complete(); } } checkIterators() { const iterators = this.iterators; const len = iterators.length; const destination = this.destination; // abort if not all of them have values for (let i = 0; i < len; i++) { let iterator = iterators[i]; if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) { return; } } let shouldComplete = false; const args: any[] = []; for (let i = 0; i < len; i++) { let iterator = iterators[i]; let result = iterator.next(); // check to see if it's completed now that you've gotten // the next value. if (iterator.hasCompleted()) { shouldComplete = true; } if (result.done) { destination.complete(); return; } args.push(result.value); } if (this.project) { this._tryProject(args); } else { destination.next(args); } if (shouldComplete) { destination.complete(); } } protected _tryProject(args: any[]) { let result: any; try { result = this.project.apply(this, args); } catch (err) { this.destination.error(err); return; } this.destination.next(result); } } interface LookAheadIterator extends Iterator { hasValue(): boolean; hasCompleted(): boolean; } class StaticIterator implements LookAheadIterator { private nextResult: IteratorResult; constructor(private iterator: Iterator) { this.nextResult = iterator.next(); } hasValue() { return true; } next(): IteratorResult { const result = this.nextResult; this.nextResult = this.iterator.next(); return result; } hasCompleted() { const nextResult = this.nextResult; return nextResult && nextResult.done; } } class StaticArrayIterator implements LookAheadIterator { private index = 0; private length = 0; constructor(private array: T[]) { this.length = array.length; } [$$iterator]() { return this; } next(value?: any): IteratorResult { const i = this.index++; const array = this.array; return i < this.length ? { value: array[i], done: false } : { value: null, done: true }; } hasValue() { return this.array.length > this.index; } hasCompleted() { return this.array.length === this.index; } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class ZipBufferIterator extends OuterSubscriber implements LookAheadIterator { stillUnsubscribed = true; buffer: T[] = []; isComplete = false; constructor(destination: PartialObserver, private parent: ZipSubscriber, private observable: Observable) { super(destination); } [$$iterator]() { return this; } // NOTE: there is actually a name collision here with Subscriber.next and Iterator.next // this is legit because `next()` will never be called by a subscription in this case. next(): IteratorResult { const buffer = this.buffer; if (buffer.length === 0 && this.isComplete) { return { value: null, done: true }; } else { return { value: buffer.shift(), done: false }; } } hasValue() { return this.buffer.length > 0; } hasCompleted() { return this.buffer.length === 0 && this.isComplete; } notifyComplete() { if (this.buffer.length > 0) { this.isComplete = true; this.parent.notifyInactive(); } else { this.destination.complete(); } } notifyNext(outerValue: T, innerValue: any, outerIndex: number, innerIndex: number, innerSub: InnerSubscriber): void { this.buffer.push(innerValue); this.parent.checkIterators(); } subscribe(value: any, index: number) { return subscribeToResult(this, this.observable, this, index); } }