Comparing version 2.2.0 to 3.0.0
@@ -1,22 +0,6 @@ | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
var q = queue({ | ||
timeout: 100, | ||
concurrency: 100 | ||
}); | ||
var q = queue(); | ||
var results = []; | ||
// listen for events | ||
q.on('success', function(result, job) { | ||
console.log('job finished processing:', job.toString().replace(/\n/g, '')); | ||
}); | ||
q.on('end', function() { | ||
console.log('all done:', results); | ||
}); | ||
// add jobs using the familiar Array API | ||
@@ -50,6 +34,7 @@ | ||
// use the timeout feature to deal with jobs that | ||
// take too long or forget to execute a callback | ||
q.timeout = 100; | ||
q.on('timeout', function(next, job) { | ||
@@ -71,2 +56,12 @@ console.log('job timed out:', job.toString().replace(/\n/g, '')); | ||
q.start(); | ||
// get notified when jobs complete | ||
q.on('success', function(result, job) { | ||
console.log('job finished processing:', job.toString().replace(/\n/g, '')); | ||
}); | ||
// begin processing, get notified on end / failure | ||
q.start(function(err) { | ||
console.log('all done:', results); | ||
}); |
43
index.js
@@ -1,19 +0,6 @@ | ||
/** | ||
* queue.js | ||
*/ | ||
/** | ||
* deps | ||
*/ | ||
var inherits = require('inherits'); | ||
var EventEmitter = require('events').EventEmitter; | ||
/** | ||
* export class | ||
*/ | ||
module.exports = Queue; | ||
/** | ||
* constructor | ||
*/ | ||
function Queue(options) { | ||
@@ -25,3 +12,3 @@ if (!(this instanceof Queue)) | ||
options = options || {}; | ||
this.concurrency = options.concurrency || 1; | ||
this.concurrency = options.concurrency || Infinity; | ||
this.timeout = options.timeout || 0; | ||
@@ -35,5 +22,2 @@ this.pending = 0; | ||
/** | ||
* expose selected array methods | ||
*/ | ||
var arrayMethods = [ | ||
@@ -57,5 +41,2 @@ 'push', | ||
/** | ||
* expose array.length | ||
*/ | ||
Object.defineProperty(Queue.prototype, 'length', { get: function() { | ||
@@ -65,7 +46,12 @@ return this.pending + this.jobs.length; | ||
/** | ||
* start processing the queue | ||
*/ | ||
Queue.prototype.start = function() { | ||
Queue.prototype.start = function(cb) { | ||
if (cb) { | ||
this.on('error', this.end.bind(this)); | ||
this.on('end', function(err) { | ||
cb(err); | ||
}); | ||
} | ||
if (this.pending === this.concurrency) { | ||
if (cb) cb(); | ||
return; | ||
@@ -132,5 +118,2 @@ } | ||
/** | ||
* stop / pause | ||
*/ | ||
Queue.prototype.stop = function() { | ||
@@ -140,5 +123,2 @@ this.running = false; | ||
/** | ||
* clear the queue including any running jobs | ||
*/ | ||
Queue.prototype.end = function(err) { | ||
@@ -153,5 +133,2 @@ if (this.jobs.length || this.pending) { | ||
/** | ||
* all done | ||
*/ | ||
function done(err) { | ||
@@ -158,0 +135,0 @@ this.session++; |
{ | ||
"name": "queue", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "async job queue with adjustable concurrency", | ||
@@ -13,13 +13,15 @@ "repository": { | ||
"devDependencies": { | ||
"tape": "*" | ||
"tape": "^2.14.0", | ||
"browserify": "^5.9.1" | ||
}, | ||
"scripts":{ | ||
"scripts": { | ||
"test": "node test", | ||
"test-browser": "browserify test/index.js > test/bundle.js && { [ -x \"$(which open)\" ] && open test/index.html || xdg-open test/index.html; }", | ||
"example": "node example" | ||
}, | ||
"testling" : { | ||
"files" : "test/index.js", | ||
"browsers" : [ | ||
"testling": { | ||
"files": "test/index.js", | ||
"browsers": [ | ||
"ie/8..latest", | ||
"ff/25..latest", | ||
"ff/24..latest", | ||
"chrome/25..latest", | ||
@@ -26,0 +28,0 @@ "safari/5.1..latest", |
141
README.md
``` | ||
____ ___ _____ __ _____ | ||
____ __ _____ __ _____ | ||
/ __ `/ / / / _ \/ / / / _ \ | ||
@@ -8,3 +8,3 @@ / /_/ / /_/ / __/ /_/ / __/ | ||
``` | ||
async job queue with adjustable concurrency | ||
manage concurrent asynchronous operations in javascript. | ||
@@ -14,64 +14,22 @@ [![browser support](http://ci.testling.com/jessetane/queue.png)](http://ci.testling.com/jessetane/queue) | ||
## why | ||
wanted something more flexible than [async](https://github.com/caolan/async#queue)'s queue | ||
[`async`](https://github.com/caolan/async) is a huge library, offering a huge number of abstractions for accomplishing the same thing. | ||
## how | ||
the module exports a class `Queue` that implements most of the `Array` api. pass async functions (ones that accept a callback) to an instance's additive array methods. processing begins when you call `q.start()` | ||
this module exports a class `Queue` that implements most of the `Array` api. pass async functions (ones that accept a callback) to an instance's additive array methods. processing begins when you call `q.start()` | ||
## api | ||
* `start()` | ||
* `stop()` | ||
* `end([err])` stop and empty the queue immediately | ||
inherited from `Array`: | ||
* `push(element1, ..., elementN)` | ||
* `unshift(element1, ..., elementN)` | ||
* `splice(index , howMany[, element1[, ...[, elementN]]])` | ||
* `pop()` | ||
* `shift()` | ||
* `slice(begin[, end])` | ||
* `reverse()` | ||
* `indexOf(searchElement[, fromIndex])` | ||
* `lastIndexOf(searchElement[, fromIndex])` | ||
## properties | ||
* `concurrency` maximum number of jobs that the queue should process concurrently - the default is 1 | ||
* `timeout` milliseconds to wait for a job to execute its callback | ||
* `length` jobs pending + jobs to process (readonly) | ||
## events | ||
* `q.emit('success', result, job)` after a job executes its callback | ||
* `q.emit('error', err, job)` after a job passes an error to its callback | ||
* `q.emit('timeout', continue, job)` after `q.timeout` milliseconds have elapsed and a job has not executed its callback | ||
* `q.emit('end'[, err])` after all jobs have been processed | ||
## download | ||
## install | ||
`npm install queue` | ||
## tests | ||
`node test` | ||
## test | ||
`npm test` | ||
`npm run test-browser` | ||
## example | ||
`node example` runs this: | ||
```javascript | ||
`npm run example` | ||
``` javascript | ||
var queue = require('queue'); | ||
var q = queue({ | ||
timeout: 100, | ||
concurrency: 100 | ||
}); | ||
var q = queue(); | ||
var results = []; | ||
// listen for events | ||
q.on('success', function(result, job) { | ||
console.log('job finished processing:', job.toString().replace(/\n/g, '')); | ||
}); | ||
q.on('end', function() { | ||
console.log('all done:', results); | ||
}); | ||
// add jobs using the familiar Array API | ||
@@ -105,6 +63,7 @@ | ||
// use the timeout feature to deal with jobs that | ||
// take too long or forget to execute a callback | ||
q.timeout = 100; | ||
q.on('timeout', function(next, job) { | ||
@@ -126,9 +85,77 @@ console.log('job timed out:', job.toString().replace(/\n/g, '')); | ||
q.start(); | ||
// get notified when jobs complete | ||
q.on('success', function(result, job) { | ||
console.log('job finished processing:', job.toString().replace(/\n/g, '')); | ||
}); | ||
// begin processing, get notified on end / failure | ||
q.start(function(err) { | ||
console.log('all done:', results); | ||
}); | ||
``` | ||
## note | ||
version 2.0 introduces api changes and is not backwards compatible with 1.0 | ||
## require | ||
### `var queue = require('queue')` | ||
## constructor | ||
### `var q = queue([opts])` | ||
where `opts` may contain inital values for: | ||
* `q.concurrency` | ||
* `q.timeout` | ||
## instance methods | ||
### `q.start([cb])` | ||
cb, if passed will be called when the queue empties | ||
### `q.stop()` | ||
stops the queue. can be resumed with `q.start()` | ||
### `q.end([err])` | ||
stop and empty the queue immediately | ||
## instance methods mixed in from `Array` | ||
Mozilla has docs on how these methods work [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). | ||
### `q.push(element1, ..., elementN)` | ||
### `q.unshift(element1, ..., elementN)` | ||
### `q.splice(index , howMany[, element1[, ...[, elementN]]])` | ||
### `q.pop()` | ||
### `q.shift()` | ||
### `q.slice(begin[, end])` | ||
### `q.reverse()` | ||
### `q.indexOf(searchElement[, fromIndex])` | ||
### `q.lastIndexOf(searchElement[, fromIndex])` | ||
## properties | ||
### `q.concurrency` | ||
max number of jobs the queue should process concurrently, defaults to `Infinity` | ||
### `q.timeout` | ||
milliseconds to wait for a job to execute its callback | ||
### `q.length` | ||
jobs pending + jobs to process (readonly) | ||
## events | ||
### `q.emit('success', result, job)` | ||
after a job executes its callback | ||
### `q.emit('error', err, job)` | ||
after a job passes an error to its callback | ||
### `q.emit('timeout', continue, job)` | ||
after `q.timeout` milliseconds have elapsed and a job has not executed its callback | ||
### `q.emit('end'[, err])` | ||
after all jobs have been processed | ||
## changelog | ||
* version 3.0 | ||
* changes the default concurrency to `Infinity` | ||
* allow `q.start()` to accept an optional callback executed on `q.emit('end')` | ||
* version 2.0 introduces api changes and is not backwards compatible with 1.0 | ||
## license | ||
WTFPL |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -25,3 +25,3 @@ tape('concurrent', function(t) { | ||
cb(); | ||
}, 1); | ||
}, 10); | ||
}); | ||
@@ -33,3 +33,3 @@ | ||
cb(); | ||
}, 6); | ||
}, 30); | ||
}); | ||
@@ -41,3 +41,3 @@ | ||
cb(); | ||
}, 3); | ||
}, 20); | ||
}); | ||
@@ -44,0 +44,0 @@ |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -7,3 +7,3 @@ tape('end', function(t) { | ||
var q = queue({ concurrency: Infinity }); | ||
var q = queue(); | ||
@@ -10,0 +10,0 @@ q.on('end', function(err) { |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -7,3 +7,3 @@ tape('error', function(t) { | ||
var q = queue({ concurrency: Infinity }); | ||
var q = queue(); | ||
@@ -10,0 +10,0 @@ q.on('error', q.end.bind(q)); |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -7,3 +7,3 @@ tape('length', function(t) { | ||
var q = queue({ concurrency: Infinity }); | ||
var q = queue(); | ||
@@ -10,0 +10,0 @@ q.on('end', function() { |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -7,7 +7,3 @@ tape('stop', function(t) { | ||
var q = queue(); | ||
q.on('end', function() { | ||
t.equal(q.running, false); | ||
}); | ||
var q = queue({ concurrency: 1 }); | ||
@@ -22,4 +18,4 @@ q.push(function(cb) { | ||
q.start(); | ||
}, 10); | ||
}, 10); | ||
}, 20); | ||
}, 20); | ||
}); | ||
@@ -33,3 +29,5 @@ | ||
// start | ||
q.start(); | ||
q.start(function(err) { | ||
t.equal(q.running, false); | ||
}); | ||
@@ -40,3 +38,3 @@ // but stop the q before the first job has finished | ||
q.stop(); | ||
}, 5); | ||
}, 10); | ||
}); |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -4,0 +4,0 @@ tape('synchronous', function(t) { |
var tape = require('tape'); | ||
var queue = require('..'); | ||
var queue = require('../'); | ||
@@ -4,0 +4,0 @@ tape('timeout', function(t) { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13220
14
158
2
377