Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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
```javascript

var 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();
});
```