throttle.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Definition of the goog.async.Throttle class.
  16. *
  17. * @see ../demos/timers.html
  18. */
  19. goog.provide('goog.Throttle');
  20. goog.provide('goog.async.Throttle');
  21. goog.require('goog.Disposable');
  22. goog.require('goog.Timer');
  23. /**
  24. * Throttle will perform an action that is passed in no more than once
  25. * per interval (specified in milliseconds). If it gets multiple signals
  26. * to perform the action while it is waiting, it will only perform the action
  27. * once at the end of the interval.
  28. * @param {function(this: T, ...?)} listener Function to callback when the
  29. * action is triggered.
  30. * @param {number} interval Interval over which to throttle. The listener can
  31. * only be called once per interval.
  32. * @param {T=} opt_handler Object in whose scope to call the listener.
  33. * @constructor
  34. * @struct
  35. * @extends {goog.Disposable}
  36. * @final
  37. * @template T
  38. */
  39. goog.async.Throttle = function(listener, interval, opt_handler) {
  40. goog.async.Throttle.base(this, 'constructor');
  41. /**
  42. * Function to callback
  43. * @type {function(this: T, ...?)}
  44. * @private
  45. */
  46. this.listener_ =
  47. opt_handler != null ? goog.bind(listener, opt_handler) : listener;
  48. /**
  49. * Interval for the throttle time
  50. * @type {number}
  51. * @private
  52. */
  53. this.interval_ = interval;
  54. /**
  55. * Cached callback function invoked after the throttle timeout completes
  56. * @type {Function}
  57. * @private
  58. */
  59. this.callback_ = goog.bind(this.onTimer_, this);
  60. /**
  61. * The last arguments passed into {@code fire}.
  62. * @private {!IArrayLike}
  63. */
  64. this.args_ = [];
  65. };
  66. goog.inherits(goog.async.Throttle, goog.Disposable);
  67. /**
  68. * A deprecated alias.
  69. * @deprecated Use goog.async.Throttle instead.
  70. * @constructor
  71. * @final
  72. */
  73. goog.Throttle = goog.async.Throttle;
  74. /**
  75. * Indicates that the action is pending and needs to be fired.
  76. * @type {boolean}
  77. * @private
  78. */
  79. goog.async.Throttle.prototype.shouldFire_ = false;
  80. /**
  81. * Indicates the count of nested pauses currently in effect on the throttle.
  82. * When this count is not zero, fired actions will be postponed until the
  83. * throttle is resumed enough times to drop the pause count to zero.
  84. * @type {number}
  85. * @private
  86. */
  87. goog.async.Throttle.prototype.pauseCount_ = 0;
  88. /**
  89. * Timer for scheduling the next callback
  90. * @type {?number}
  91. * @private
  92. */
  93. goog.async.Throttle.prototype.timer_ = null;
  94. /**
  95. * Notifies the throttle that the action has happened. It will throttle the call
  96. * so that the callback is not called too often according to the interval
  97. * parameter passed to the constructor, passing the arguments from the last call
  98. * of this function into the throttled function.
  99. * @param {...?} var_args Arguments to pass on to the throttled function.
  100. */
  101. goog.async.Throttle.prototype.fire = function(var_args) {
  102. this.args_ = arguments;
  103. if (!this.timer_ && !this.pauseCount_) {
  104. this.doAction_();
  105. } else {
  106. this.shouldFire_ = true;
  107. }
  108. };
  109. /**
  110. * Cancels any pending action callback. The throttle can be restarted by
  111. * calling {@link #fire}.
  112. */
  113. goog.async.Throttle.prototype.stop = function() {
  114. if (this.timer_) {
  115. goog.Timer.clear(this.timer_);
  116. this.timer_ = null;
  117. this.shouldFire_ = false;
  118. this.args_ = [];
  119. }
  120. };
  121. /**
  122. * Pauses the throttle. All pending and future action callbacks will be
  123. * delayed until the throttle is resumed. Pauses can be nested.
  124. */
  125. goog.async.Throttle.prototype.pause = function() {
  126. this.pauseCount_++;
  127. };
  128. /**
  129. * Resumes the throttle. If doing so drops the pausing count to zero, pending
  130. * action callbacks will be executed as soon as possible, but still no sooner
  131. * than an interval's delay after the previous call. Future action callbacks
  132. * will be executed as normal.
  133. */
  134. goog.async.Throttle.prototype.resume = function() {
  135. this.pauseCount_--;
  136. if (!this.pauseCount_ && this.shouldFire_ && !this.timer_) {
  137. this.shouldFire_ = false;
  138. this.doAction_();
  139. }
  140. };
  141. /** @override */
  142. goog.async.Throttle.prototype.disposeInternal = function() {
  143. goog.async.Throttle.base(this, 'disposeInternal');
  144. this.stop();
  145. };
  146. /**
  147. * Handler for the timer to fire the throttle
  148. * @private
  149. */
  150. goog.async.Throttle.prototype.onTimer_ = function() {
  151. this.timer_ = null;
  152. if (this.shouldFire_ && !this.pauseCount_) {
  153. this.shouldFire_ = false;
  154. this.doAction_();
  155. }
  156. };
  157. /**
  158. * Calls the callback
  159. * @private
  160. */
  161. goog.async.Throttle.prototype.doAction_ = function() {
  162. this.timer_ = goog.Timer.callOnce(this.callback_, this.interval_);
  163. this.listener_.apply(null, this.args_);
  164. };