Pool
Pool for JavaScript.
Installing
Using npm:
npm install xy-pool
Example
Implementing producer-consumer model.
const Pool = require('xy-pool');
const instance = new Pool({ maxSize: 10 });
// create a product
const product = producer();
// put the product into the pool
instance.push(product);
// get a product from the pool
consume(instance.pop());
Start async task with pool. It can limit the number of task.
const Pool = require('xy-pool');
const instance = new Pool({ maxSize: 10 });
instance.on('poolenter', onPoolEnter);
// start the task when the task enter the pool
function onPoolEnter(key, task) {
if (typeof task === 'function') {
task(key, task);
}
}
function post(key) {
http.post(...)
.then(...)
.catch(...)
.finally(function() { instance.pop(key) }); // remove the task when the async task is ok
}
// put the task into pool
instance.push(post);