Update progress manager

This commit is contained in:
Vadim
2021-11-30 16:24:05 +03:00
parent 1b6735700f
commit 3fb46f71b7

View File

@@ -4,35 +4,35 @@ const STEP_MS = 35
const MAX_VALUE = 1 const MAX_VALUE = 1
export class ProgressManager { export class ProgressManager {
#autoplayDuration
#onProgressValueChange
#interval
#paused = false
constructor({ onProgressValueChange }) { constructor({ onProgressValueChange }) {
this.#onProgressValueChange = onProgressValueChange this._onProgressValueChange = onProgressValueChange
this._autoplayDuration
this._onProgressValueChange
this._interval
this._paused = false
} }
setAutoplayDuration(autoplayDuration) { setAutoplayDuration(autoplayDuration) {
this.#autoplayDuration = autoplayDuration this._autoplayDuration = autoplayDuration
} }
start(onFinish) { start(onFinish) {
return new Promise((resolve) => { return new Promise((resolve) => {
this.reset() this.reset()
const stepMs = Math.min(STEP_MS, this.#autoplayDuration) const stepMs = Math.min(STEP_MS, this._autoplayDuration)
let progress = -stepMs let progress = -stepMs
this.#interval = setIntervalImmediate(async () => { this._interval = setIntervalImmediate(async () => {
if (this.#paused) { if (this._paused) {
return return
} }
progress += stepMs progress += stepMs
const value = progress / this.#autoplayDuration const value = progress / this._autoplayDuration
this.#onProgressValueChange(value) this._onProgressValueChange(value)
if (value > MAX_VALUE) { if (value > MAX_VALUE) {
this.reset() this.reset()
@@ -44,15 +44,15 @@ export class ProgressManager {
} }
pause() { pause() {
this.#paused = true this._paused = true
} }
resume() { resume() {
this.#paused = false this._paused = false
} }
reset() { reset() {
clearInterval(this.#interval) clearInterval(this._interval)
this.#onProgressValueChange(MAX_VALUE) this._onProgressValueChange(MAX_VALUE)
} }
} }