mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
39 lines
983 B
TypeScript
39 lines
983 B
TypeScript
/**
|
|
* 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;
|
|
}
|
|
}
|
|
}
|