debounceTime.js 4.8KB

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