2 var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 Object.defineProperty(exports, "__esModule", { value: true });
16 var subscribeToResult_1 = require("../util/subscribeToResult");
17 var OuterSubscriber_1 = require("../OuterSubscriber");
18 var InnerSubscriber_1 = require("../InnerSubscriber");
19 var map_1 = require("./map");
20 var from_1 = require("../observable/from");
21 function mergeMap(project, resultSelector, concurrent) {
22 if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
23 if (typeof resultSelector === 'function') {
24 return function (source) { return source.pipe(mergeMap(function (a, i) { return from_1.from(project(a, i)).pipe(map_1.map(function (b, ii) { return resultSelector(a, b, i, ii); })); }, concurrent)); };
26 else if (typeof resultSelector === 'number') {
27 concurrent = resultSelector;
29 return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
31 exports.mergeMap = mergeMap;
32 var MergeMapOperator = (function () {
33 function MergeMapOperator(project, concurrent) {
34 if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
35 this.project = project;
36 this.concurrent = concurrent;
38 MergeMapOperator.prototype.call = function (observer, source) {
39 return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
41 return MergeMapOperator;
43 exports.MergeMapOperator = MergeMapOperator;
44 var MergeMapSubscriber = (function (_super) {
45 __extends(MergeMapSubscriber, _super);
46 function MergeMapSubscriber(destination, project, concurrent) {
47 if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
48 var _this = _super.call(this, destination) || this;
49 _this.project = project;
50 _this.concurrent = concurrent;
51 _this.hasCompleted = false;
57 MergeMapSubscriber.prototype._next = function (value) {
58 if (this.active < this.concurrent) {
62 this.buffer.push(value);
65 MergeMapSubscriber.prototype._tryNext = function (value) {
67 var index = this.index++;
69 result = this.project(value, index);
72 this.destination.error(err);
76 this._innerSub(result, value, index);
78 MergeMapSubscriber.prototype._innerSub = function (ish, value, index) {
79 var innerSubscriber = new InnerSubscriber_1.InnerSubscriber(this, value, index);
80 var destination = this.destination;
81 destination.add(innerSubscriber);
82 var innerSubscription = subscribeToResult_1.subscribeToResult(this, ish, undefined, undefined, innerSubscriber);
83 if (innerSubscription !== innerSubscriber) {
84 destination.add(innerSubscription);
87 MergeMapSubscriber.prototype._complete = function () {
88 this.hasCompleted = true;
89 if (this.active === 0 && this.buffer.length === 0) {
90 this.destination.complete();
94 MergeMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
95 this.destination.next(innerValue);
97 MergeMapSubscriber.prototype.notifyComplete = function (innerSub) {
98 var buffer = this.buffer;
99 this.remove(innerSub);
101 if (buffer.length > 0) {
102 this._next(buffer.shift());
104 else if (this.active === 0 && this.hasCompleted) {
105 this.destination.complete();
108 return MergeMapSubscriber;
109 }(OuterSubscriber_1.OuterSubscriber));
110 exports.MergeMapSubscriber = MergeMapSubscriber;
111 exports.flatMap = mergeMap;
112 //# sourceMappingURL=mergeMap.js.map