Something went wrong. Try again.
more up to date at codeberg https://codeberg.org/cowie/tau.git
Something went wrong. Try again.
983 B · 39 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940/** * Reusable countdown timer for dialog components. */
import type { TUI } from "@earendil-works/pi-tui";
export class CountdownTimer { private intervalId: ReturnType<typeof setInterval> | undefined; private remainingSeconds: number; private tui: TUI | undefined; private onTick: (seconds: number) => void; private onExpire: () => void;
constructor(timeoutMs: number, tui: TUI | undefined, onTick: (seconds: number) => void, onExpire: () => void) { this.tui = tui; this.onTick = onTick; this.onExpire = onExpire; this.remainingSeconds = Math.ceil(timeoutMs / 1000); this.onTick(this.remainingSeconds);
this.intervalId = setInterval(() => { this.remainingSeconds--; this.onTick(this.remainingSeconds); this.tui?.requestRender();
if (this.remainingSeconds <= 0) { this.dispose(); this.onExpire(); } }, 1000); }
dispose(): void { if (this.intervalId) { clearInterval(this.intervalId); this.intervalId = undefined; } }}