Added idle.js so we can stop refreshing the page forever on ladder views when they're not looking.

This commit is contained in:
Nick Winter 2014-03-11 13:59:12 -07:00
parent bba3c78107
commit 852b1c97ac
3 changed files with 135 additions and 4 deletions

View file

@ -44,8 +44,13 @@ Application = initialize: ->
}, (t) =>
@router = new Router()
@router.subscribe()
Object.freeze this if typeof Object.freeze is 'function'
@router = Router
@idleTracker = new Idle
onAway: => @userIsIdle = true
onAwayBack: => @userIsIdle = false
onHidden: => @userIsIdle = true
onVisible: => @userIsIdle = false
awayTimeout: 5 * 60 * 1000
@idleTracker.start()
module.exports = Application
window.application = Application

View file

@ -4,6 +4,7 @@ Simulator = require 'lib/simulator/Simulator'
LevelSession = require 'models/LevelSession'
CocoCollection = require 'models/CocoCollection'
{teamDataFromLevel} = require './ladder/utils'
application = require 'application'
LadderTabView = require './ladder/ladder_tab'
MyMatchesTabView = require './ladder/my_matches_tab'
@ -74,12 +75,11 @@ module.exports = class LadderView extends RootView
@sessions.fetch({"success": @refreshViews})
refreshViews: =>
return if @destroyed
return if @destroyed or application.userIsIdle
@ladderTab.refreshLadder()
@myMatchesTab.refreshMatches()
console.log "refreshed views!"
# Simulations
onSimulateAllButtonClick: (e) ->

126
vendor/scripts/idle.js vendored Normal file
View file

@ -0,0 +1,126 @@
// https://github.com/shawnmclean/Idle.js
(function() {
"use strict";
var Idle;
Idle = {};
Idle = (function() {
Idle.isAway = false;
Idle.awayTimeout = 3000;
Idle.awayTimestamp = 0;
Idle.awayTimer = null;
Idle.onAway = null;
Idle.onAwayBack = null;
Idle.onVisible = null;
Idle.onHidden = null;
function Idle(options) {
var activeMethod, activity;
if (options) {
this.awayTimeout = parseInt(options.awayTimeout, 10);
this.onAway = options.onAway;
this.onAwayBack = options.onAwayBack;
this.onVisible = options.onVisible;
this.onHidden = options.onHidden;
}
activity = this;
activeMethod = function() {
return activity.onActive();
};
window.onclick = activeMethod;
window.onmousemove = activeMethod;
window.onmouseenter = activeMethod;
window.onkeydown = activeMethod;
window.onscroll = activeMethod;
window.onmousewheel = activeMethod;
document.addEventListener("visibilitychange", (function() {
return activity.handleVisibilityChange();
}), false);
document.addEventListener("webkitvisibilitychange", (function() {
return activity.handleVisibilityChange();
}), false);
document.addEventListener("msvisibilitychange", (function() {
return activity.handleVisibilityChange();
}), false);
}
Idle.prototype.onActive = function() {
this.awayTimestamp = new Date().getTime() + this.awayTimeout;
if (this.isAway) {
if (this.onAwayBack) {
this.onAwayBack();
}
this.start();
}
this.isAway = false;
return true;
};
Idle.prototype.start = function() {
var activity;
this.awayTimestamp = new Date().getTime() + this.awayTimeout;
if (this.awayTimer !== null) {
clearTimeout(this.awayTimer);
}
activity = this;
this.awayTimer = setTimeout((function() {
return activity.checkAway();
}), this.awayTimeout + 100);
return this;
};
Idle.prototype.setAwayTimeout = function(ms) {
this.awayTimeout = parseInt(ms, 10);
return this;
};
Idle.prototype.checkAway = function() {
var activity, t;
t = new Date().getTime();
if (t < this.awayTimestamp) {
this.isAway = false;
activity = this;
this.awayTimer = setTimeout((function() {
return activity.checkAway();
}), this.awayTimestamp - t + 100);
return;
}
if (this.awayTimer !== null) {
clearTimeout(this.awayTimer);
}
this.isAway = true;
if (this.onAway) {
return this.onAway();
}
};
Idle.prototype.handleVisibilityChange = function() {
if (document.hidden || document.msHidden || document.webkitHidden) {
if (this.onHidden) {
return this.onHidden();
}
} else {
if (this.onVisible) {
return this.onVisible();
}
}
};
return Idle;
})();
window.Idle = Idle;
}).call(this);