#31 : Add ProgressManager

This commit is contained in:
Vadim
2021-07-02 00:21:50 +03:00
parent b07caa9bac
commit 06a9302278

View File

@@ -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)
}
}