A processing queue with workload limit
npm i nanoqueueconst NanoQueue = require('nanoqueue')
let sum = 0
const queue = new NanoQueue(15, {
// Process enqueue items, invoke `done' when done
process: (n, done) => {
setTimeout(() => {
sum += n
console.log(`Current progress: ${Math.floor(queue.progress * 100)}, ${queue.remaining} items remaining`)
done()
}, 50)
},
// Will be invoked every time queue.remaining reaches 0
oncomplete: () => {
console.log('All done, current sum:', sum)
}
})
for (let i = 0; i < 100; i++) {
queue.push(i)
}Properties
- ro
{Number} remainingAmount of items that have not yet finished processing - ro
{Number} progressValue between 0 and 1
new NanoQueue(nSlots, handlers)
Arguments
{Number} nSlotsNumber of active work slots.{Object} handlers{Function} process (item, done)Invoked for enqueued item once a slot is free- optional
{Function} oncomplete ()Invoked wheneverremainingbecomes 0
Description
Creates a new NanoQueue instance with specified amount of work-slots.
The process handler is required to be implemented and once invoked it must in turn
invoke the done callback to let the queue continue.
queue.push(item)
Description
Enqueues itemfor processing
queue.reset()
Description
Resets processed/enqueued counters back to zero
This software is released under GNU LGPL 3.0 or later