Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add queue lib.

  • Loading branch information...
commit f7b49e0bba84d657eabb1f3d8b4dafd28b5f1d5a 1 parent adec2a3
@yhahn yhahn authored
Showing with 46 additions and 0 deletions.
  1. +46 −0 lib/queue.js
View
46 lib/queue.js
@@ -0,0 +1,46 @@
+var util = require('util');
+var EventEmitter = require('events').EventEmitter;
+
+module.exports = Queue;
+function Queue(callback, concurrency, timeout) {
+ this.callback = callback;
+ this.concurrency = concurrency || 10;
+ this.timeout = timeout || 0;
+ this.add = this.add.bind(this);
+ 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) {
+ if (this.timeout) {
+ setTimeout(this.invoke, this.timeout);
+ } else {
+ process.nextTick(this.invoke);
+ }
+ } else {
+ this.running--;
+ if (!this.running) {
+ this.emit('empty');
+ }
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.