3 function Queue(options) {
4 if (!(this instanceof Queue)) {
5 return new Queue(options);
8 options = options || {};
9 this.concurrency = options.concurrency || Infinity;
13 this._done = done.bind(this);
16 var arrayAddMethods = [
22 arrayAddMethods.forEach(function(method) {
23 Queue.prototype[method] = function() {
24 var methodResult = Array.prototype[method].apply(this.jobs, arguments);
30 Object.defineProperty(Queue.prototype, 'length', {
32 return this.pending + this.jobs.length;
36 Queue.prototype._run = function() {
37 if (this.pending === this.concurrency) {
40 if (this.jobs.length) {
41 var job = this.jobs.shift();
47 if (this.pending === 0) {
48 while (this.cbs.length !== 0) {
49 var cb = this.cbs.pop();
55 Queue.prototype.onDone = function(cb) {
56 if (typeof cb === 'function') {
67 module.exports = Queue;