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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, undefined, undefined);
  80. var destination = this.destination;
  81. destination.add(innerSubscriber);
  82. subscribeToResult_1.subscribeToResult(this, ish, value, index, innerSubscriber);
  83. };
  84. MergeMapSubscriber.prototype._complete = function () {
  85. this.hasCompleted = true;
  86. if (this.active === 0 && this.buffer.length === 0) {
  87. this.destination.complete();
  88. }
  89. this.unsubscribe();
  90. };
  91. MergeMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  92. this.destination.next(innerValue);
  93. };
  94. MergeMapSubscriber.prototype.notifyComplete = function (innerSub) {
  95. var buffer = this.buffer;
  96. this.remove(innerSub);
  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. }(OuterSubscriber_1.OuterSubscriber));
  107. exports.MergeMapSubscriber = MergeMapSubscriber;
  108. //# sourceMappingURL=mergeMap.js.map