Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noblesamurai/node-batch
Module to handle batching/aggregation of work with a timeout. E.g. batching requests to an API where you can stuff the payload will multiple queries.
https://github.com/noblesamurai/node-batch
Last synced: about 1 month ago
JSON representation
Module to handle batching/aggregation of work with a timeout. E.g. batching requests to an API where you can stuff the payload will multiple queries.
- Host: GitHub
- URL: https://github.com/noblesamurai/node-batch
- Owner: noblesamurai
- License: bsd-3-clause
- Created: 2014-07-21T02:41:13.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-07-22T06:08:29.000Z (over 10 years ago)
- Last Synced: 2024-04-24T16:09:42.579Z (9 months ago)
- Language: JavaScript
- Size: 223 KB
- Stars: 0
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
work-batcher
Module to handle batching/aggregation of work with a timeout. E.g. batching
requests to an API where you can stuff the payload will multiple queries.You need to provide a worker. When either the timeout expires or we have
enough items, the worker will be called. A function to multiplex the results
together for the worker should be provided, as well as a function to
demultiplex the results in the response.# Usage
```javascriptvar Batch = require('work-batcher'),
async = require('async'),
expect = require('expect.js');var batch = new Batch({
// Will assemble the individual payloads into one payload for the worker when
// it is time to call it.
multiplexer: function(items) {
return items.join(',');
},
// Will dis-assemble the result into individual results when the worker gives
// us a result.
demultiplexer: function(item, index, result) {
return result[index];
},
// The worker to call. called when we have maxItems or timeout occurs.
worker: function(data, callback) {
var result = {};
data.split(',').forEach(function(item, index) {
result[index] = item * item;
});
callback(null, result);
},
maxItems: 3,
timeout: 500
});/**
* Simple example involving a worker function that returns the squares of each
* element in the array.
*/
async.map([1,2,3,4], function(item, callback) {
pool.handleItem(item, callback);
},
function callback(err, results) {
if(err) return done(err);expect(results).to.eql([1,4,9,16]);
done();
});
```