UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

228 lines 6.54 kB
import { ArrayObservable } from '../observable/ArrayObservable'; import { isArray } from '../util/isArray'; import { Subscriber } from '../Subscriber'; import { OuterSubscriber } from '../OuterSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; import { $$iterator } from '../symbol/iterator'; /** * @param observables * @return {Observable<R>} * @method zip * @owner Observable */ export function zipProto(...observables) { observables.unshift(this); return zipStatic.apply(this, observables); } /* tslint:enable:max-line-length */ /** * @param observables * @return {Observable<R>} * @static true * @name zip * @owner Observable */ export function zipStatic(...observables) { const project = observables[observables.length - 1]; if (typeof project === 'function') { observables.pop(); } return new ArrayObservable(observables).lift(new ZipOperator(project)); } export class ZipOperator { constructor(project) { this.project = project; } call(subscriber, source) { return source._subscribe(new ZipSubscriber(subscriber, this.project)); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ export class ZipSubscriber extends Subscriber { constructor(destination, project, values = Object.create(null)) { super(destination); this.index = 0; this.iterators = []; this.active = 0; this.project = (typeof project === 'function') ? project : null; this.values = values; } _next(value) { const iterators = this.iterators; const index = this.index++; 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, index)); } } _complete() { const iterators = this.iterators; const len = iterators.length; this.active = len; for (let i = 0; i < len; i++) { let iterator = 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 = []; 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(); } } _tryProject(args) { let result; try { result = this.project.apply(this, args); } catch (err) { this.destination.error(err); return; } this.destination.next(result); } } class StaticIterator { constructor(iterator) { this.iterator = iterator; this.nextResult = iterator.next(); } hasValue() { return true; } next() { const result = this.nextResult; this.nextResult = this.iterator.next(); return result; } hasCompleted() { const nextResult = this.nextResult; return nextResult && nextResult.done; } } class StaticArrayIterator { constructor(array) { this.array = array; this.index = 0; this.length = 0; this.length = array.length; } [$$iterator]() { return this; } next(value) { const i = this.index++; const array = this.array; return i < this.length ? { value: array[i], done: false } : { 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 { constructor(destination, parent, observable, index) { super(destination); this.parent = parent; this.observable = observable; this.index = index; this.stillUnsubscribed = true; this.buffer = []; this.isComplete = false; } [$$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() { const buffer = this.buffer; if (buffer.length === 0 && this.isComplete) { return { 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, innerValue, outerIndex, innerIndex, innerSub) { this.buffer.push(innerValue); this.parent.checkIterators(); } subscribe(value, index) { return subscribeToResult(this, this.observable, this, index); } } //# sourceMappingURL=zip.js.map