blob: b7d295adf94247e00252d340ee885e44085fd239 [file] [log] [blame]
/** PURE_IMPORTS_START tslib,_map,_observable_from,_innerSubscribe PURE_IMPORTS_END */
import * as tslib_1 from "tslib";
import { map } from './map';
import { from } from '../observable/from';
import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
export function mergeMap(project, resultSelector, concurrent) {
if (concurrent === void 0) {
concurrent = Number.POSITIVE_INFINITY;
}
if (typeof resultSelector === 'function') {
return function (source) { return source.pipe(mergeMap(function (a, i) { return from(project(a, i)).pipe(map(function (b, ii) { return resultSelector(a, b, i, ii); })); }, concurrent)); };
}
else if (typeof resultSelector === 'number') {
concurrent = resultSelector;
}
return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
}
var MergeMapOperator = /*@__PURE__*/ (function () {
function MergeMapOperator(project, concurrent) {
if (concurrent === void 0) {
concurrent = Number.POSITIVE_INFINITY;
}
this.project = project;
this.concurrent = concurrent;
}
MergeMapOperator.prototype.call = function (observer, source) {
return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
};
return MergeMapOperator;
}());
export { MergeMapOperator };
var MergeMapSubscriber = /*@__PURE__*/ (function (_super) {
tslib_1.__extends(MergeMapSubscriber, _super);
function MergeMapSubscriber(destination, project, concurrent) {
if (concurrent === void 0) {
concurrent = Number.POSITIVE_INFINITY;
}
var _this = _super.call(this, destination) || this;
_this.project = project;
_this.concurrent = concurrent;
_this.hasCompleted = false;
_this.buffer = [];
_this.active = 0;
_this.index = 0;
return _this;
}
MergeMapSubscriber.prototype._next = function (value) {
if (this.active < this.concurrent) {
this._tryNext(value);
}
else {
this.buffer.push(value);
}
};
MergeMapSubscriber.prototype._tryNext = function (value) {
var result;
var index = this.index++;
try {
result = this.project(value, index);
}
catch (err) {
this.destination.error(err);
return;
}
this.active++;
this._innerSub(result);
};
MergeMapSubscriber.prototype._innerSub = function (ish) {
var innerSubscriber = new SimpleInnerSubscriber(this);
var destination = this.destination;
destination.add(innerSubscriber);
var innerSubscription = innerSubscribe(ish, innerSubscriber);
if (innerSubscription !== innerSubscriber) {
destination.add(innerSubscription);
}
};
MergeMapSubscriber.prototype._complete = function () {
this.hasCompleted = true;
if (this.active === 0 && this.buffer.length === 0) {
this.destination.complete();
}
this.unsubscribe();
};
MergeMapSubscriber.prototype.notifyNext = function (innerValue) {
this.destination.next(innerValue);
};
MergeMapSubscriber.prototype.notifyComplete = function () {
var buffer = this.buffer;
this.active--;
if (buffer.length > 0) {
this._next(buffer.shift());
}
else if (this.active === 0 && this.hasCompleted) {
this.destination.complete();
}
};
return MergeMapSubscriber;
}(SimpleOuterSubscriber));
export { MergeMapSubscriber };
export var flatMap = mergeMap;
//# sourceMappingURL=mergeMap.js.map