]> git.codecow.com Git - libnemo.git/commitdiff
Revert to simpler vault timer.
authorChris Duncan <chris@codecow.com>
Wed, 12 Aug 2026 00:25:13 +0000 (17:25 -0700)
committerChris Duncan <chris@codecow.com>
Wed, 12 Aug 2026 00:25:13 +0000 (17:25 -0700)
src/lib/vault/vault-timer.ts

index b596b5c42bda0209ddec253b58e17642f2a7fd02..407953fb9a90cdbcc28eb93316cbc005a8254708 100644 (file)
@@ -8,50 +8,51 @@
  */
 export class VaultTimer {
        #f: () => any
-       #end: PerformanceMark
-       #start: PerformanceMark
+       #isPaused: boolean = false
+       #end: number
+       #start: number
        #ticker: number | NodeJS.Timeout
        #timeout: number | NodeJS.Timeout
 
-       get #measureOptions (): PerformanceMeasureOptions { return { start: this.#start.startTime, end: this.#end.startTime } }
-       get #remaining (): PerformanceMeasure { return performance.measure('vault-timer-remaining', this.#measureOptions) }
+       get #remaining (): number { return this.#end - this.#start }
 
        constructor (f: () => any, t: number) {
                this.#f = () => {
-                       clearTimeout(this.#timeout)
                        clearInterval(this.#ticker)
+                       clearTimeout(this.#timeout)
                        f()
                }
-               this.#start = performance.mark('vault-timer-start')
-               this.#end = performance.mark('vault-timer-end', { startTime: performance.now() + t })
+               this.#start = performance.now()
+               this.#end = this.#start + t
                this.#ticker = setInterval(Function.prototype, 1000)
-               this.#timeout = setTimeout(this.#f, this.#remaining.duration)
+               this.#timeout = setTimeout(this.#f, this.#remaining)
        }
 
        pause () {
-               this.#start = performance.mark('vault-timer-paused')
-               const remaining = this.#remaining.duration
-               if (remaining > 0) {
-                       clearTimeout(this.#timeout)
+               if (!this.#isPaused && this.#remaining > 0) {
                        clearInterval(this.#ticker)
+                       clearTimeout(this.#timeout)
+                       this.#start = performance.now()
+                       this.#isPaused = true
                }
        }
 
        reset (t: number) {
                clearTimeout(this.#timeout)
                clearInterval(this.#ticker)
-               this.#start = performance.mark('vault-timer-start')
-               this.#end = performance.mark('vault-timer-end', { startTime: performance.now() + t })
+               this.#start = performance.now()
+               this.#end = this.#start + t
                this.#ticker = setInterval(Function.prototype, 1000)
-               this.#timeout = setTimeout(this.#f, this.#remaining.duration)
+               this.#timeout = setTimeout(this.#f, this.#remaining)
+               this.#isPaused = false
        }
 
        resume () {
-               const remaining = this.#remaining.duration
-               if (remaining > 0) {
-                       this.#start = performance.mark('vault-timer-start')
-                       this.#end = performance.mark('vault-timer-end', { startTime: performance.now() + remaining })
-                       this.#timeout = setTimeout(this.#f, remaining)
+               if (this.#isPaused && this.#remaining > 0) {
+                       this.#start = performance.now()
+                       this.#ticker = setInterval(Function.prototype, 1000)
+                       this.#timeout = setTimeout(this.#f, this.#remaining)
+                       this.#isPaused = false
                }
        }
 }