Add mbrekey.
This commit is contained in:
43
lib/utils.js
43
lib/utils.js
@@ -1,4 +1,6 @@
|
||||
var utils = {};
|
||||
var util = require('util'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
utils = {};
|
||||
|
||||
utils.table = function(fields) {
|
||||
if (!fields[0]) return;
|
||||
@@ -18,4 +20,43 @@ utils.table = function(fields) {
|
||||
});
|
||||
};
|
||||
|
||||
function Queue(callback, concurrency) {
|
||||
this.callback = callback;
|
||||
this.concurrency = concurrency || 10;
|
||||
this.next = this.next.bind(this);
|
||||
this.invoke = this.invoke.bind(this);
|
||||
this.queue = [];
|
||||
this.running = 0;
|
||||
}
|
||||
util.inherits(Queue, EventEmitter);
|
||||
|
||||
Queue.prototype.add = function(item) {
|
||||
this.queue.push(item);
|
||||
if (this.running < this.concurrency) {
|
||||
this.running++;
|
||||
this.next();
|
||||
}
|
||||
};
|
||||
|
||||
Queue.prototype.invoke = function() {
|
||||
if (this.queue.length) {
|
||||
this.callback(this.queue.shift(), this.next);
|
||||
} else {
|
||||
this.next();
|
||||
}
|
||||
};
|
||||
|
||||
Queue.prototype.next = function(err) {
|
||||
if (this.queue.length) {
|
||||
process.nextTick(this.invoke);
|
||||
} else {
|
||||
this.running--;
|
||||
if (!this.running) {
|
||||
this.emit('empty');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
utils.Queue = Queue;
|
||||
|
||||
module.exports = utils;
|
||||
|
||||
Reference in New Issue
Block a user