delayWhen.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /** PURE_IMPORTS_START tslib,_Subscriber,_Observable,_OuterSubscriber,_util_subscribeToResult PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. import { Observable } from '../Observable';
  5. import { OuterSubscriber } from '../OuterSubscriber';
  6. import { subscribeToResult } from '../util/subscribeToResult';
  7. export function delayWhen(delayDurationSelector, subscriptionDelay) {
  8. if (subscriptionDelay) {
  9. return function (source) {
  10. return new SubscriptionDelayObservable(source, subscriptionDelay)
  11. .lift(new DelayWhenOperator(delayDurationSelector));
  12. };
  13. }
  14. return function (source) { return source.lift(new DelayWhenOperator(delayDurationSelector)); };
  15. }
  16. var DelayWhenOperator = /*@__PURE__*/ (function () {
  17. function DelayWhenOperator(delayDurationSelector) {
  18. this.delayDurationSelector = delayDurationSelector;
  19. }
  20. DelayWhenOperator.prototype.call = function (subscriber, source) {
  21. return source.subscribe(new DelayWhenSubscriber(subscriber, this.delayDurationSelector));
  22. };
  23. return DelayWhenOperator;
  24. }());
  25. var DelayWhenSubscriber = /*@__PURE__*/ (function (_super) {
  26. tslib_1.__extends(DelayWhenSubscriber, _super);
  27. function DelayWhenSubscriber(destination, delayDurationSelector) {
  28. var _this = _super.call(this, destination) || this;
  29. _this.delayDurationSelector = delayDurationSelector;
  30. _this.completed = false;
  31. _this.delayNotifierSubscriptions = [];
  32. _this.index = 0;
  33. return _this;
  34. }
  35. DelayWhenSubscriber.prototype.notifyNext = function (outerValue, _innerValue, _outerIndex, _innerIndex, innerSub) {
  36. this.destination.next(outerValue);
  37. this.removeSubscription(innerSub);
  38. this.tryComplete();
  39. };
  40. DelayWhenSubscriber.prototype.notifyError = function (error, innerSub) {
  41. this._error(error);
  42. };
  43. DelayWhenSubscriber.prototype.notifyComplete = function (innerSub) {
  44. var value = this.removeSubscription(innerSub);
  45. if (value) {
  46. this.destination.next(value);
  47. }
  48. this.tryComplete();
  49. };
  50. DelayWhenSubscriber.prototype._next = function (value) {
  51. var index = this.index++;
  52. try {
  53. var delayNotifier = this.delayDurationSelector(value, index);
  54. if (delayNotifier) {
  55. this.tryDelay(delayNotifier, value);
  56. }
  57. }
  58. catch (err) {
  59. this.destination.error(err);
  60. }
  61. };
  62. DelayWhenSubscriber.prototype._complete = function () {
  63. this.completed = true;
  64. this.tryComplete();
  65. this.unsubscribe();
  66. };
  67. DelayWhenSubscriber.prototype.removeSubscription = function (subscription) {
  68. subscription.unsubscribe();
  69. var subscriptionIdx = this.delayNotifierSubscriptions.indexOf(subscription);
  70. if (subscriptionIdx !== -1) {
  71. this.delayNotifierSubscriptions.splice(subscriptionIdx, 1);
  72. }
  73. return subscription.outerValue;
  74. };
  75. DelayWhenSubscriber.prototype.tryDelay = function (delayNotifier, value) {
  76. var notifierSubscription = subscribeToResult(this, delayNotifier, value);
  77. if (notifierSubscription && !notifierSubscription.closed) {
  78. var destination = this.destination;
  79. destination.add(notifierSubscription);
  80. this.delayNotifierSubscriptions.push(notifierSubscription);
  81. }
  82. };
  83. DelayWhenSubscriber.prototype.tryComplete = function () {
  84. if (this.completed && this.delayNotifierSubscriptions.length === 0) {
  85. this.destination.complete();
  86. }
  87. };
  88. return DelayWhenSubscriber;
  89. }(OuterSubscriber));
  90. var SubscriptionDelayObservable = /*@__PURE__*/ (function (_super) {
  91. tslib_1.__extends(SubscriptionDelayObservable, _super);
  92. function SubscriptionDelayObservable(source, subscriptionDelay) {
  93. var _this = _super.call(this) || this;
  94. _this.source = source;
  95. _this.subscriptionDelay = subscriptionDelay;
  96. return _this;
  97. }
  98. SubscriptionDelayObservable.prototype._subscribe = function (subscriber) {
  99. this.subscriptionDelay.subscribe(new SubscriptionDelaySubscriber(subscriber, this.source));
  100. };
  101. return SubscriptionDelayObservable;
  102. }(Observable));
  103. var SubscriptionDelaySubscriber = /*@__PURE__*/ (function (_super) {
  104. tslib_1.__extends(SubscriptionDelaySubscriber, _super);
  105. function SubscriptionDelaySubscriber(parent, source) {
  106. var _this = _super.call(this) || this;
  107. _this.parent = parent;
  108. _this.source = source;
  109. _this.sourceSubscribed = false;
  110. return _this;
  111. }
  112. SubscriptionDelaySubscriber.prototype._next = function (unused) {
  113. this.subscribeToSource();
  114. };
  115. SubscriptionDelaySubscriber.prototype._error = function (err) {
  116. this.unsubscribe();
  117. this.parent.error(err);
  118. };
  119. SubscriptionDelaySubscriber.prototype._complete = function () {
  120. this.unsubscribe();
  121. this.subscribeToSource();
  122. };
  123. SubscriptionDelaySubscriber.prototype.subscribeToSource = function () {
  124. if (!this.sourceSubscribed) {
  125. this.sourceSubscribed = true;
  126. this.unsubscribe();
  127. this.source.subscribe(this.parent);
  128. }
  129. };
  130. return SubscriptionDelaySubscriber;
  131. }(Subscriber));
  132. //# sourceMappingURL=delayWhen.js.map