Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mergeMap.js 3.8KB

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