Front end of the Slack clone application.

debounceTime.js 4.7KB

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