Created
September 23, 2009 00:29
-
-
Save rmurphey/191576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dojo.provide('hitpost.ActivityMonitor'); | |
dojo.declare('hitpost.ActivityMonitor', null, { | |
constructor : function(props) { | |
this._settings = { | |
domNode : dojo.body(), | |
events : [ 'mousemove', 'keypress' ], | |
allowedInactivePeriods : 3, | |
periodLength : 15, | |
activeTopic : '/user/active', | |
inactiveTopic : '/user/inactive' | |
}; | |
dojo.mixin(this._settings, props); | |
this.watchers = []; | |
this.start(); | |
}, | |
start : function() { | |
this.reset(); | |
this.interval || this._discoverInactivity(); | |
setTimeout(dojo.hitch(this, '_awaitActivity'), this._settings.periodLength * 1000); | |
}, | |
stop : function() { | |
clearInterval(this.interval); | |
}, | |
ping : function(publish) { | |
if (publish !== false) { | |
this.active ? | |
dojo.publish(this._settings.activeTopic) : | |
dojo.publish(this._settings.inactiveTopic); | |
} | |
return this.active; | |
}, | |
reset : function() { | |
this.watchers = []; | |
this.inactivePeriods = 0; | |
this.active = false; | |
}, | |
_awaitActivity : function() { | |
dojo.forEach(this._settings.events, dojo.hitch(this, function(ev) { | |
this.watchers.push(dojo.connect(this._settings.domNode, ev, this, '_registerActivity')); | |
})); | |
}, | |
_registerActivity : function() { | |
dojo.publish(this._settings.activeTopic); | |
dojo.forEach(this.watchers, dojo.disconnect); | |
this.watchers = []; | |
this.active = true; | |
setTimeout(dojo.hitch(this, '_awaitActivity'), this._settings.periodLength * 1000); | |
}, | |
_discoverInactivity : function() { | |
this.interval = setInterval(dojo.hitch(this, function() { | |
if (!this.active) { | |
this.inactivePeriods++; | |
if (this.inactivePeriods > this._settings.allowedInactivePeriods) { | |
dojo.publish(this._settings.inactiveTopic); | |
} | |
} else { | |
this.reset(); | |
} | |
}), this._settings.periodLength * 1000); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment