import { fromArray } from './fromArray'; import { isArray } from '../util/isArray'; import { Subscriber } from '../Subscriber'; import { iterator as Symbol_iterator } from '../../internal/symbol/iterator'; import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe'; export function zip(...observables) { const resultSelector = observables[observables.length - 1]; if (typeof resultSelector === 'function') { observables.pop(); } return fromArray(observables, undefined).lift(new ZipOperator(resultSelector)); } export class ZipOperator { constructor(resultSelector) { this.resultSelector = resultSelector; } call(subscriber, source) { return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector)); } } export class ZipSubscriber extends Subscriber { constructor(destination, resultSelector, values = Object.create(null)) { super(destination); this.resultSelector = resultSelector; this.iterators = []; this.active = 0; this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : undefined; } _next(value) { const iterators = this.iterators; if (isArray(value)) { iterators.push(new StaticArrayIterator(value)); } else if (typeof value[Symbol_iterator] === 'function') { iterators.push(new StaticIterator(value[Symbol_iterator]())); } else { iterators.push(new ZipBufferIterator(this.destination, this, value)); } } _complete() { const iterators = this.iterators; const len = iterators.length; this.unsubscribe(); if (len === 0) { this.destination.complete(); return; } this.active = len; for (let i = 0; i < len; i++) { let iterator = iterators[i]; if (iterator.stillUnsubscribed) { const destination = this.destination; destination.add(iterator.subscribe()); } else { this.active--; } } } notifyInactive() { this.active--; if (this.active === 0) { this.destination.complete(); } } checkIterators() { const iterators = this.iterators; const len = iterators.length; const destination = this.destination; 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(); if (iterator.hasCompleted()) { shouldComplete = true; } if (result.done) { destination.complete(); return; } args.push(result.value); } if (this.resultSelector) { this._tryresultSelector(args); } else { destination.next(args); } if (shouldComplete) { destination.complete(); } } _tryresultSelector(args) { let result; try { result = this.resultSelector.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 Boolean(nextResult && nextResult.done); } } class StaticArrayIterator { constructor(array) { this.array = array; this.index = 0; this.length = 0; this.length = array.length; } [Symbol_iterator]() { return this; } next(value) { 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; } } class ZipBufferIterator extends SimpleOuterSubscriber { constructor(destination, parent, observable) { super(destination); this.parent = parent; this.observable = observable; this.stillUnsubscribed = true; this.buffer = []; this.isComplete = false; } [Symbol_iterator]() { return this; } next() { 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(innerValue) { this.buffer.push(innerValue); this.parent.checkIterators(); } subscribe() { return innerSubscribe(this.observable, new SimpleInnerSubscriber(this)); } } //# sourceMappingURL=zip.js.map
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.js | File | 4.14 KB | 0644 |
|
ConnectableObservable.js.map | File | 3.82 KB | 0644 |
|
SubscribeOnObservable.js | File | 1.14 KB | 0644 |
|
SubscribeOnObservable.js.map | File | 1.2 KB | 0644 |
|
bindCallback.js | File | 2.82 KB | 0644 |
|
bindCallback.js.map | File | 2.83 KB | 0644 |
|
bindNodeCallback.js | File | 3.25 KB | 0644 |
|
bindNodeCallback.js.map | File | 3.23 KB | 0644 |
|
combineLatest.js | File | 2.82 KB | 0644 |
|
combineLatest.js.map | File | 2.86 KB | 0644 |
|
concat.js | File | 200 B | 0644 |
|
concat.js.map | File | 308 B | 0644 |
|
defer.js | File | 516 B | 0644 |
|
defer.js.map | File | 636 B | 0644 |
|
empty.js | File | 375 B | 0644 |
|
empty.js.map | File | 521 B | 0644 |
|
forkJoin.js | File | 2.2 KB | 0644 |
|
forkJoin.js.map | File | 2.43 KB | 0644 |
|
from.js | File | 441 B | 0644 |
|
from.js.map | File | 489 B | 0644 |
|
fromArray.js | File | 399 B | 0644 |
|
fromArray.js.map | File | 446 B | 0644 |
|
fromEvent.js | File | 2.45 KB | 0644 |
|
fromEvent.js.map | File | 2.53 KB | 0644 |
|
fromEventPattern.js | File | 926 B | 0644 |
|
fromEventPattern.js.map | File | 1.09 KB | 0644 |
|
fromIterable.js | File | 499 B | 0644 |
|
fromIterable.js.map | File | 529 B | 0644 |
|
fromPromise.js | File | 415 B | 0644 |
|
fromPromise.js.map | File | 450 B | 0644 |
|
generate.js | File | 3.39 KB | 0644 |
|
generate.js.map | File | 2.88 KB | 0644 |
|
iif.js | File | 237 B | 0644 |
|
iif.js.map | File | 364 B | 0644 |
|
interval.js | File | 758 B | 0644 |
|
interval.js.map | File | 945 B | 0644 |
|
merge.js | File | 901 B | 0644 |
|
merge.js.map | File | 995 B | 0644 |
|
never.js | File | 203 B | 0644 |
|
never.js.map | File | 307 B | 0644 |
|
of.js | File | 413 B | 0644 |
|
of.js.map | File | 511 B | 0644 |
|
onErrorResumeNext.js | File | 763 B | 0644 |
|
onErrorResumeNext.js.map | File | 941 B | 0644 |
|
pairs.js | File | 1.29 KB | 0644 |
|
pairs.js.map | File | 1.52 KB | 0644 |
|
partition.js | File | 441 B | 0644 |
|
partition.js.map | File | 562 B | 0644 |
|
race.js | File | 2.04 KB | 0644 |
|
race.js.map | File | 2.01 KB | 0644 |
|
range.js | File | 1.14 KB | 0644 |
|
range.js.map | File | 1.19 KB | 0644 |
|
throwError.js | File | 421 B | 0644 |
|
throwError.js.map | File | 565 B | 0644 |
|
timer.js | File | 1.12 KB | 0644 |
|
timer.js.map | File | 1.29 KB | 0644 |
|
using.js | File | 898 B | 0644 |
|
using.js.map | File | 939 B | 0644 |
|
zip.js | File | 5.7 KB | 0644 |
|
zip.js.map | File | 5.67 KB | 0644 |
|