throttle.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /** PURE_IMPORTS_START .._OuterSubscriber,.._util_subscribeToResult PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { OuterSubscriber } from '../OuterSubscriber';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. export var defaultThrottleConfig = {
  12. leading: true,
  13. trailing: false
  14. };
  15. /**
  16. * Emits a value from the source Observable, then ignores subsequent source
  17. * values for a duration determined by another Observable, then repeats this
  18. * process.
  19. *
  20. * <span class="informal">It's like {@link throttleTime}, but the silencing
  21. * duration is determined by a second Observable.</span>
  22. *
  23. * <img src="./img/throttle.png" width="100%">
  24. *
  25. * `throttle` emits the source Observable values on the output Observable
  26. * when its internal timer is disabled, and ignores source values when the timer
  27. * is enabled. Initially, the timer is disabled. As soon as the first source
  28. * value arrives, it is forwarded to the output Observable, and then the timer
  29. * is enabled by calling the `durationSelector` function with the source value,
  30. * which returns the "duration" Observable. When the duration Observable emits a
  31. * value or completes, the timer is disabled, and this process repeats for the
  32. * next source value.
  33. *
  34. * @example <caption>Emit clicks at a rate of at most one click per second</caption>
  35. * var clicks = Rx.Observable.fromEvent(document, 'click');
  36. * var result = clicks.throttle(ev => Rx.Observable.interval(1000));
  37. * result.subscribe(x => console.log(x));
  38. *
  39. * @see {@link audit}
  40. * @see {@link debounce}
  41. * @see {@link delayWhen}
  42. * @see {@link sample}
  43. * @see {@link throttleTime}
  44. *
  45. * @param {function(value: T): SubscribableOrPromise} durationSelector A function
  46. * that receives a value from the source Observable, for computing the silencing
  47. * duration for each source value, returned as an Observable or a Promise.
  48. * @param {Object} config a configuration object to define `leading` and `trailing` behavior. Defaults
  49. * to `{ leading: true, trailing: false }`.
  50. * @return {Observable<T>} An Observable that performs the throttle operation to
  51. * limit the rate of emissions from the source.
  52. * @method throttle
  53. * @owner Observable
  54. */
  55. export function throttle(durationSelector, config) {
  56. if (config === void 0) {
  57. config = defaultThrottleConfig;
  58. }
  59. return function (source) { return source.lift(new ThrottleOperator(durationSelector, config.leading, config.trailing)); };
  60. }
  61. var ThrottleOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  62. function ThrottleOperator(durationSelector, leading, trailing) {
  63. this.durationSelector = durationSelector;
  64. this.leading = leading;
  65. this.trailing = trailing;
  66. }
  67. ThrottleOperator.prototype.call = function (subscriber, source) {
  68. return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
  69. };
  70. return ThrottleOperator;
  71. }());
  72. /**
  73. * We need this JSDoc comment for affecting ESDoc
  74. * @ignore
  75. * @extends {Ignored}
  76. */
  77. var ThrottleSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  78. __extends(ThrottleSubscriber, _super);
  79. function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) {
  80. _super.call(this, destination);
  81. this.destination = destination;
  82. this.durationSelector = durationSelector;
  83. this._leading = _leading;
  84. this._trailing = _trailing;
  85. this._hasTrailingValue = false;
  86. }
  87. ThrottleSubscriber.prototype._next = function (value) {
  88. if (this.throttled) {
  89. if (this._trailing) {
  90. this._hasTrailingValue = true;
  91. this._trailingValue = value;
  92. }
  93. }
  94. else {
  95. var duration = this.tryDurationSelector(value);
  96. if (duration) {
  97. this.add(this.throttled = subscribeToResult(this, duration));
  98. }
  99. if (this._leading) {
  100. this.destination.next(value);
  101. if (this._trailing) {
  102. this._hasTrailingValue = true;
  103. this._trailingValue = value;
  104. }
  105. }
  106. }
  107. };
  108. ThrottleSubscriber.prototype.tryDurationSelector = function (value) {
  109. try {
  110. return this.durationSelector(value);
  111. }
  112. catch (err) {
  113. this.destination.error(err);
  114. return null;
  115. }
  116. };
  117. /** @deprecated internal use only */ ThrottleSubscriber.prototype._unsubscribe = function () {
  118. var _a = this, throttled = _a.throttled, _trailingValue = _a._trailingValue, _hasTrailingValue = _a._hasTrailingValue, _trailing = _a._trailing;
  119. this._trailingValue = null;
  120. this._hasTrailingValue = false;
  121. if (throttled) {
  122. this.remove(throttled);
  123. this.throttled = null;
  124. throttled.unsubscribe();
  125. }
  126. };
  127. ThrottleSubscriber.prototype._sendTrailing = function () {
  128. var _a = this, destination = _a.destination, throttled = _a.throttled, _trailing = _a._trailing, _trailingValue = _a._trailingValue, _hasTrailingValue = _a._hasTrailingValue;
  129. if (throttled && _trailing && _hasTrailingValue) {
  130. destination.next(_trailingValue);
  131. this._trailingValue = null;
  132. this._hasTrailingValue = false;
  133. }
  134. };
  135. ThrottleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  136. this._sendTrailing();
  137. this._unsubscribe();
  138. };
  139. ThrottleSubscriber.prototype.notifyComplete = function () {
  140. this._sendTrailing();
  141. this._unsubscribe();
  142. };
  143. return ThrottleSubscriber;
  144. }(OuterSubscriber));
  145. //# sourceMappingURL=throttle.js.map