mergeMap.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 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)); };
  25. }
  26. else if (typeof resultSelector === 'number') {
  27. concurrent = resultSelector;
  28. }
  29. return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
  30. }
  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;
  37. }
  38. MergeMapOperator.prototype.call = function (observer, source) {
  39. return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
  40. };
  41. return MergeMapOperator;
  42. }());
  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;
  52. _this.buffer = [];
  53. _this.active = 0;
  54. _this.index = 0;
  55. return _this;
  56. }
  57. MergeMapSubscriber.prototype._next = function (value) {
  58. if (this.active < this.concurrent) {
  59. this._tryNext(value);
  60. }
  61. else {
  62. this.buffer.push(value);
  63. }
  64. };
  65. MergeMapSubscriber.prototype._tryNext = function (value) {
  66. var result;
  67. var index = this.index++;
  68. try {
  69. result = this.project(value, index);
  70. }
  71. catch (err) {
  72. this.destination.error(err);
  73. return;
  74. }
  75. this.active++;
  76. this._innerSub(result, value, index);
  77. };
  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);
  85. }
  86. };
  87. MergeMapSubscriber.prototype._complete = function () {
  88. this.hasCompleted = true;
  89. if (this.active === 0 && this.buffer.length === 0) {
  90. this.destination.complete();
  91. }
  92. this.unsubscribe();
  93. };
  94. MergeMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  95. this.destination.next(innerValue);
  96. };
  97. MergeMapSubscriber.prototype.notifyComplete = function (innerSub) {
  98. var buffer = this.buffer;
  99. this.remove(innerSub);
  100. this.active--;
  101. if (buffer.length > 0) {
  102. this._next(buffer.shift());
  103. }
  104. else if (this.active === 0 && this.hasCompleted) {
  105. this.destination.complete();
  106. }
  107. };
  108. return MergeMapSubscriber;
  109. }(OuterSubscriber_1.OuterSubscriber));
  110. exports.MergeMapSubscriber = MergeMapSubscriber;
  111. exports.flatMap = mergeMap;
  112. //# sourceMappingURL=mergeMap.js.map