Permalink
Browse files

adds pause(), resume() and shove()

- 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...
1 parent 27b990e commit 03c0e21106490192382886cd3e0c49abf1f1a985 @manuelstofer committed Feb 14, 2012
Showing with 25 additions and 4 deletions.
  1. +25 −4 lib/trickle.js
View
@@ -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;

0 comments on commit 03c0e21

Please sign in to comment.