1 import { fromArray } from './fromArray';
2 import { isArray } from '../util/isArray';
3 import { Subscriber } from '../Subscriber';
4 import { iterator as Symbol_iterator } from '../../internal/symbol/iterator';
5 import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
6 export function zip(...observables) {
7 const resultSelector = observables[observables.length - 1];
8 if (typeof resultSelector === 'function') {
11 return fromArray(observables, undefined).lift(new ZipOperator(resultSelector));
13 export class ZipOperator {
14 constructor(resultSelector) {
15 this.resultSelector = resultSelector;
17 call(subscriber, source) {
18 return source.subscribe(new ZipSubscriber(subscriber, this.resultSelector));
21 export class ZipSubscriber extends Subscriber {
22 constructor(destination, resultSelector, values = Object.create(null)) {
24 this.resultSelector = resultSelector;
27 this.resultSelector = (typeof resultSelector === 'function') ? resultSelector : undefined;
30 const iterators = this.iterators;
32 iterators.push(new StaticArrayIterator(value));
34 else if (typeof value[Symbol_iterator] === 'function') {
35 iterators.push(new StaticIterator(value[Symbol_iterator]()));
38 iterators.push(new ZipBufferIterator(this.destination, this, value));
42 const iterators = this.iterators;
43 const len = iterators.length;
46 this.destination.complete();
50 for (let i = 0; i < len; i++) {
51 let iterator = iterators[i];
52 if (iterator.stillUnsubscribed) {
53 const destination = this.destination;
54 destination.add(iterator.subscribe());
63 if (this.active === 0) {
64 this.destination.complete();
68 const iterators = this.iterators;
69 const len = iterators.length;
70 const destination = this.destination;
71 for (let i = 0; i < len; i++) {
72 let iterator = iterators[i];
73 if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
77 let shouldComplete = false;
79 for (let i = 0; i < len; i++) {
80 let iterator = iterators[i];
81 let result = iterator.next();
82 if (iterator.hasCompleted()) {
83 shouldComplete = true;
86 destination.complete();
89 args.push(result.value);
91 if (this.resultSelector) {
92 this._tryresultSelector(args);
95 destination.next(args);
98 destination.complete();
101 _tryresultSelector(args) {
104 result = this.resultSelector.apply(this, args);
107 this.destination.error(err);
110 this.destination.next(result);
113 class StaticIterator {
114 constructor(iterator) {
115 this.iterator = iterator;
116 this.nextResult = iterator.next();
122 const result = this.nextResult;
123 this.nextResult = this.iterator.next();
127 const nextResult = this.nextResult;
128 return Boolean(nextResult && nextResult.done);
131 class StaticArrayIterator {
136 this.length = array.length;
138 [Symbol_iterator]() {
142 const i = this.index++;
143 const array = this.array;
144 return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
147 return this.array.length > this.index;
150 return this.array.length === this.index;
153 class ZipBufferIterator extends SimpleOuterSubscriber {
154 constructor(destination, parent, observable) {
156 this.parent = parent;
157 this.observable = observable;
158 this.stillUnsubscribed = true;
160 this.isComplete = false;
162 [Symbol_iterator]() {
166 const buffer = this.buffer;
167 if (buffer.length === 0 && this.isComplete) {
168 return { value: null, done: true };
171 return { value: buffer.shift(), done: false };
175 return this.buffer.length > 0;
178 return this.buffer.length === 0 && this.isComplete;
181 if (this.buffer.length > 0) {
182 this.isComplete = true;
183 this.parent.notifyInactive();
186 this.destination.complete();
189 notifyNext(innerValue) {
190 this.buffer.push(innerValue);
191 this.parent.checkIterators();
194 return innerSubscribe(this.observable, new SimpleInnerSubscriber(this));
197 //# sourceMappingURL=zip.js.map