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.

delayWhen.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Subscriber_1 = require("../Subscriber");
  17. var Observable_1 = require("../Observable");
  18. var OuterSubscriber_1 = require("../OuterSubscriber");
  19. var subscribeToResult_1 = require("../util/subscribeToResult");
  20. function delayWhen(delayDurationSelector, subscriptionDelay) {
  21. if (subscriptionDelay) {
  22. return function (source) {
  23. return new SubscriptionDelayObservable(source, subscriptionDelay)
  24. .lift(new DelayWhenOperator(delayDurationSelector));
  25. };
  26. }
  27. return function (source) { return source.lift(new DelayWhenOperator(delayDurationSelector)); };
  28. }
  29. exports.delayWhen = delayWhen;
  30. var DelayWhenOperator = (function () {
  31. function DelayWhenOperator(delayDurationSelector) {
  32. this.delayDurationSelector = delayDurationSelector;
  33. }
  34. DelayWhenOperator.prototype.call = function (subscriber, source) {
  35. return source.subscribe(new DelayWhenSubscriber(subscriber, this.delayDurationSelector));
  36. };
  37. return DelayWhenOperator;
  38. }());
  39. var DelayWhenSubscriber = (function (_super) {
  40. __extends(DelayWhenSubscriber, _super);
  41. function DelayWhenSubscriber(destination, delayDurationSelector) {
  42. var _this = _super.call(this, destination) || this;
  43. _this.delayDurationSelector = delayDurationSelector;
  44. _this.completed = false;
  45. _this.delayNotifierSubscriptions = [];
  46. _this.index = 0;
  47. return _this;
  48. }
  49. DelayWhenSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  50. this.destination.next(outerValue);
  51. this.removeSubscription(innerSub);
  52. this.tryComplete();
  53. };
  54. DelayWhenSubscriber.prototype.notifyError = function (error, innerSub) {
  55. this._error(error);
  56. };
  57. DelayWhenSubscriber.prototype.notifyComplete = function (innerSub) {
  58. var value = this.removeSubscription(innerSub);
  59. if (value) {
  60. this.destination.next(value);
  61. }
  62. this.tryComplete();
  63. };
  64. DelayWhenSubscriber.prototype._next = function (value) {
  65. var index = this.index++;
  66. try {
  67. var delayNotifier = this.delayDurationSelector(value, index);
  68. if (delayNotifier) {
  69. this.tryDelay(delayNotifier, value);
  70. }
  71. }
  72. catch (err) {
  73. this.destination.error(err);
  74. }
  75. };
  76. DelayWhenSubscriber.prototype._complete = function () {
  77. this.completed = true;
  78. this.tryComplete();
  79. this.unsubscribe();
  80. };
  81. DelayWhenSubscriber.prototype.removeSubscription = function (subscription) {
  82. subscription.unsubscribe();
  83. var subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
  84. if (subscriptionIdx !== -1) {
  85. this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
  86. }
  87. return subscription.outerValue;
  88. };
  89. DelayWhenSubscriber.prototype.tryDelay = function (delayNotifier, value) {
  90. var notifierSubscription = subscribeToResult_1.subscribeToResult(this, delayNotifier, value);
  91. if (notifierSubscription && !notifierSubscription.closed) {
  92. var destination = this.destination;
  93. destination.add(notifierSubscription);
  94. this.delayNotifierSubscriptions.push(notifierSubscription);
  95. }
  96. };
  97. DelayWhenSubscriber.prototype.tryComplete = function () {
  98. if (this.completed && this.delayNotifierSubscriptions.length === 0) {
  99. this.destination.complete();
  100. }
  101. };
  102. return DelayWhenSubscriber;
  103. }(OuterSubscriber_1.OuterSubscriber));
  104. var SubscriptionDelayObservable = (function (_super) {
  105. __extends(SubscriptionDelayObservable, _super);
  106. function SubscriptionDelayObservable(source, subscriptionDelay) {
  107. var _this = _super.call(this) || this;
  108. _this.source = source;
  109. _this.subscriptionDelay = subscriptionDelay;
  110. return _this;
  111. }
  112. SubscriptionDelayObservable.prototype._subscribe = function (subscriber) {
  113. this.subscriptionDelay.subscribe(new SubscriptionDelaySubscriber(subscriber, this.source));
  114. };
  115. return SubscriptionDelayObservable;
  116. }(Observable_1.Observable));
  117. var SubscriptionDelaySubscriber = (function (_super) {
  118. __extends(SubscriptionDelaySubscriber, _super);
  119. function SubscriptionDelaySubscriber(parent, source) {
  120. var _this = _super.call(this) || this;
  121. _this.parent = parent;
  122. _this.source = source;
  123. _this.sourceSubscribed = false;
  124. return _this;
  125. }
  126. SubscriptionDelaySubscriber.prototype._next = function (unused) {
  127. this.subscribeToSource();
  128. };
  129. SubscriptionDelaySubscriber.prototype._error = function (err) {
  130. this.unsubscribe();
  131. this.parent.error(err);
  132. };
  133. SubscriptionDelaySubscriber.prototype._complete = function () {
  134. this.unsubscribe();
  135. this.subscribeToSource();
  136. };
  137. SubscriptionDelaySubscriber.prototype.subscribeToSource = function () {
  138. if (!this.sourceSubscribed) {
  139. this.sourceSubscribed = true;
  140. this.unsubscribe();
  141. this.source.subscribe(this.parent);
  142. }
  143. };
  144. return SubscriptionDelaySubscriber;
  145. }(Subscriber_1.Subscriber));
  146. //# sourceMappingURL=delayWhen.js.map