Skip to content

Commit

Permalink
adds pause(), resume() and shove()
Browse files Browse the repository at this point in the history
- pause and resume are usefull to stop processing
the queue and resume later

- shove() places a task in front of the queue
  • Loading branch information
manuelstofer committed Feb 14, 2012
1 parent 27b990e commit 03c0e21
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/trickle.js
Expand Up @@ -6,24 +6,45 @@
this._interval = interval;
this._requests = [];
this._lastRun = 0;
this._paused = false;
this._callback = callback;
};

Trickle.prototype = {

trickle: function (callback) {
var self = this;
this._requests.push(callback);
process.nextTick(function () { self._run(); });
},
this._runNextTick();
},

shove: function (callback) {
this._requests.unshift(callback);
this._runNextTick();
},

isEmpty: function () {
return this._requests.length === 0;
},

pause: function () {
this._paused = true;
},

resume: function () {
this._paused = false;
this._run();
},

_runNextTick: function () {
var self = this;
process.nextTick(function () { self._run(); });
},

_run: function () {
if (this._paused) return;

var now = (new Date()).getTime();

if (this._isReady()) {
this._requests.shift().call();
this._lastRun = now;
Expand Down

0 comments on commit 03c0e21

Please sign in to comment.