From 15c85b11ea68d42938f8ecd6f29358bc63b1498e Mon Sep 17 00:00:00 2001 From: wryk Date: Mon, 20 May 2019 15:18:00 +0200 Subject: [PATCH] refact(countdown): rewrite with task --- package-lock.json | 5 +++ package.json | 1 + src/services/countdown.js | 84 ++++++++--------------------------- src/services/effects.js | 2 +- src/views/screens/capture.vue | 29 ++++++------ 5 files changed, 40 insertions(+), 81 deletions(-) diff --git a/package-lock.json b/package-lock.json index a983642..3d42581 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3584,6 +3584,11 @@ "integrity": "sha512-R+H8IZclI8AAkSBRQJLVOsxwAoHd6WC40b4QTNWIjzAa6BXOBfQcM587MXDTVPeYaopFNWHUFLx7eNmHDSxMWg==", "dev": true }, + "folktale": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/folktale/-/folktale-2.3.2.tgz", + "integrity": "sha512-+8GbtQBwEqutP0v3uajDDoN64K2ehmHd0cjlghhxh0WpcfPzAIjPA03e1VvHlxL02FVGR0A6lwXsNQKn3H1RNQ==" + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/package.json b/package.json index 874b08f..8ac490a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "@babel/runtime": "^7.4.3", "eventemitter3": "^3.1.0", + "folktale": "^2.3.2", "generic-pool": "^3.6.1", "gif-writer": "^0.9.3", "objectFitPolyfill": "^2.1.1", diff --git a/src/services/countdown.js b/src/services/countdown.js index 3fea6d5..620dd1e 100644 --- a/src/services/countdown.js +++ b/src/services/countdown.js @@ -1,68 +1,20 @@ -import EventEmitter from 'eventemitter3' +import { task } from 'folktale/concurrency/task' -export function countdown (n, delay) { - return new Countdown(n, delay) -} - -class Countdown extends EventEmitter { - constructor (n, delay) { - super() - - this._n = n - this._delay = delay - - this._count = 0 - this._intervalId = null - - this._running = false - 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) - } +export function countdown (n, delay, onStep) { + return task(resolver => { + let count = 0 + + let intervalId = setInterval(() => { + count++ + onStep(n - count) + + if (count >= n) { + resolver.resolve() + } + }, delay) + + resolver.cleanup(() => { + clearTimeout(intervalId) + }) + }) } diff --git a/src/services/effects.js b/src/services/effects.js index bd190ca..2a52ae2 100644 --- a/src/services/effects.js +++ b/src/services/effects.js @@ -1 +1 @@ -export const boomerang = xs => [...xs, ...xs.slice(1, xs.length - 1).reverse()] \ No newline at end of file +export const boomerang = xs => [...xs, ...xs.slice(1, xs.length - 1).reverse()] diff --git a/src/views/screens/capture.vue b/src/views/screens/capture.vue index 6df35d2..426015e 100644 --- a/src/views/screens/capture.vue +++ b/src/views/screens/capture.vue @@ -89,27 +89,28 @@ export default { } }, runCountdown () { - this.countdown = countdown(this.timer, 1000) - - this.countdown.on('started', value => { - this.timerActive = true - }) - - this.countdown.on('update', value => { + const step = (value) => { this.timerProgress = value - }) + } - this.countdown.on('ended', () => { + const cleanup = () => { this.timerActive = false this.timerProgress = 0 this.countdown = null - }) + } - this.countdown.on('done', () => { - this.runCapture() - }) + this.timerActive = true + 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 () { if (this.countdown) {