Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created September 23, 2009 00:29
Show Gist options
  • Save rmurphey/191576 to your computer and use it in GitHub Desktop.
Save rmurphey/191576 to your computer and use it in GitHub Desktop.
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