A small package to create code queue's
- Multiple code queue's
- Thread count support
Install the Code Queue package:
npm install code-queue
Require the Code Queue package somewhere in your code:
const codeQueue = require('code-queue');
Start by initializing the package with the following function:
codeQueue.init({
debug: false,
queues: [
"default"
],
threads: 1,
onUpdate: (update) => {
console.log(update);
}
});
The only required item in the config is the queues
array. The reset of these items are optional.
Now to add functions to the queue do the following:
codeQueue.add("default", (completed) => {
console.log(`Starting item: ${item}`);
setTimeout(() => {
console.log(`Completed item: ${item}`);
completed();
}, 1000);
});
This will add a function to the default
queue. That will complete after the setTimeout
is done
You have to trigger the completed in your own code when you are done with your function.
By default we will run at 1 thread. This means we will only run one function at a time. To change this:
codeQueue.init({
threads: 5
});
To check the progress of a queue you can hook into the update function as follows:
codeQueue.init({
onUpdate: (update) => {
console.log(update);
}
});
Since we are constantly checking for your new code your NodeJS app will never exit. So we made a function that you can trigger to stop the queue module.
codeQueue.disable();
Want to see what is happening on the background. Sure just set debug to true
:
codeQueue.init({
debug: true
});
MIT