]> git.codecow.com Git - libnemo.git/commitdiff
Fix self-extending timer.
authorChris Duncan <chris@codecow.com>
Wed, 5 Aug 2026 05:56:40 +0000 (22:56 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 5 Aug 2026 05:56:40 +0000 (22:56 -0700)
src/lib/vault/vault-timer.ts

index 3d6b429fcba60b76fa839cf5079a78fd7a15d4cc..39c5491973a8cfc1ae2744cb9d347ad65f50faf7 100644 (file)
@@ -5,31 +5,34 @@ export class VaultTimer {
        #f: () => any
        #elapsed: number = 0
        #isPaused: boolean = false
+       #remaining: number
        #start: number
        #ticker: number | NodeJS.Timeout
        #timeout: number | NodeJS.Timeout
 
        constructor (f: () => any, t: number) {
-               this.#ticker = setInterval(Function.prototype, 1000)
                this.#f = f
+               this.#remaining = t
                this.#start = performance.now()
-               this.#timeout = setTimeout(this.#f, t)
+               this.#ticker = setInterval(Function.prototype, 1000)
+               this.#timeout = setTimeout(this.#f, this.#remaining)
        }
 
        pause () {
                if (!this.#isPaused) {
-                       clearInterval(this.#ticker)
                        clearTimeout(this.#timeout)
-                       this.#elapsed = performance.now() - this.#start
+                       clearInterval(this.#ticker)
+                       const elapsed = performance.now() - this.#start
+                       this.#remaining -= elapsed
                        this.#isPaused = true
                }
        }
 
        resume () {
                if (this.#isPaused) {
-                       this.#ticker = setInterval(Function.prototype, 1000)
                        this.#start = performance.now()
-                       this.#timeout = setTimeout(this.#f, this.#elapsed)
+                       this.#ticker = setInterval(Function.prototype, 1000)
+                       this.#timeout = setTimeout(this.#f, this.#remaining)
                        this.#isPaused = false
                }
        }