throttleTime.js 3.9 KB

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