From 276eba0fb1e316cab84f93bd5793cc138f596c14 Mon Sep 17 00:00:00 2001 From: Sarimoko Date: Sun, 13 Feb 2022 05:40:01 +0000 Subject: [PATCH] Add 'www/queue.js' --- www/queue.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 www/queue.js 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