1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Idle Timer.
*
* Keeps track of transitions between active and idle. This class is built on
* top of ActivityMonitor. Whenever an active user becomes idle, this class
* dispatches a BECOME_IDLE event. Whenever an idle user becomes active, this
* class dispatches a BECOME_ACTIVE event. The amount of inactive time it
* takes for a user to be considered idle is specified by the client, and
* different instances of this class can all use different thresholds.
*
*/
goog.provide('goog.ui.IdleTimer');
goog.require('goog.Timer');
goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.structs.Set');
goog.require('goog.ui.ActivityMonitor');
/**
* Event target that will give notification of state changes between active and
* idle. This class is designed to require few resources while the user is
* active.
* @param {number} idleThreshold Amount of time in ms at which we consider the
* user has gone idle.
* @param {goog.ui.ActivityMonitor=} opt_activityMonitor The activity monitor
* keeping track of user interaction. Defaults to a default-constructed
* activity monitor. If a default activity monitor is used then this class
* will dispose of it. If an activity monitor is passed in then the caller
* remains responsible for disposing of it.
* @constructor
* @extends {goog.events.EventTarget}
* @final
*/
goog.ui.IdleTimer = function(idleThreshold, opt_activityMonitor) {
goog.events.EventTarget.call(this);
var activityMonitor =
opt_activityMonitor || this.getDefaultActivityMonitor_();
/**
* The amount of time in ms at which we consider the user has gone idle
* @type {number}
* @private
*/
this.idleThreshold_ = idleThreshold;
/**
* The activity monitor keeping track of user interaction
* @type {goog.ui.ActivityMonitor}
* @private
*/
this.activityMonitor_ = activityMonitor;
/**
* Cached onActivityTick_ bound to the object for later use
* @type {Function}
* @private
*/
this.boundOnActivityTick_ = goog.bind(this.onActivityTick_, this);
// Decide whether the user is currently active or idle. This method will
// check whether it is correct to start with the user in the active state.
this.maybeStillActive_();
};
goog.inherits(goog.ui.IdleTimer, goog.events.EventTarget);
/**
* Whether a listener is currently registered for an idle timer event. On
* initialization, the user is assumed to be active.
* @type {boolean}
* @private
*/
goog.ui.IdleTimer.prototype.hasActivityListener_ = false;
/**
* Handle to the timer ID used for checking ongoing activity, or null
* @type {?number}
* @private
*/
goog.ui.IdleTimer.prototype.onActivityTimerId_ = null;
/**
* Whether the user is currently idle
* @type {boolean}
* @private
*/
goog.ui.IdleTimer.prototype.isIdle_ = false;
/**
* The default activity monitor created by this class, if any
* @type {goog.ui.ActivityMonitor?}
* @private
*/
goog.ui.IdleTimer.defaultActivityMonitor_ = null;
/**
* The idle timers that currently reference the default activity monitor
* @type {goog.structs.Set}
* @private
*/
goog.ui.IdleTimer.defaultActivityMonitorReferences_ = new goog.structs.Set();
/**
* Event constants for the idle timer event target
* @enum {string}
*/
goog.ui.IdleTimer.Event = {
/** Event fired when an idle user transitions into the active state */
BECOME_ACTIVE: 'active',
/** Event fired when an active user transitions into the idle state */
BECOME_IDLE: 'idle'
};
/**
* Gets the default activity monitor used by this class. If a default has not
* been created yet, then a new one will be created.
* @return {!goog.ui.ActivityMonitor} The default activity monitor.
* @private
*/
goog.ui.IdleTimer.prototype.getDefaultActivityMonitor_ = function() {
goog.ui.IdleTimer.defaultActivityMonitorReferences_.add(this);
if (goog.ui.IdleTimer.defaultActivityMonitor_ == null) {
goog.ui.IdleTimer.defaultActivityMonitor_ = new goog.ui.ActivityMonitor();
}
return goog.ui.IdleTimer.defaultActivityMonitor_;
};
/**
* Removes the reference to the default activity monitor. If there are no more
* references then the default activity monitor gets disposed.
* @private
*/
goog.ui.IdleTimer.prototype.maybeDisposeDefaultActivityMonitor_ = function() {
goog.ui.IdleTimer.defaultActivityMonitorReferences_.remove(this);
if (goog.ui.IdleTimer.defaultActivityMonitor_ != null &&
goog.ui.IdleTimer.defaultActivityMonitorReferences_.isEmpty()) {
goog.ui.IdleTimer.defaultActivityMonitor_.dispose();
goog.ui.IdleTimer.defaultActivityMonitor_ = null;
}
};
/**
* Checks whether the user is active. If the user is still active, then a timer
* is started to check again later.
* @private
*/
goog.ui.IdleTimer.prototype.maybeStillActive_ = function() {
// See how long before the user would go idle. The user is considered idle
// after the idle time has passed, not exactly when the idle time arrives.
var remainingIdleThreshold = this.idleThreshold_ + 1 -
(goog.now() - this.activityMonitor_.getLastEventTime());
if (remainingIdleThreshold > 0) {
// The user is still active. Check again later.
this.onActivityTimerId_ =
goog.Timer.callOnce(this.boundOnActivityTick_, remainingIdleThreshold);
} else {
// The user has not been active recently.
this.becomeIdle_();
}
};
/**
* Handler for the timeout used for checking ongoing activity
* @private
*/
goog.ui.IdleTimer.prototype.onActivityTick_ = function() {
// The timer has fired.
this.onActivityTimerId_ = null;
// The maybeStillActive method will restart the timer, if appropriate.
this.maybeStillActive_();
};
/**
* Transitions from the active state to the idle state
* @private
*/
goog.ui.IdleTimer.prototype.becomeIdle_ = function() {
this.isIdle_ = true;
// The idle timer will send notification when the user does something
// interactive.
goog.events.listen(
this.activityMonitor_, goog.ui.ActivityMonitor.Event.ACTIVITY,
this.onActivity_, false, this);
this.hasActivityListener_ = true;
// Notify clients of the state change.
this.dispatchEvent(goog.ui.IdleTimer.Event.BECOME_IDLE);
};
/**
* Handler for idle timer events when the user does something interactive
* @param {goog.events.Event} e The event object.
* @private
*/
goog.ui.IdleTimer.prototype.onActivity_ = function(e) {
this.becomeActive_();
};
/**
* Transitions from the idle state to the active state
* @private
*/
goog.ui.IdleTimer.prototype.becomeActive_ = function() {
this.isIdle_ = false;
// Stop listening to every interactive event.
this.removeActivityListener_();
// Notify clients of the state change.
this.dispatchEvent(goog.ui.IdleTimer.Event.BECOME_ACTIVE);
// Periodically check whether the user has gone inactive.
this.maybeStillActive_();
};
/**
* Removes the activity listener, if necessary
* @private
*/
goog.ui.IdleTimer.prototype.removeActivityListener_ = function() {
if (this.hasActivityListener_) {
goog.events.unlisten(
this.activityMonitor_, goog.ui.ActivityMonitor.Event.ACTIVITY,
this.onActivity_, false, this);
this.hasActivityListener_ = false;
}
};
/** @override */
goog.ui.IdleTimer.prototype.disposeInternal = function() {
this.removeActivityListener_();
if (this.onActivityTimerId_ != null) {
goog.global.clearTimeout(this.onActivityTimerId_);
this.onActivityTimerId_ = null;
}
this.maybeDisposeDefaultActivityMonitor_();
goog.ui.IdleTimer.superClass_.disposeInternal.call(this);
};
/**
* @return {number} the amount of time at which we consider the user has gone
* idle in ms.
*/
goog.ui.IdleTimer.prototype.getIdleThreshold = function() {
return this.idleThreshold_;
};
/**
* @return {goog.ui.ActivityMonitor} the activity monitor keeping track of user
* interaction.
*/
goog.ui.IdleTimer.prototype.getActivityMonitor = function() {
return this.activityMonitor_;
};
/**
* Returns true if there has been no user action for at least the specified
* interval, and false otherwise
* @return {boolean} true if the user is idle, false otherwise.
*/
goog.ui.IdleTimer.prototype.isIdle = function() {
return this.isIdle_;
};
|