#45 : Promisify

This commit is contained in:
Vadim
2021-07-17 18:58:00 +03:00
parent e9805e373c
commit 1b314a530a
3 changed files with 109 additions and 110 deletions

View File

@@ -17,25 +17,28 @@ export class ProgressManager {
}
start(onFinish) {
this.reset()
return new Promise((resolve) => {
this.reset()
const stepMs = Math.min(STEP_MS, this.#autoplayDuration)
let progress = -stepMs
this.#interval = setIntervalImmediate(() => {
if (this.#paused) {
return
}
progress += stepMs
const value = progress / this.#autoplayDuration
this.#onProgressValueChange(value)
if (value > 1) {
this.reset()
onFinish()
}
}, stepMs)
const stepMs = Math.min(STEP_MS, this.#autoplayDuration)
let progress = -stepMs
this.#interval = setIntervalImmediate(async () => {
if (this.#paused) {
return
}
progress += stepMs
const value = progress / this.#autoplayDuration
this.#onProgressValueChange(value)
if (value > 1) {
this.reset()
await onFinish()
resolve()
}
}, stepMs)
})
}
pause() {

View File

@@ -2,3 +2,11 @@ export const setIntervalImmediate = (fn, ms) => {
fn();
return setInterval(fn, ms);
}
export const wait = (ms) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, ms)
})
}