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