@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
80 lines • 3.13 kB
JavaScript
import { isStaleExtensionContextError } from "../goal/stale-context.js";
import { formatMonitorStatus } from "./monitor-status.js";
/** Footer live-elapsed refresh cadence while at least one watch is active. */
export const MONITOR_STATUS_TICK_INTERVAL_MS = 1000;
/**
* Drives a once-per-second footer refresh while monitors are active so the
* "◉ watching … (Ns)" elapsed label advances live instead of freezing between
* registry transitions. Mirrors the goal builtin's GoalElapsedTicker: the
* interval is unref'd so it never keeps the process alive, ticks that
* produce the already-rendered label are skipped, and a render that hits a
* retired extension context retires the ticker until the next sync.
*/
export class MonitorStatusTicker {
constructor(options) {
this.snapshot = [];
this.hasRendered = false;
this.render = options.render;
this.now = options.now ?? Date.now;
this.animate = options.animate ?? (() => false);
}
get running() {
return this.intervalId !== undefined;
}
/**
* Point the ticker at the current monitor snapshot, render once immediately,
* and start the interval when watches are live (or stop it when none remain).
*/
sync(snapshot) {
this.snapshot = snapshot;
this.hasRendered = false;
if (!this.tick())
return;
if (snapshot.length === 0) {
this.stopInterval();
return;
}
if (this.intervalId !== undefined)
return;
const handle = setInterval(() => this.tick(), MONITOR_STATUS_TICK_INTERVAL_MS);
handle.unref();
this.intervalId = handle;
}
/** Stop the interval and drop the retained snapshot. */
stop() {
this.stopInterval();
this.snapshot = [];
this.lastRenderedStatus = undefined;
this.hasRendered = false;
}
stopInterval() {
if (this.intervalId !== undefined) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
}
/** Returns false when the render hit a retired context and the ticker retired. */
tick() {
try {
const status = formatMonitorStatus(this.snapshot, this.now(), this.animate());
if (this.hasRendered && status === this.lastRenderedStatus)
return true;
this.hasRendered = true;
this.lastRenderedStatus = status;
this.render(status);
}
catch (error) {
// The render and the animate gate read the extension's captured ctx; after a session replacement
// or reload that throws from inside the interval callback, where nothing
// catches it and the process exits. Retire instead: stop() renders nothing,
// and the next sync() (with a live ctx) re-arms the ticker.
if (isStaleExtensionContextError(error)) {
this.stop();
return false;
}
throw error;
}
return true;
}
}
//# sourceMappingURL=monitor-status-ticker.js.map