Skip to content

Commit 843c8f7

Browse files
perf(web): useLiveRefresh reads its callback through useEffectEvent
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 30bd865 commit 843c8f7

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ export const REACT_COMPILED_FILES = [
3939
"apps/web/src/components/auth/PairingRouteSurface.tsx",
4040
"apps/web/src/components/Sidebar.logic.ts",
4141
"apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
42+
"apps/web/src/hooks/useLiveRefresh.ts",
4243
] as const;

‎apps/web/src/hooks/useLiveRefresh.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useId, useRef } from "react";
1+
import { useEffect, useEffectEvent, useId } from "react";
22

33
export const LIVE_REFRESH_MIN_INTERVAL_MS = 10_000;
44

@@ -83,20 +83,19 @@ export function useLiveRefresh(
8383
} = {},
8484
): void {
8585
const { enabled = true, key, intervalMs = LIVE_REFRESH_INTERVAL_MS } = options;
86-
// Held in a ref so a caller can pass a fresh closure every render without re-arming the
86+
// An effect event so a caller can pass a fresh closure every render without re-arming the
8787
// listeners, which would otherwise refresh on every render that changed anything at all.
88-
const latest = useRef(refresh);
89-
latest.current = refresh;
88+
const runRefresh = useEffectEvent((viewId: string, now: number) => {
89+
if (refresh === null) return;
90+
lastRefreshedAtByView.set(viewId, now);
91+
refresh();
92+
});
9093
const fallbackViewId = useId();
9194
const viewId = key ?? fallbackViewId;
9295

9396
useEffect(() => {
9497
if (!enabled) return;
95-
const read = (now: number) => {
96-
if (latest.current === null) return;
97-
lastRefreshedAtByView.set(viewId, now);
98-
latest.current();
99-
};
98+
const read = (now: number) => runRefresh(viewId, now);
10099
const visible = () => document.visibilityState === "visible";
101100
const onArrival = () => {
102101
const now = Date.now();

0 commit comments

Comments
 (0)