1.0.0 • Published 3 years ago
enqueu v1.0.0
Enqueu
Promise queue for concurrency control
Installation
$ npm i enqueuor
<script src="https://unpkg.com/enqueu@1.0.0/dist/index.js"></script>Example
In this case you will get "hey, my name is yoann, how are you ?, bye" and without Enqueu "my name is yoann, bye, how are you ?, hey".
import Enqueu from "enqueu";
const queu = new Enqueu();
queu
.add(() => new Promise((r) => setTimeout(r, 3000)))
.then((_) => console.log("hey"));
queu
.add(() => new Promise((r) => setTimeout(r, 2000)))
.then((_) => console.log("how are you ?"));
queu
.add(() => new Promise((r) => setTimeout(r, 1000)))
.then((_) => console.log("bye"));
queu
.add(() => new Promise((r) => setTimeout(r, 500)), { priority: 1 }) //from 1 to Infinity
.then((_) => console.log("my name is Yoann"));With event listener
For example in this case if you click 4 times at once on the button you will get "10, 5, 2.5". But without Enqueu you will get "1.25, 1.25, 1.25, 1.25".
const queu = new Enqueu({ maxSize: 3 }); //the maximum size of the queu is 3 (other will be throwed off)
let count = 20;
document.querySelector("button").addEventListener(
"click",
queu.createFn(function () {
count = count / 2;
return new Promise((r) =>
setTimeout(function () {
r(count);
}, count * 1000)
);
})
);Enqueu
Constructor
maxSizeMax size of the queumaxConcurrencyMax promise as the same time
Attibutes
isPausedReturn a boolean to see if the queu is paused or notpendingReturn the number of pending functionsqueuReturn the queu
Methods
add(fn: Function, options: Options = {}): FunctionAdd the function to the queucreateFn(fn: Function, options: Options = {}): FunctionCreate a function that will add "fn" to the queu on call (useful for addEventListener for example)pause()Pause the queustart()Start the queu after a pauseonEmpty(fn: Function)Call the function passed as argument when the queu is emptyonQueuElementExecuted(fn: Function)Call the function passed as argument when the a new queu function is startedonQueuElementFinishExecution(fn: Function)Call the function passed as argument when the a new queu function which started is finishedclear()Clear the queuremove(fn: Function)Remove a function from the queuremoveFromIndex(index: number)Remove a specified indexed element in the queu
1.0.0
3 years ago