Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created October 14, 2009 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmurphey/210383 to your computer and use it in GitHub Desktop.
Save rmurphey/210383 to your computer and use it in GitHub Desktop.
dojo.provide('foo._Scheduler');
dojo.declare('foo._Scheduler', null, {
constructor : function() {
dojo.subscribe('/user/inactive', this, '_pause');
dojo.subscribe('/user/active', this, '_restart');
dojo.subscribe('/scheduler/clear', this, '_clear');
this.intervals = [];
this.requests = [];
},
schedule : function(r) {
/**
* r.wait : time to wait, if any
* r.dfd : deferred method used to fetch data
* r.data : data to send to the smd
* r.dataFn : function to run to get the data for each interval
* r.callback : callback to run when fetched
*/
r.fn = this._makeFn(r);
this._addRequest(r);
this.requests.push(r);
},
_addRequest : function(r) {
(r.wait && this.intervals.push(setInterval(r.fn, r.wait))) || r.fn();
},
_makeFn : function(config) {
return function() {
var data = typeof(config.data) == 'function' ? config.data() : config.data;
var dfd = config.dfd(data);
config.callback && dfd.addCallback(config.callback);
};
},
_restart : function() {
if (this.intervals.length) { return; }
dojo.forEach(this.requests, dojo.hitch(this, function(r) {
r.fn();
this._addRequest(r);
}));
},
_pause : function() {
dojo.forEach(this.intervals, window.clearInterval);
this.intervals = [];
},
_clear : function() {
this._pause();
this.requests = [];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment