The spinner ran a CSS animation on every active row for the whole turn, repainting a composited layer at frame rate. Rows now carry a static dot — primary while running, info while unread — and the metadata slot on the right shows how long the turn has been going, updating once per second in the dot's colour. The counter is the motion the spinner used to provide, at 1 fps. Collapsed groups, folders and projects take the dot only, since one counter cannot speak for several running turns. Elapsed time is measured client-side because SessionStatus carries no timestamps, and starts are persisted so a reload resumes the same count. Two rules keep that honest. Only a liveness stamp — refreshed while a session is observed active, stamped as the page hides, and compared against the page's navigation start so a slow bootstrap is not charged to the absence — and a 90s adoption window may expire a record; a snapshot that cannot yet see a session is not evidence its turn ended. And a busy event is never read as a turn boundary, because OpenCode republishes busy at every step of the agent loop, so after a reload one of those repeats normally beats the first status snapshot. Idle and error events do end a turn, and retire the record with it. Snapshot reconciliation walks the running turns and asks whether the snapshot covers each one, rather than being handed everything it covers: only a live start can settle, so the pass scales with timing work instead of with the directory's session list, and allocates nothing per poll. Also applied to the mobile sessions sheet and session switcher. The shared duration ticker moves to hooks/ now that it has a second consumer.
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
// Shared wall-clock ticker for live duration readouts (tool runtimes, session
|
|
// activity counters). Subscribers of the same interval share one timer and one
|
|
// `now`, so N live rows cost one interval rather than N.
|
|
|
|
import React from 'react';
|
|
|
|
type Subscriber = (now: number) => void;
|
|
|
|
type TickerChannel = {
|
|
subscribers: Set<Subscriber>;
|
|
timerId: number | null;
|
|
};
|
|
|
|
const tickerChannels = new Map<number, TickerChannel>();
|
|
|
|
const getTickerChannel = (intervalMs: number): TickerChannel => {
|
|
const existing = tickerChannels.get(intervalMs);
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
|
|
const created: TickerChannel = {
|
|
subscribers: new Set<Subscriber>(),
|
|
timerId: null,
|
|
};
|
|
tickerChannels.set(intervalMs, created);
|
|
return created;
|
|
};
|
|
|
|
const subscribeToTicker = (intervalMs: number, subscriber: Subscriber): (() => void) => {
|
|
const channel = getTickerChannel(intervalMs);
|
|
channel.subscribers.add(subscriber);
|
|
subscriber(Date.now());
|
|
|
|
if (channel.timerId === null && typeof window !== 'undefined') {
|
|
channel.timerId = window.setInterval(() => {
|
|
const now = Date.now();
|
|
channel.subscribers.forEach((listener) => {
|
|
listener(now);
|
|
});
|
|
}, intervalMs);
|
|
}
|
|
|
|
return () => {
|
|
const tracked = tickerChannels.get(intervalMs);
|
|
if (!tracked) {
|
|
return;
|
|
}
|
|
|
|
tracked.subscribers.delete(subscriber);
|
|
if (tracked.subscribers.size > 0) {
|
|
return;
|
|
}
|
|
|
|
if (tracked.timerId !== null && typeof window !== 'undefined') {
|
|
window.clearInterval(tracked.timerId);
|
|
}
|
|
tickerChannels.delete(intervalMs);
|
|
};
|
|
};
|
|
|
|
export const useDurationTickerNow = (active: boolean, intervalMs: number = 250): number => {
|
|
const [now, setNow] = React.useState(() => Date.now());
|
|
|
|
React.useEffect(() => {
|
|
if (!active) {
|
|
return;
|
|
}
|
|
|
|
return subscribeToTicker(intervalMs, setNow);
|
|
}, [active, intervalMs]);
|
|
|
|
return now;
|
|
};
|