refact(countdown): rewrite with task

This commit is contained in:
wryk 2019-05-20 15:18:00 +02:00
parent 0b251c679d
commit 15c85b11ea
5 changed files with 40 additions and 81 deletions

5
package-lock.json generated
View file

@ -3584,6 +3584,11 @@
"integrity": "sha512-R+H8IZclI8AAkSBRQJLVOsxwAoHd6WC40b4QTNWIjzAa6BXOBfQcM587MXDTVPeYaopFNWHUFLx7eNmHDSxMWg==", "integrity": "sha512-R+H8IZclI8AAkSBRQJLVOsxwAoHd6WC40b4QTNWIjzAa6BXOBfQcM587MXDTVPeYaopFNWHUFLx7eNmHDSxMWg==",
"dev": true "dev": true
}, },
"folktale": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/folktale/-/folktale-2.3.2.tgz",
"integrity": "sha512-+8GbtQBwEqutP0v3uajDDoN64K2ehmHd0cjlghhxh0WpcfPzAIjPA03e1VvHlxL02FVGR0A6lwXsNQKn3H1RNQ=="
},
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz",

View file

@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"@babel/runtime": "^7.4.3", "@babel/runtime": "^7.4.3",
"eventemitter3": "^3.1.0", "eventemitter3": "^3.1.0",
"folktale": "^2.3.2",
"generic-pool": "^3.6.1", "generic-pool": "^3.6.1",
"gif-writer": "^0.9.3", "gif-writer": "^0.9.3",
"objectFitPolyfill": "^2.1.1", "objectFitPolyfill": "^2.1.1",

View file

@ -1,68 +1,20 @@
import EventEmitter from 'eventemitter3' import { task } from 'folktale/concurrency/task'
export function countdown (n, delay) { export function countdown (n, delay, onStep) {
return new Countdown(n, delay) return task(resolver => {
} let count = 0
class Countdown extends EventEmitter { let intervalId = setInterval(() => {
constructor (n, delay) { count++
super() onStep(n - count)
this._n = n if (count >= n) {
this._delay = delay resolver.resolve()
}
this._count = 0 }, delay)
this._intervalId = null
resolver.cleanup(() => {
this._running = false clearTimeout(intervalId)
this._started = false })
this._ended = false })
this._cancelled = false
this._done = false
}
run () {
if (!this._running && !this._started) {
this._running = true
this._started = true
this.emit('started')
this._update()
this._intervalId = setInterval(() => {
this._count++
this._update()
if (this._count >= this._n) {
this._cleanup()
this.done = true
this.emit('done')
this._end()
}
}, this._delay)
}
}
cancel () {
if (this._running && !this._ended) {
this._cleanup()
this._cancelled = true
this.emit('cancelled')
this._end()
}
}
_update () {
this.emit('progress', this._count / this._n)
this.emit('update', this._n - this._count)
}
_end () {
this.ended = true
this.emit('ended')
this._running = false
}
_cleanup () {
clearInterval(this._intervalId)
}
} }

View file

@ -1 +1 @@
export const boomerang = xs => [...xs, ...xs.slice(1, xs.length - 1).reverse()] export const boomerang = xs => [...xs, ...xs.slice(1, xs.length - 1).reverse()]

View file

@ -89,27 +89,28 @@ export default {
} }
}, },
runCountdown () { runCountdown () {
this.countdown = countdown(this.timer, 1000) const step = (value) => {
this.countdown.on('started', value => {
this.timerActive = true
})
this.countdown.on('update', value => {
this.timerProgress = value this.timerProgress = value
}) }
this.countdown.on('ended', () => { const cleanup = () => {
this.timerActive = false this.timerActive = false
this.timerProgress = 0 this.timerProgress = 0
this.countdown = null this.countdown = null
}) }
this.countdown.on('done', () => { this.timerActive = true
this.runCapture() this.timerProgress = this.timer
})
this.countdown.run() this.countdown = countdown(this.timer, 1000, step).run()
this.countdown.listen({
onCancelled: cleanup,
onRejected: cleanup,
onResolved: () => {
this.runCapture()
}
})
}, },
cancelCountdown () { cancelCountdown () {
if (this.countdown) { if (this.countdown) {