debounceTime.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Subscriber } from '../Subscriber';
  2. import { async } from '../scheduler/async';
  3. /**
  4. * Emits a value from the source Observable only after a particular time span
  5. * has passed without another source emission.
  6. *
  7. * <span class="informal">It's like {@link delay}, but passes only the most
  8. * recent value from each burst of emissions.</span>
  9. *
  10. * <img src="./img/debounceTime.png" width="100%">
  11. *
  12. * `debounceTime` delays values emitted by the source Observable, but drops
  13. * previous pending delayed emissions if a new value arrives on the source
  14. * Observable. This operator keeps track of the most recent value from the
  15. * source Observable, and emits that only when `dueTime` enough time has passed
  16. * without any other value appearing on the source Observable. If a new value
  17. * appears before `dueTime` silence occurs, the previous value will be dropped
  18. * and will not be emitted on the output Observable.
  19. *
  20. * This is a rate-limiting operator, because it is impossible for more than one
  21. * value to be emitted in any time window of duration `dueTime`, but it is also
  22. * a delay-like operator since output emissions do not occur at the same time as
  23. * they did on the source Observable. Optionally takes a {@link IScheduler} for
  24. * managing timers.
  25. *
  26. * @example <caption>Emit the most recent click after a burst of clicks</caption>
  27. * var clicks = Rx.Observable.fromEvent(document, 'click');
  28. * var result = clicks.debounceTime(1000);
  29. * result.subscribe(x => console.log(x));
  30. *
  31. * @see {@link auditTime}
  32. * @see {@link debounce}
  33. * @see {@link delay}
  34. * @see {@link sampleTime}
  35. * @see {@link throttleTime}
  36. *
  37. * @param {number} dueTime The timeout duration in milliseconds (or the time
  38. * unit determined internally by the optional `scheduler`) for the window of
  39. * time required to wait for emission silence before emitting the most recent
  40. * source value.
  41. * @param {Scheduler} [scheduler=async] The {@link IScheduler} to use for
  42. * managing the timers that handle the timeout for each value.
  43. * @return {Observable} An Observable that delays the emissions of the source
  44. * Observable by the specified `dueTime`, and may drop some values if they occur
  45. * too frequently.
  46. * @method debounceTime
  47. * @owner Observable
  48. */
  49. export function debounceTime(dueTime, scheduler = async) {
  50. return (source) => source.lift(new DebounceTimeOperator(dueTime, scheduler));
  51. }
  52. class DebounceTimeOperator {
  53. constructor(dueTime, scheduler) {
  54. this.dueTime = dueTime;
  55. this.scheduler = scheduler;
  56. }
  57. call(subscriber, source) {
  58. return source.subscribe(new DebounceTimeSubscriber(subscriber, this.dueTime, this.scheduler));
  59. }
  60. }
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. class DebounceTimeSubscriber extends Subscriber {
  67. constructor(destination, dueTime, scheduler) {
  68. super(destination);
  69. this.dueTime = dueTime;
  70. this.scheduler = scheduler;
  71. this.debouncedSubscription = null;
  72. this.lastValue = null;
  73. this.hasValue = false;
  74. }
  75. _next(value) {
  76. this.clearDebounce();
  77. this.lastValue = value;
  78. this.hasValue = true;
  79. this.add(this.debouncedSubscription = this.scheduler.schedule(dispatchNext, this.dueTime, this));
  80. }
  81. _complete() {
  82. this.debouncedNext();
  83. this.destination.complete();
  84. }
  85. debouncedNext() {
  86. this.clearDebounce();
  87. if (this.hasValue) {
  88. this.destination.next(this.lastValue);
  89. this.lastValue = null;
  90. this.hasValue = false;
  91. }
  92. }
  93. clearDebounce() {
  94. const debouncedSubscription = this.debouncedSubscription;
  95. if (debouncedSubscription !== null) {
  96. this.remove(debouncedSubscription);
  97. debouncedSubscription.unsubscribe();
  98. this.debouncedSubscription = null;
  99. }
  100. }
  101. }
  102. function dispatchNext(subscriber) {
  103. subscriber.debouncedNext();
  104. }
  105. //# sourceMappingURL=debounceTime.js.map