From: Chris Duncan Date: Mon, 10 Aug 2026 20:52:20 +0000 (-0700) Subject: Refactor vault timer to use Performance API objects. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=035c811b5ebd91feee5ddb6f1894ec2888472153;p=libnemo.git Refactor vault timer to use Performance API objects. --- diff --git a/src/lib/vault/vault-timer.ts b/src/lib/vault/vault-timer.ts index bb86e76..fa324b3 100644 --- a/src/lib/vault/vault-timer.ts +++ b/src/lib/vault/vault-timer.ts @@ -8,46 +8,51 @@ */ export class VaultTimer { #f: () => any - #isPaused: boolean = false - #remaining: number - #start: number + #end: PerformanceMark + #start: PerformanceMark #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) } + constructor (f: () => any, t: number) { - this.#f = f - this.#remaining = t - this.#start = performance.now() + this.#f = () => { + clearTimeout(this.#timeout) + clearInterval(this.#ticker) + f() + this.#f = () => void 0 + } + this.#start = performance.mark('vault-timer-start') + this.#end = performance.mark('vault-timer-end', { startTime: performance.now() + t }) this.#ticker = setInterval(Function.prototype, 1000) - this.#timeout = setTimeout(this.#f, this.#remaining) + this.#timeout = setTimeout(this.#f, this.#remaining.duration) } pause () { - if (!this.#isPaused) { + this.#start = performance.mark('vault-timer-paused') + const remaining = this.#remaining.duration + if (remaining > 0) { clearTimeout(this.#timeout) clearInterval(this.#ticker) - const elapsed = performance.now() - this.#start - this.#remaining -= elapsed - this.#isPaused = true } } reset (t: number) { clearTimeout(this.#timeout) clearInterval(this.#ticker) - this.#remaining = t - this.#start = performance.now() + this.#start = performance.mark('vault-timer-start') + this.#end = performance.mark('vault-timer-end', { startTime: performance.now() + t }) this.#ticker = setInterval(Function.prototype, 1000) - this.#timeout = setTimeout(this.#f, this.#remaining) - this.#isPaused = false + this.#timeout = setTimeout(this.#f, this.#remaining.duration) } resume () { - if (this.#isPaused) { - this.#start = performance.now() - this.#ticker = setInterval(Function.prototype, 1000) - this.#timeout = setTimeout(this.#f, this.#remaining) - this.#isPaused = false + 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) } } }