1 /** PURE_IMPORTS_START tslib,_util_subscribeToResult,_OuterSubscriber,_InnerSubscriber,_map,_observable_from PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { subscribeToResult } from '../util/subscribeToResult';
4 import { OuterSubscriber } from '../OuterSubscriber';
5 import { InnerSubscriber } from '../InnerSubscriber';
6 import { map } from './map';
7 import { from } from '../observable/from';
8 export function mergeMap(project, resultSelector, concurrent) {
9 if (concurrent === void 0) {
10 concurrent = Number.POSITIVE_INFINITY;
12 if (typeof resultSelector === 'function') {
13 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)); };
15 else if (typeof resultSelector === 'number') {
16 concurrent = resultSelector;
18 return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
20 var MergeMapOperator = /*@__PURE__*/ (function () {
21 function MergeMapOperator(project, concurrent) {
22 if (concurrent === void 0) {
23 concurrent = Number.POSITIVE_INFINITY;
25 this.project = project;
26 this.concurrent = concurrent;
28 MergeMapOperator.prototype.call = function (observer, source) {
29 return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
31 return MergeMapOperator;
33 export { MergeMapOperator };
34 var MergeMapSubscriber = /*@__PURE__*/ (function (_super) {
35 tslib_1.__extends(MergeMapSubscriber, _super);
36 function MergeMapSubscriber(destination, project, concurrent) {
37 if (concurrent === void 0) {
38 concurrent = Number.POSITIVE_INFINITY;
40 var _this = _super.call(this, destination) || this;
41 _this.project = project;
42 _this.concurrent = concurrent;
43 _this.hasCompleted = false;
49 MergeMapSubscriber.prototype._next = function (value) {
50 if (this.active < this.concurrent) {
54 this.buffer.push(value);
57 MergeMapSubscriber.prototype._tryNext = function (value) {
59 var index = this.index++;
61 result = this.project(value, index);
64 this.destination.error(err);
68 this._innerSub(result, value, index);
70 MergeMapSubscriber.prototype._innerSub = function (ish, value, index) {
71 var innerSubscriber = new InnerSubscriber(this, value, index);
72 var destination = this.destination;
73 destination.add(innerSubscriber);
74 var innerSubscription = subscribeToResult(this, ish, undefined, undefined, innerSubscriber);
75 if (innerSubscription !== innerSubscriber) {
76 destination.add(innerSubscription);
79 MergeMapSubscriber.prototype._complete = function () {
80 this.hasCompleted = true;
81 if (this.active === 0 && this.buffer.length === 0) {
82 this.destination.complete();
86 MergeMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
87 this.destination.next(innerValue);
89 MergeMapSubscriber.prototype.notifyComplete = function (innerSub) {
90 var buffer = this.buffer;
91 this.remove(innerSub);
93 if (buffer.length > 0) {
94 this._next(buffer.shift());
96 else if (this.active === 0 && this.hasCompleted) {
97 this.destination.complete();
100 return MergeMapSubscriber;
102 export { MergeMapSubscriber };
103 export var flatMap = mergeMap;
104 //# sourceMappingURL=mergeMap.js.map