diff --git a/www/queue.js b/www/queue.js new file mode 100644 index 0000000..1c67da1 --- /dev/null +++ b/www/queue.js @@ -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; +} \ No newline at end of file