mergeMap.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** PURE_IMPORTS_START tslib,_map,_observable_from,_innerSubscribe PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { map } from './map';
  4. import { from } from '../observable/from';
  5. import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
  6. export function mergeMap(project, resultSelector, concurrent) {
  7. if (concurrent === void 0) {
  8. concurrent = Number.POSITIVE_INFINITY;
  9. }
  10. if (typeof resultSelector === 'function') {
  11. 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)); };
  12. }
  13. else if (typeof resultSelector === 'number') {
  14. concurrent = resultSelector;
  15. }
  16. return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); };
  17. }
  18. var MergeMapOperator = /*@__PURE__*/ (function () {
  19. function MergeMapOperator(project, concurrent) {
  20. if (concurrent === void 0) {
  21. concurrent = Number.POSITIVE_INFINITY;
  22. }
  23. this.project = project;
  24. this.concurrent = concurrent;
  25. }
  26. MergeMapOperator.prototype.call = function (observer, source) {
  27. return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
  28. };
  29. return MergeMapOperator;
  30. }());
  31. export { MergeMapOperator };
  32. var MergeMapSubscriber = /*@__PURE__*/ (function (_super) {
  33. tslib_1.__extends(MergeMapSubscriber, _super);
  34. function MergeMapSubscriber(destination, project, concurrent) {
  35. if (concurrent === void 0) {
  36. concurrent = Number.POSITIVE_INFINITY;
  37. }
  38. var _this = _super.call(this, destination) || this;
  39. _this.project = project;
  40. _this.concurrent = concurrent;
  41. _this.hasCompleted = false;
  42. _this.buffer = [];
  43. _this.active = 0;
  44. _this.index = 0;
  45. return _this;
  46. }
  47. MergeMapSubscriber.prototype._next = function (value) {
  48. if (this.active < this.concurrent) {
  49. this._tryNext(value);
  50. }
  51. else {
  52. this.buffer.push(value);
  53. }
  54. };
  55. MergeMapSubscriber.prototype._tryNext = function (value) {
  56. var result;
  57. var index = this.index++;
  58. try {
  59. result = this.project(value, index);
  60. }
  61. catch (err) {
  62. this.destination.error(err);
  63. return;
  64. }
  65. this.active++;
  66. this._innerSub(result);
  67. };
  68. MergeMapSubscriber.prototype._innerSub = function (ish) {
  69. var innerSubscriber = new SimpleInnerSubscriber(this);
  70. var destination = this.destination;
  71. destination.add(innerSubscriber);
  72. var innerSubscription = innerSubscribe(ish, innerSubscriber);
  73. if (innerSubscription !== innerSubscriber) {
  74. destination.add(innerSubscription);
  75. }
  76. };
  77. MergeMapSubscriber.prototype._complete = function () {
  78. this.hasCompleted = true;
  79. if (this.active === 0 && this.buffer.length === 0) {
  80. this.destination.complete();
  81. }
  82. this.unsubscribe();
  83. };
  84. MergeMapSubscriber.prototype.notifyNext = function (innerValue) {
  85. this.destination.next(innerValue);
  86. };
  87. MergeMapSubscriber.prototype.notifyComplete = function () {
  88. var buffer = this.buffer;
  89. this.active--;
  90. if (buffer.length > 0) {
  91. this._next(buffer.shift());
  92. }
  93. else if (this.active === 0 && this.hasCompleted) {
  94. this.destination.complete();
  95. }
  96. };
  97. return MergeMapSubscriber;
  98. }(SimpleOuterSubscriber));
  99. export { MergeMapSubscriber };
  100. export var flatMap = mergeMap;
  101. //# sourceMappingURL=mergeMap.js.map