1 import { fromArray } from './fromArray';
2 import { isArray } from '../util/isArray';
3 import { Subscriber } from '../Subscriber';
4 import { OuterSubscriber } from '../OuterSubscriber';
5 import { subscribeToResult } from '../util/subscribeToResult';
6 import { iterator as Symbol_iterator } from '../../internal/symbol/iterator';
7 export function zip(...observables) {
8 const resultSelector = observables[observables.length - 1];
9 if (typeof resultSelector === 'function') {
12 return fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
14 export class ZipOperator {
15 constructor(resultSelector) {
16 this.resultSelector = resultSelector;
18 call(subscriber, source) {
19 return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
22 export class ZipSubscriber extends Subscriber {
23 constructor(destination, resultSelector, values = Object.create(null)) {
27 this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : null;
31 const iterators = this.iterators;
33 iterators.push(new StaticArrayIterator(value));
35 else if (typeof value[Symbol_iterator] === 'function') {
36 iterators.push(new StaticIterator(value[Symbol_iterator]()));
39 iterators.push(new ZipBufferIterator(this.destination, this, value));
43 const iterators = this.iterators;
44 const len = iterators.length;
47 this.destination.complete();
51 for (let i = 0; i < len; i++) {
52 let iterator = iterators[i];
53 if (iterator.stillUnsubscribed) {
54 const destination = this.destination;
55 destination.add(iterator.subscribe(iterator, i));
64 if (this.active === 0) {
65 this.destination.complete();
69 const iterators = this.iterators;
70 const len = iterators.length;
71 const destination = this.destination;
72 for (let i = 0; i < len; i++) {
73 let iterator = iterators[i];
74 if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
78 let shouldComplete = false;
80 for (let i = 0; i < len; i++) {
81 let iterator = iterators[i];
82 let result = iterator.next();
83 if (iterator.hasCompleted()) {
84 shouldComplete = true;
87 destination.complete();
90 args.push(result.value);
92 if (this.resultSelector) {
93 this._tryresultSelector(args);
96 destination.next(args);
99 destination.complete();
102 _tryresultSelector(args) {
105 result = this.resultSelector.apply(this, args);
108 this.destination.error(err);
111 this.destination.next(result);
114 class StaticIterator {
115 constructor(iterator) {
116 this.iterator = iterator;
117 this.nextResult = iterator.next();
123 const result = this.nextResult;
124 this.nextResult = this.iterator.next();
128 const nextResult = this.nextResult;
129 return nextResult && nextResult.done;
132 class StaticArrayIterator {
137 this.length = array.length;
139 [Symbol_iterator]() {
143 const i = this.index++;
144 const array = this.array;
145 return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
148 return this.array.length > this.index;
151 return this.array.length === this.index;
154 class ZipBufferIterator extends OuterSubscriber {
155 constructor(destination, parent, observable) {
157 this.parent = parent;
158 this.observable = observable;
159 this.stillUnsubscribed = true;
161 this.isComplete = false;
163 [Symbol_iterator]() {
167 const buffer = this.buffer;
168 if (buffer.length === 0 && this.isComplete) {
169 return { value: null, done: true };
172 return { value: buffer.shift(), done: false };
176 return this.buffer.length > 0;
179 return this.buffer.length === 0 && this.isComplete;
182 if (this.buffer.length > 0) {
183 this.isComplete = true;
184 this.parent.notifyInactive();
187 this.destination.complete();
190 notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
191 this.buffer.push(innerValue);
192 this.parent.checkIterators();
194 subscribe(value, index) {
195 return subscribeToResult(this, this.observable, this, index);
198 //# sourceMappingURL=zip.js.map