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 map_1 = require("./map");
17 var from_1 = require("../observable/from");
18 var innerSubscribe_1 = require("../innerSubscribe");
19 function mergeMap(project, resultSelector, concurrent) {
20 if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
21 if (typeof resultSelector === 'function') {
22 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)); };
24 else if (typeof resultSelector === 'number') {
25 concurrent = resultSelector;
27 return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
29 exports.mergeMap = mergeMap;
30 var MergeMapOperator = (function () {
31 function MergeMapOperator(project, concurrent) {
32 if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
33 this.project = project;
34 this.concurrent = concurrent;
36 MergeMapOperator.prototype.call = function (observer, source) {
37 return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
39 return MergeMapOperator;
41 exports.MergeMapOperator = MergeMapOperator;
42 var MergeMapSubscriber = (function (_super) {
43 __extends(MergeMapSubscriber, _super);
44 function MergeMapSubscriber(destination, project, concurrent) {
45 if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; }
46 var _this = _super.call(this, destination) || this;
47 _this.project = project;
48 _this.concurrent = concurrent;
49 _this.hasCompleted = false;
55 MergeMapSubscriber.prototype._next = function (value) {
56 if (this.active < this.concurrent) {
60 this.buffer.push(value);
63 MergeMapSubscriber.prototype._tryNext = function (value) {
65 var index = this.index++;
67 result = this.project(value, index);
70 this.destination.error(err);
74 this._innerSub(result);
76 MergeMapSubscriber.prototype._innerSub = function (ish) {
77 var innerSubscriber = new innerSubscribe_1.SimpleInnerSubscriber(this);
78 var destination = this.destination;
79 destination.add(innerSubscriber);
80 var innerSubscription = innerSubscribe_1.innerSubscribe(ish, innerSubscriber);
81 if (innerSubscription !== innerSubscriber) {
82 destination.add(innerSubscription);
85 MergeMapSubscriber.prototype._complete = function () {
86 this.hasCompleted = true;
87 if (this.active === 0 && this.buffer.length === 0) {
88 this.destination.complete();
92 MergeMapSubscriber.prototype.notifyNext = function (innerValue) {
93 this.destination.next(innerValue);
95 MergeMapSubscriber.prototype.notifyComplete = function () {
96 var buffer = this.buffer;
98 if (buffer.length > 0) {
99 this._next(buffer.shift());
101 else if (this.active === 0 && this.hasCompleted) {
102 this.destination.complete();
105 return MergeMapSubscriber;
106 }(innerSubscribe_1.SimpleOuterSubscriber));
107 exports.MergeMapSubscriber = MergeMapSubscriber;
108 exports.flatMap = mergeMap;
109 //# sourceMappingURL=mergeMap.js.map