throttleTime.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /** PURE_IMPORTS_START .._Subscriber,.._scheduler_async,._throttle 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 { Subscriber } from '../Subscriber';
  10. import { async } from '../scheduler/async';
  11. import { defaultThrottleConfig } from './throttle';
  12. /**
  13. * Emits a value from the source Observable, then ignores subsequent source
  14. * values for `duration` milliseconds, then repeats this process.
  15. *
  16. * <span class="informal">Lets a value pass, then ignores source values for the
  17. * next `duration` milliseconds.</span>
  18. *
  19. * <img src="./img/throttleTime.png" width="100%">
  20. *
  21. * `throttleTime` emits the source Observable values on the output Observable
  22. * when its internal timer is disabled, and ignores source values when the timer
  23. * is enabled. Initially, the timer is disabled. As soon as the first source
  24. * value arrives, it is forwarded to the output Observable, and then the timer
  25. * is enabled. After `duration` milliseconds (or the time unit determined
  26. * internally by the optional `scheduler`) has passed, the timer is disabled,
  27. * and this process repeats for the next source value. Optionally takes a
  28. * {@link IScheduler} for managing timers.
  29. *
  30. * @example <caption>Emit clicks at a rate of at most one click per second</caption>
  31. * var clicks = Rx.Observable.fromEvent(document, 'click');
  32. * var result = clicks.throttleTime(1000);
  33. * result.subscribe(x => console.log(x));
  34. *
  35. * @see {@link auditTime}
  36. * @see {@link debounceTime}
  37. * @see {@link delay}
  38. * @see {@link sampleTime}
  39. * @see {@link throttle}
  40. *
  41. * @param {number} duration Time to wait before emitting another value after
  42. * emitting the last value, measured in milliseconds or the time unit determined
  43. * internally by the optional `scheduler`.
  44. * @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for
  45. * managing the timers that handle the throttling.
  46. * @return {Observable<T>} An Observable that performs the throttle operation to
  47. * limit the rate of emissions from the source.
  48. * @method throttleTime
  49. * @owner Observable
  50. */
  51. export function throttleTime(duration, scheduler, config) {
  52. if (scheduler === void 0) {
  53. scheduler = async;
  54. }
  55. if (config === void 0) {
  56. config = defaultThrottleConfig;
  57. }
  58. return function (source) { return source.lift(new ThrottleTimeOperator(duration, scheduler, config.leading, config.trailing)); };
  59. }
  60. var ThrottleTimeOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  61. function ThrottleTimeOperator(duration, scheduler, leading, trailing) {
  62. this.duration = duration;
  63. this.scheduler = scheduler;
  64. this.leading = leading;
  65. this.trailing = trailing;
  66. }
  67. ThrottleTimeOperator.prototype.call = function (subscriber, source) {
  68. return source.subscribe(new ThrottleTimeSubscriber(subscriber, this.duration, this.scheduler, this.leading, this.trailing));
  69. };
  70. return ThrottleTimeOperator;
  71. }());
  72. /**
  73. * We need this JSDoc comment for affecting ESDoc.
  74. * @ignore
  75. * @extends {Ignored}
  76. */
  77. var ThrottleTimeSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  78. __extends(ThrottleTimeSubscriber, _super);
  79. function ThrottleTimeSubscriber(destination, duration, scheduler, leading, trailing) {
  80. _super.call(this, destination);
  81. this.duration = duration;
  82. this.scheduler = scheduler;
  83. this.leading = leading;
  84. this.trailing = trailing;
  85. this._hasTrailingValue = false;
  86. this._trailingValue = null;
  87. }
  88. ThrottleTimeSubscriber.prototype._next = function (value) {
  89. if (this.throttled) {
  90. if (this.trailing) {
  91. this._trailingValue = value;
  92. this._hasTrailingValue = true;
  93. }
  94. }
  95. else {
  96. this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.duration, { subscriber: this }));
  97. if (this.leading) {
  98. this.destination.next(value);
  99. }
  100. }
  101. };
  102. ThrottleTimeSubscriber.prototype.clearThrottle = function () {
  103. var throttled = this.throttled;
  104. if (throttled) {
  105. if (this.trailing && this._hasTrailingValue) {
  106. this.destination.next(this._trailingValue);
  107. this._trailingValue = null;
  108. this._hasTrailingValue = false;
  109. }
  110. throttled.unsubscribe();
  111. this.remove(throttled);
  112. this.throttled = null;
  113. }
  114. };
  115. return ThrottleTimeSubscriber;
  116. }(Subscriber));
  117. function dispatchNext(arg) {
  118. var subscriber = arg.subscriber;
  119. subscriber.clearThrottle();
  120. }
  121. //# sourceMappingURL=throttleTime.js.map