Add 'www/queue.js'
This commit is contained in:
parent
cc74ab5dba
commit
276eba0fb1
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue