Add asyncqueue class

This commit is contained in:
calzoneman 2013-09-29 00:27:43 -05:00
parent 8ba5743bc2
commit 3d3d21a3f9
1 changed files with 45 additions and 0 deletions

45
lib/asyncqueue.js Normal file
View File

@ -0,0 +1,45 @@
var AsyncQueue = function () {
this._q = [];
this._lock = false;
this._tm = 0;
};
AsyncQueue.prototype.next = function () {
if (this._q.length > 0) {
if (!this.lock())
return;
var fn = this._q.shift();
fn(this);
}
};
AsyncQueue.prototype.lock = function () {
if (this._lock)
return false;
this._lock = true;
return true;
};
AsyncQueue.prototype.release = function () {
var self = this;
if (!self._lock)
return false;
self._lock = false;
process.nextTick(function () {
self.next();
});
return true;
};
AsyncQueue.prototype.queue = function (fn) {
var self = this;
self._q.push(fn);
self.next();
};
AsyncQueue.prototype.reset = function () {
this._q = [];
this._lock = false;
};