1 | "use strict";
|
---|
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);
|
---|
8 | }
|
---|
9 | return function (d, b) {
|
---|
10 | extendStatics(d, b);
|
---|
11 | function __() { this.constructor = d; }
|
---|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
---|
13 | };
|
---|
14 | })();
|
---|
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)); };
|
---|
23 | }
|
---|
24 | else if (typeof resultSelector === 'number') {
|
---|
25 | concurrent = resultSelector;
|
---|
26 | }
|
---|
27 | return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
|
---|
28 | }
|
---|
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;
|
---|
35 | }
|
---|
36 | MergeMapOperator.prototype.call = function (observer, source) {
|
---|
37 | return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
|
---|
38 | };
|
---|
39 | return MergeMapOperator;
|
---|
40 | }());
|
---|
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;
|
---|
50 | _this.buffer = [];
|
---|
51 | _this.active = 0;
|
---|
52 | _this.index = 0;
|
---|
53 | return _this;
|
---|
54 | }
|
---|
55 | MergeMapSubscriber.prototype._next = function (value) {
|
---|
56 | if (this.active < this.concurrent) {
|
---|
57 | this._tryNext(value);
|
---|
58 | }
|
---|
59 | else {
|
---|
60 | this.buffer.push(value);
|
---|
61 | }
|
---|
62 | };
|
---|
63 | MergeMapSubscriber.prototype._tryNext = function (value) {
|
---|
64 | var result;
|
---|
65 | var index = this.index++;
|
---|
66 | try {
|
---|
67 | result = this.project(value, index);
|
---|
68 | }
|
---|
69 | catch (err) {
|
---|
70 | this.destination.error(err);
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | this.active++;
|
---|
74 | this._innerSub(result);
|
---|
75 | };
|
---|
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);
|
---|
83 | }
|
---|
84 | };
|
---|
85 | MergeMapSubscriber.prototype._complete = function () {
|
---|
86 | this.hasCompleted = true;
|
---|
87 | if (this.active === 0 && this.buffer.length === 0) {
|
---|
88 | this.destination.complete();
|
---|
89 | }
|
---|
90 | this.unsubscribe();
|
---|
91 | };
|
---|
92 | MergeMapSubscriber.prototype.notifyNext = function (innerValue) {
|
---|
93 | this.destination.next(innerValue);
|
---|
94 | };
|
---|
95 | MergeMapSubscriber.prototype.notifyComplete = function () {
|
---|
96 | var buffer = this.buffer;
|
---|
97 | this.active--;
|
---|
98 | if (buffer.length > 0) {
|
---|
99 | this._next(buffer.shift());
|
---|
100 | }
|
---|
101 | else if (this.active === 0 && this.hasCompleted) {
|
---|
102 | this.destination.complete();
|
---|
103 | }
|
---|
104 | };
|
---|
105 | return MergeMapSubscriber;
|
---|
106 | }(innerSubscribe_1.SimpleOuterSubscriber));
|
---|
107 | exports.MergeMapSubscriber = MergeMapSubscriber;
|
---|
108 | exports.flatMap = mergeMap;
|
---|
109 | //# sourceMappingURL=mergeMap.js.map |
---|