Skip to content
TypeScript

perf(web): sidebar working timers share one clock (#21195)

Alyx6ac3bc553e9f
diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx
index b955323c9a0..81eb57b37dd 100644
--- a/apps/web/src/components/Sidebar.tsx
+++ b/apps/web/src/components/Sidebar.tsx
@@ -151,6 +151,7 @@ import { useClientSettings } from "../hooks/useSettings";
import { useCopyToClipboard } from "../hooks/useCopyToClipboard";
import { useLocalStorage } from "../hooks/useLocalStorage";
import { useNowMinute } from "../hooks/useNowMinute";
+import { useNowSecond } from "../hooks/useNowSecond";
import {
useEnvironmentIdentities,
useConnectedEnvironmentIds,
@@ -848,18 +849,19 @@ function JumpHintBadge(props: { label: string }) {
);
}
-// Self-ticking so only this span re-renders each second, not the whole row.
function WorkingDuration(props: { startedAt: string | null }) {
const startedMs = props.startedAt !== null ? Date.parse(props.startedAt) : Number.NaN;
- // Mount time is close enough for the first frame; the interval keeps it current.
- const [nowMs, setNowMs] = useState(() => Date.now());
- useEffect(() => {
- if (Number.isNaN(startedMs)) return;
- const id = window.setInterval(() => setNowMs(Date.now()), 1_000);
- return () => window.clearInterval(id);
- }, [startedMs]);
if (Number.isNaN(startedMs)) return null;
- return <span className="tabular-nums">{formatWorkingDurationLabel(nowMs - startedMs)}</span>;
+ return <WorkingDurationLabel startedMs={startedMs} />;
+}
+
+// Self-ticking so only this span re-renders each second, not the whole row;
+// the shared tick lands every working row's update in the same frame.
+function WorkingDurationLabel(props: { startedMs: number }) {
+ const nowMs = useNowSecond();
+ return (
+ <span className="tabular-nums">{formatWorkingDurationLabel(nowMs - props.startedMs)}</span>
+ );
}
function terminalProcessLabel(count: number): string {
diff --git a/apps/web/src/hooks/useNowSecond.ts b/apps/web/src/hooks/useNowSecond.ts
new file mode 100644
index 00000000000..909dae18b2d
--- /dev/null
+++ b/apps/web/src/hooks/useNowSecond.ts
@@ -0,0 +1,46 @@
+import { useSyncExternalStore } from "react";
+
+// One shared tick on wall-clock second boundaries: every live duration label
+// updates in the same frame instead of each repainting on its own phase.
+
+function currentSecond(): number {
+ return Math.floor(Date.now() / 1000) * 1000;
+}
+
+let nowSecond = currentSecond();
+let timerId: number | null = null;
+const listeners = new Set<() => void>();
+
+function scheduleTick(): void {
+ timerId = window.setTimeout(
+ () => {
+ nowSecond = currentSecond();
+ for (const listener of listeners) listener();
+ scheduleTick();
+ },
+ 1000 - (Date.now() % 1000),
+ );
+}
+
+function subscribe(listener: () => void): () => void {
+ if (listeners.size === 0) scheduleTick();
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ if (listeners.size === 0 && timerId !== null) {
+ window.clearTimeout(timerId);
+ timerId = null;
+ }
+ };
+}
+
+function getSnapshot(): number {
+ // While the timer runs the cached value is returned untouched, as
+ // useSyncExternalStore requires between change notifications.
+ if (timerId === null) nowSecond = currentSecond();
+ return nowSecond;
+}
+
+export function useNowSecond(): number {
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
+}