Add 'www/queue.js'

This commit is contained in:
Sarimoko 2022-02-13 05:40:01 +00:00
parent cc74ab5dba
commit 276eba0fb1
1 changed files with 40 additions and 0 deletions

40
www/queue.js Normal file
View File

@ -0,0 +1,40 @@
// Loops and calls each function in a queue
function Queue() {
let queue = [];
let isLooping = false;
let isPaused = false;
this.loop = async () => {
isLooping = true;
const item = queue[0];
queue.shift();
await item();
if (!queue.length || isPaused) {
isLooping = false;
return;
}
this.loop();
};
this.add = item => {
if (isPaused) return;
queue.push(item);
if (!isLooping) this.loop();
};
this.clear = () => {
queue = [];
};
this.pause = (duration = 0) => {
isPaused = true;
setTimeout(() => (isPaused = false), duration);
};
this.isLooping = isLooping;
}