|
| 1 | +// Copyright 2015 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 | +/** |
| 16 | + * @fileoverview Definition of the goog.async.Debouncer class. |
| 17 | + * |
| 18 | + * @see ../demos/timers.html |
| 19 | + */ |
| 20 | + |
| 21 | +goog.provide('goog.async.Debouncer'); |
| 22 | + |
| 23 | +goog.require('goog.Disposable'); |
| 24 | +goog.require('goog.Timer'); |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Debouncer will perform a specified action exactly once for any sequence of |
| 30 | + * signals fired repeatedly so long as they are fired less than a specified |
| 31 | + * interval apart (in milliseconds). Whether it receives one signal or multiple, |
| 32 | + * it will always wait until a full interval has elapsed since the last signal |
| 33 | + * before performing the action. |
| 34 | + * @param {function(this: T)} listener Function to callback when the action is |
| 35 | + * triggered. |
| 36 | + * @param {number} interval Interval over which to debounce. The listener will |
| 37 | + * only be called after the full interval has elapsed since the last signal. |
| 38 | + * @param {T=} opt_handler Object in whose scope to call the listener. |
| 39 | + * @constructor |
| 40 | + * @struct |
| 41 | + * @extends {goog.Disposable} |
| 42 | + * @final |
| 43 | + * @template T |
| 44 | + */ |
| 45 | +goog.async.Debouncer = function(listener, interval, opt_handler) { |
| 46 | + goog.async.Debouncer.base(this, 'constructor'); |
| 47 | + |
| 48 | + /** |
| 49 | + * Function to callback |
| 50 | + * @private {function(this: T)} |
| 51 | + */ |
| 52 | + this.listener_ = listener; |
| 53 | + |
| 54 | + /** |
| 55 | + * Interval for the debounce time |
| 56 | + * @type {number} |
| 57 | + * @private |
| 58 | + */ |
| 59 | + this.interval_ = interval; |
| 60 | + |
| 61 | + /** |
| 62 | + * "this" context for the listener |
| 63 | + * @type {Object|undefined} |
| 64 | + * @private |
| 65 | + */ |
| 66 | + this.handler_ = opt_handler; |
| 67 | + |
| 68 | + /** |
| 69 | + * Cached callback function invoked after the debounce timeout completes |
| 70 | + * @type {Function} |
| 71 | + * @private |
| 72 | + */ |
| 73 | + this.callback_ = goog.bind(this.onTimer_, this); |
| 74 | + |
| 75 | + /** |
| 76 | + * Indicates that the action is pending and needs to be fired. |
| 77 | + * @type {boolean} |
| 78 | + * @private |
| 79 | + */ |
| 80 | + this.shouldFire_ = false; |
| 81 | + |
| 82 | + /** |
| 83 | + * Indicates the count of nested pauses currently in effect on the debouncer. |
| 84 | + * When this count is not zero, fired actions will be postponed until the |
| 85 | + * debouncer is resumed enough times to drop the pause count to zero. |
| 86 | + * @type {number} |
| 87 | + * @private |
| 88 | + */ |
| 89 | + this.pauseCount_ = 0; |
| 90 | + |
| 91 | + /** |
| 92 | + * Timer for scheduling the next callback |
| 93 | + * @type {?number} |
| 94 | + * @private |
| 95 | + */ |
| 96 | + this.timer_ = null; |
| 97 | +}; |
| 98 | +goog.inherits(goog.async.Debouncer, goog.Disposable); |
| 99 | + |
| 100 | + |
| 101 | +/** |
| 102 | + * Notifies the debouncer that the action has happened. It will debounce the |
| 103 | + * call so that the callback is only called after the last action in a sequence |
| 104 | + * of actions separated by periods less the interval parameter passed to the |
| 105 | + * constructor. |
| 106 | + */ |
| 107 | +goog.async.Debouncer.prototype.fire = function() { |
| 108 | + this.stop(); |
| 109 | + this.timer_ = goog.Timer.callOnce(this.callback_, this.interval_); |
| 110 | +}; |
| 111 | + |
| 112 | + |
| 113 | +/** |
| 114 | + * Cancels any pending action callback. The debouncer can be restarted by |
| 115 | + * calling {@link #fire}. |
| 116 | + */ |
| 117 | +goog.async.Debouncer.prototype.stop = function() { |
| 118 | + if (this.timer_) { |
| 119 | + goog.Timer.clear(this.timer_); |
| 120 | + this.timer_ = null; |
| 121 | + } |
| 122 | + this.shouldFire_ = false; |
| 123 | +}; |
| 124 | + |
| 125 | + |
| 126 | +/** |
| 127 | + * Pauses the debouncer. All pending and future action callbacks will be delayed |
| 128 | + * until the debouncer is resumed. Pauses can be nested. |
| 129 | + */ |
| 130 | +goog.async.Debouncer.prototype.pause = function() { |
| 131 | + ++this.pauseCount_; |
| 132 | +}; |
| 133 | + |
| 134 | + |
| 135 | +/** |
| 136 | + * Resumes the debouncer. If doing so drops the pausing count to zero, pending |
| 137 | + * action callbacks will be executed as soon as possible, but still no sooner |
| 138 | + * than an interval's delay after the previous call. Future action callbacks |
| 139 | + * will be executed as normal. |
| 140 | + */ |
| 141 | +goog.async.Debouncer.prototype.resume = function() { |
| 142 | + if (!this.pauseCount_) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + --this.pauseCount_; |
| 147 | + if (!this.pauseCount_ && this.shouldFire_) { |
| 148 | + this.doAction_(); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | + |
| 153 | +/** @override */ |
| 154 | +goog.async.Debouncer.prototype.disposeInternal = function() { |
| 155 | + this.stop(); |
| 156 | + goog.async.Debouncer.base(this, 'disposeInternal'); |
| 157 | +}; |
| 158 | + |
| 159 | + |
| 160 | +/** |
| 161 | + * Handler for the timer to fire the debouncer. |
| 162 | + * @private |
| 163 | + */ |
| 164 | +goog.async.Debouncer.prototype.onTimer_ = function() { |
| 165 | + this.timer_ = null; |
| 166 | + |
| 167 | + if (!this.pauseCount_) { |
| 168 | + this.doAction_(); |
| 169 | + } else { |
| 170 | + this.shouldFire_ = true; |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | + |
| 175 | +/** |
| 176 | + * Calls the callback. |
| 177 | + * @private |
| 178 | + */ |
| 179 | +goog.async.Debouncer.prototype.doAction_ = function() { |
| 180 | + this.shouldFire_ = false; |
| 181 | + this.listener_.call(this.handler_); |
| 182 | +}; |
0 commit comments