stream-worker
Advanced tools
Comparing version 1.0.2 to 2.0.0
{ | ||
"name": "stream-worker", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Execute an async function per stream data event, pausing the stream when a concurrency limit is saturated", | ||
@@ -23,7 +23,8 @@ "main": "stream-worker.js", | ||
"expect.js": "~0.2.0", | ||
"sinon": "~1.7.3", | ||
"sinon-expect": "~0.2.0", | ||
"grunt": "^0.4.0", | ||
"grunt-contrib-jshint": "~0.6.3", | ||
"grunt-simple-mocha": "~0.4.0", | ||
"matchdep": "~0.1.2" | ||
"matchdep": "~0.1.2", | ||
"sinon": "~1.7.3", | ||
"sinon-expect": "~0.2.0" | ||
}, | ||
@@ -30,0 +31,0 @@ "engines": { |
@@ -24,8 +24,8 @@ stream-worker [![build status](https://secure.travis-ci.org/goodeggs/stream-worker.png)](http://travis-ci.org/goodeggs/stream-worker) | ||
Promise style: | ||
```js | ||
streamWorker(stream, 10, function(data) { | ||
function doWork(data){ | ||
/* ... do some work with data ... */ | ||
return Promise.resolve(); | ||
}) | ||
} | ||
streamWorker(stream, doWork, {promises : true, concurrency : 10}) | ||
.then(function() { | ||
@@ -42,7 +42,8 @@ /* ... the stream is exhausted and all workers are finished ... */ | ||
```js | ||
streamWorker(stream, 10, | ||
function(data, done) { | ||
/* ... do some work with data ... */ | ||
done(err); | ||
}, | ||
function doWork(data, done){ | ||
/* ... do some work with data ... */ | ||
return done(err);; | ||
} | ||
streamWorker(stream, doWork, {promises : false, concurrency :10}, | ||
function(err) { | ||
@@ -56,2 +57,13 @@ /* ... the stream is exhauseted and all workers are finished ... */ | ||
--------- | ||
streamWorker(**stream**, **concurrencyLimit**, **work**, **done**) | ||
streamWorker(**stream**, **work**, **options**, **done**) | ||
Where **options** is an object with 2 optional parameters: | ||
| Parameter | Default | Description| | ||
|------------- |-------------| -----| | ||
| promises |false| true if you want to use the above promises style| | ||
| concurrency| 10|specifies how many concurrent workers you want doing work in the stream | | ||
And **done** is a callback function if you use the callback workflow. | ||
var Promise = require('bluebird'); | ||
module.exports = function(stream, concurrency, worker, cb) { | ||
/** | ||
* | ||
* @param {Stream} stream | ||
* @param {Function} worker - work to be done on each data element of the stream | ||
* @param {Object} options | ||
* @param {Boolean} [options.promises=false] - if true, the worker operates using promises. | ||
* @param {Number} [options.concurrency=10] - the maximum number of tasks to run concurrently | ||
* @param {Function} [done] - if using callbacks, this is called when work on the stream finishes | ||
*/ | ||
module.exports = function(stream, worker, options, done) { | ||
var tasks = [], | ||
@@ -9,3 +19,5 @@ running = 0, | ||
if (worker.length > 1) { // worker returns a callback | ||
var promises = options.promises ? options.promises : false; | ||
var concurrency = options.concurrency ? options.concurrency : 10; | ||
if (promises === false) { | ||
worker = Promise.promisify(worker); | ||
@@ -86,3 +98,3 @@ } | ||
}) | ||
.asCallback(cb); | ||
.asCallback(done); | ||
}; |
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
14315
85
67
7