blob: 0f59df539ae8a12af230ba147a559ae995b2b6fe [file] [log] [blame]
/** PURE_IMPORTS_START .._observable_ArrayObservable,.._util_isArray,.._Subscriber,.._OuterSubscriber,.._util_subscribeToResult,.._symbol_iterator PURE_IMPORTS_END */
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b)
if (b.hasOwnProperty(p))
d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
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 as Symbol_iterator } from '../symbol/iterator';
/* tslint:enable:max-line-length */
/**
* @param observables
* @return {Observable<R>}
* @method zip
* @owner Observable
*/
export function zip() {
var observables = [];
for (var _i = 0; _i < arguments.length; _i++) {
observables[_i - 0] = arguments[_i];
}
return function zipOperatorFunction(source) {
return source.lift.call(zipStatic.apply(void 0, [source].concat(observables)));
};
}
/* tslint:enable:max-line-length */
/**
* Combines multiple Observables to create an Observable whose values are calculated from the values, in order, of each
* of its input Observables.
*
* If the latest parameter is a function, this function is used to compute the created value from the input values.
* Otherwise, an array of the input values is returned.
*
* @example <caption>Combine age and name from different sources</caption>
*
* let age$ = Observable.of<number>(27, 25, 29);
* let name$ = Observable.of<string>('Foo', 'Bar', 'Beer');
* let isDev$ = Observable.of<boolean>(true, true, false);
*
* Observable
* .zip(age$,
* name$,
* isDev$,
* (age: number, name: string, isDev: boolean) => ({ age, name, isDev }))
* .subscribe(x => console.log(x));
*
* // outputs
* // { age: 27, name: 'Foo', isDev: true }
* // { age: 25, name: 'Bar', isDev: true }
* // { age: 29, name: 'Beer', isDev: false }
*
* @param observables
* @return {Observable<R>}
* @static true
* @name zip
* @owner Observable
*/
export function zipStatic() {
var observables = [];
for (var _i = 0; _i < arguments.length; _i++) {
observables[_i - 0] = arguments[_i];
}
var project = observables[observables.length - 1];
if (typeof project === 'function') {
observables.pop();
}
return new ArrayObservable(observables).lift(new ZipOperator(project));
}
export var ZipOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
function ZipOperator(project) {
this.project = project;
}
ZipOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new ZipSubscriber(subscriber, this.project));
};
return ZipOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export var ZipSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
__extends(ZipSubscriber, _super);
function ZipSubscriber(destination, project, values) {
if (values === void 0) {
values = Object.create(null);
}
_super.call(this, destination);
this.iterators = [];
this.active = 0;
this.project = (typeof project === 'function') ? project : null;
this.values = values;
}
ZipSubscriber.prototype._next = function (value) {
var 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));
}
};
ZipSubscriber.prototype._complete = function () {
var iterators = this.iterators;
var len = iterators.length;
if (len === 0) {
this.destination.complete();
return;
}
this.active = len;
for (var i = 0; i < len; i++) {
var iterator = iterators[i];
if (iterator.stillUnsubscribed) {
this.add(iterator.subscribe(iterator, i));
}
else {
this.active--; // not an observable
}
}
};
ZipSubscriber.prototype.notifyInactive = function () {
this.active--;
if (this.active === 0) {
this.destination.complete();
}
};
ZipSubscriber.prototype.checkIterators = function () {
var iterators = this.iterators;
var len = iterators.length;
var destination = this.destination;
// abort if not all of them have values
for (var i = 0; i < len; i++) {
var iterator = iterators[i];
if (typeof iterator.hasValue === 'function' && !iterator.hasValue()) {
return;
}
}
var shouldComplete = false;
var args = [];
for (var i = 0; i < len; i++) {
var iterator = iterators[i];
var 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();
}
};
ZipSubscriber.prototype._tryProject = function (args) {
var result;
try {
result = this.project.apply(this, args);
}
catch (err) {
this.destination.error(err);
return;
}
this.destination.next(result);
};
return ZipSubscriber;
}(Subscriber));
var StaticIterator = /*@__PURE__*/ (/*@__PURE__*/ function () {
function StaticIterator(iterator) {
this.iterator = iterator;
this.nextResult = iterator.next();
}
StaticIterator.prototype.hasValue = function () {
return true;
};
StaticIterator.prototype.next = function () {
var result = this.nextResult;
this.nextResult = this.iterator.next();
return result;
};
StaticIterator.prototype.hasCompleted = function () {
var nextResult = this.nextResult;
return nextResult && nextResult.done;
};
return StaticIterator;
}());
var StaticArrayIterator = /*@__PURE__*/ (/*@__PURE__*/ function () {
function StaticArrayIterator(array) {
this.array = array;
this.index = 0;
this.length = 0;
this.length = array.length;
}
StaticArrayIterator.prototype[Symbol_iterator] = function () {
return this;
};
StaticArrayIterator.prototype.next = function (value) {
var i = this.index++;
var array = this.array;
return i < this.length ? { value: array[i], done: false } : { value: null, done: true };
};
StaticArrayIterator.prototype.hasValue = function () {
return this.array.length > this.index;
};
StaticArrayIterator.prototype.hasCompleted = function () {
return this.array.length === this.index;
};
return StaticArrayIterator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var ZipBufferIterator = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
__extends(ZipBufferIterator, _super);
function ZipBufferIterator(destination, parent, observable) {
_super.call(this, destination);
this.parent = parent;
this.observable = observable;
this.stillUnsubscribed = true;
this.buffer = [];
this.isComplete = false;
}
ZipBufferIterator.prototype[Symbol_iterator] = function () {
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.
ZipBufferIterator.prototype.next = function () {
var buffer = this.buffer;
if (buffer.length === 0 && this.isComplete) {
return { value: null, done: true };
}
else {
return { value: buffer.shift(), done: false };
}
};
ZipBufferIterator.prototype.hasValue = function () {
return this.buffer.length > 0;
};
ZipBufferIterator.prototype.hasCompleted = function () {
return this.buffer.length === 0 && this.isComplete;
};
ZipBufferIterator.prototype.notifyComplete = function () {
if (this.buffer.length > 0) {
this.isComplete = true;
this.parent.notifyInactive();
}
else {
this.destination.complete();
}
};
ZipBufferIterator.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
this.buffer.push(innerValue);
this.parent.checkIterators();
};
ZipBufferIterator.prototype.subscribe = function (value, index) {
return subscribeToResult(this, this.observable, this, index);
};
return ZipBufferIterator;
}(OuterSubscriber));
//# sourceMappingURL=zip.js.map