diff --git a/src/utils/ProgressManager.js b/src/utils/ProgressManager.js new file mode 100644 index 0000000..260252d --- /dev/null +++ b/src/utils/ProgressManager.js @@ -0,0 +1,44 @@ +const PROGRESS_STEPS_COUNT = 100 + +export class ProgressManager { + #autoplayDuration + #onValueChange + + #interval + #paused = false + + constructor({ + autoplayDuration, + onValueChange, + }) { + this.#autoplayDuration = autoplayDuration + this.#onValueChange = onValueChange + } + + start() { + let value = 0 + let progress = 0 + const stepMs = this.#autoplayDuration / PROGRESS_STEPS_COUNT + + this.#interval = setInterval(() => { + if (this.#paused) { + return + } + progress += stepMs + value = progress / this.#autoplayDuration + this.#onValueChange(value) + }, stepMs) + } + + pause() { + this.#paused = true + } + + resume() { + this.#paused = false + } + + reset() { + clearInterval(this.#interval) + } +}