Path: blob/trunk/third_party/closure/goog/async/throttle.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Definition of the goog.async.Throttle class.16*17* @see ../demos/timers.html18*/1920goog.provide('goog.Throttle');21goog.provide('goog.async.Throttle');2223goog.require('goog.Disposable');24goog.require('goog.Timer');25262728/**29* Throttle will perform an action that is passed in no more than once30* per interval (specified in milliseconds). If it gets multiple signals31* to perform the action while it is waiting, it will only perform the action32* once at the end of the interval.33* @param {function(this: T, ...?)} listener Function to callback when the34* action is triggered.35* @param {number} interval Interval over which to throttle. The listener can36* only be called once per interval.37* @param {T=} opt_handler Object in whose scope to call the listener.38* @constructor39* @struct40* @extends {goog.Disposable}41* @final42* @template T43*/44goog.async.Throttle = function(listener, interval, opt_handler) {45goog.async.Throttle.base(this, 'constructor');4647/**48* Function to callback49* @type {function(this: T, ...?)}50* @private51*/52this.listener_ =53opt_handler != null ? goog.bind(listener, opt_handler) : listener;5455/**56* Interval for the throttle time57* @type {number}58* @private59*/60this.interval_ = interval;6162/**63* Cached callback function invoked after the throttle timeout completes64* @type {Function}65* @private66*/67this.callback_ = goog.bind(this.onTimer_, this);6869/**70* The last arguments passed into {@code fire}.71* @private {!IArrayLike}72*/73this.args_ = [];74};75goog.inherits(goog.async.Throttle, goog.Disposable);76777879/**80* A deprecated alias.81* @deprecated Use goog.async.Throttle instead.82* @constructor83* @final84*/85goog.Throttle = goog.async.Throttle;868788/**89* Indicates that the action is pending and needs to be fired.90* @type {boolean}91* @private92*/93goog.async.Throttle.prototype.shouldFire_ = false;949596/**97* Indicates the count of nested pauses currently in effect on the throttle.98* When this count is not zero, fired actions will be postponed until the99* throttle is resumed enough times to drop the pause count to zero.100* @type {number}101* @private102*/103goog.async.Throttle.prototype.pauseCount_ = 0;104105106/**107* Timer for scheduling the next callback108* @type {?number}109* @private110*/111goog.async.Throttle.prototype.timer_ = null;112113114/**115* Notifies the throttle that the action has happened. It will throttle the call116* so that the callback is not called too often according to the interval117* parameter passed to the constructor, passing the arguments from the last call118* of this function into the throttled function.119* @param {...?} var_args Arguments to pass on to the throttled function.120*/121goog.async.Throttle.prototype.fire = function(var_args) {122this.args_ = arguments;123if (!this.timer_ && !this.pauseCount_) {124this.doAction_();125} else {126this.shouldFire_ = true;127}128};129130131/**132* Cancels any pending action callback. The throttle can be restarted by133* calling {@link #fire}.134*/135goog.async.Throttle.prototype.stop = function() {136if (this.timer_) {137goog.Timer.clear(this.timer_);138this.timer_ = null;139this.shouldFire_ = false;140this.args_ = [];141}142};143144145/**146* Pauses the throttle. All pending and future action callbacks will be147* delayed until the throttle is resumed. Pauses can be nested.148*/149goog.async.Throttle.prototype.pause = function() {150this.pauseCount_++;151};152153154/**155* Resumes the throttle. If doing so drops the pausing count to zero, pending156* action callbacks will be executed as soon as possible, but still no sooner157* than an interval's delay after the previous call. Future action callbacks158* will be executed as normal.159*/160goog.async.Throttle.prototype.resume = function() {161this.pauseCount_--;162if (!this.pauseCount_ && this.shouldFire_ && !this.timer_) {163this.shouldFire_ = false;164this.doAction_();165}166};167168169/** @override */170goog.async.Throttle.prototype.disposeInternal = function() {171goog.async.Throttle.base(this, 'disposeInternal');172this.stop();173};174175176/**177* Handler for the timer to fire the throttle178* @private179*/180goog.async.Throttle.prototype.onTimer_ = function() {181this.timer_ = null;182183if (this.shouldFire_ && !this.pauseCount_) {184this.shouldFire_ = false;185this.doAction_();186}187};188189190/**191* Calls the callback192* @private193*/194goog.async.Throttle.prototype.doAction_ = function() {195this.timer_ = goog.Timer.callOnce(this.callback_, this.interval_);196this.listener_.apply(null, this.args_);197};198199200