From 86cbeeadde694143f67d1603d2fe88bdaad08973 Mon Sep 17 00:00:00 2001 From: Tim Wong Date: Sun, 12 Sep 2021 17:33:10 +0800 Subject: [PATCH 1/2] resume timer after the computer wake up from sleep mode --- src/background/Timer.js | 66 ++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/src/background/Timer.js b/src/background/Timer.js index d0ad8d9e..90db30cc 100644 --- a/src/background/Timer.js +++ b/src/background/Timer.js @@ -16,11 +16,10 @@ class Timer extends EventEmitter this.duration = duration; this.tick = tick; - this.tickInterval = null; - this.expireTimeout = null; - this.checkpointStartAt = null; this.checkpointElapsed = 0; + + this.countdownInterval = null; } observe(observer) { @@ -72,11 +71,11 @@ class Timer extends EventEmitter return; } - this.setExpireTimeout(this.duration); - this.setTickInterval(this.tick); + this.checkpointStartAt = Date.now(); + + this.countdown(); this.state = TimerState.Running; - this.checkpointStartAt = Date.now(); this.emit('start', this.status); } @@ -86,11 +85,9 @@ class Timer extends EventEmitter return; } - clearInterval(this.tickInterval); - clearTimeout(this.expireTimeout); + clearInterval(this.countdownInterval); + this.countdownInterval = null; - this.tickInterval = null; - this.expireTimeout = null; this.checkpointStartAt = null; this.checkpointElapsed = 0; @@ -104,8 +101,7 @@ class Timer extends EventEmitter return; } - clearInterval(this.tickInterval); - clearTimeout(this.expireTimeout); + clearInterval(this.countdownInterval); let periodElapsed = (Date.now() - this.checkpointStartAt) / 1000; this.checkpointElapsed += periodElapsed; @@ -120,11 +116,11 @@ class Timer extends EventEmitter return; } - this.setExpireTimeout(this.remaining); - this.setTickInterval(this.tick); + this.checkpointStartAt = Date.now(); + + this.countdown(); this.state = TimerState.Running; - this.checkpointStartAt = Date.now(); this.emit('resume', this.status); } @@ -134,26 +130,36 @@ class Timer extends EventEmitter this.start(); } - setExpireTimeout(seconds) { - this.expireTimeout = setTimeout(() => { - clearInterval(this.tickInterval); - clearTimeout(this.expireTimeout); + expire() { + clearInterval(this.countdownInterval); + this.countdownInterval = null; + + this.checkpointStartAt = null; + this.checkpointElapsed = 0; + + this.state = TimerState.Stopped; + + this.emit('expire', this.status); + } - this.tickInterval = null; - this.expireTimeout = null; - this.checkpointStartAt = Date.now(); - this.checkpointElapsed = this.duration; + countdown() { + const expireAt = this.checkpointStartAt + this.remaining * 1000; + let nextTickAt = this.checkpointStartAt + this.periodBeforeNextTick() * 1000; - this.state = TimerState.Stopped; + this.countdownInterval = setInterval(() => { + const now = Date.now(); - this.emit('expire', this.status); - }, seconds * 1000); + if (now >= expireAt) { + this.expire(); + } else if (now >= nextTickAt) { + this.emit('tick', this.status); + nextTickAt = now + this.periodBeforeNextTick() * 1000; + } + }, 1000); } - setTickInterval(seconds) { - this.tickInterval = setInterval(() => { - this.emit('tick', this.status); - }, seconds * 1000); + periodBeforeNextTick() { + return this.tick - ((this.elapsed + this.tick) % this.tick); } } From b7fc2d9e04d1769923e4348252f546f12c6b932b Mon Sep 17 00:00:00 2001 From: Tim Wong Date: Sun, 12 Sep 2021 18:27:42 +0800 Subject: [PATCH 2/2] refactoring --- src/background/Timer.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/background/Timer.js b/src/background/Timer.js index 90db30cc..17676555 100644 --- a/src/background/Timer.js +++ b/src/background/Timer.js @@ -102,6 +102,7 @@ class Timer extends EventEmitter } clearInterval(this.countdownInterval); + this.countdownInterval = null; let periodElapsed = (Date.now() - this.checkpointStartAt) / 1000; this.checkpointElapsed += periodElapsed; @@ -144,7 +145,7 @@ class Timer extends EventEmitter countdown() { const expireAt = this.checkpointStartAt + this.remaining * 1000; - let nextTickAt = this.checkpointStartAt + this.periodBeforeNextTick() * 1000; + let nextTickAt = this.checkpointStartAt + this.getRemainingTimeBeforeNextTick() * 1000; this.countdownInterval = setInterval(() => { const now = Date.now(); @@ -153,12 +154,12 @@ class Timer extends EventEmitter this.expire(); } else if (now >= nextTickAt) { this.emit('tick', this.status); - nextTickAt = now + this.periodBeforeNextTick() * 1000; + nextTickAt = now + this.getRemainingTimeBeforeNextTick() * 1000; } }, 1000); } - periodBeforeNextTick() { + getRemainingTimeBeforeNextTick() { return this.tick - ((this.elapsed + this.tick) % this.tick); } }