*/
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
}
}
}