Skip to content
TypeScript

perf(web): useLiveRefresh reads its callback through useEffectEvent (#20464)

Julius Marmingede89a7a76dfa
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index 752a0fab707..d85925d8dfb 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -39,4 +39,5 @@ export const REACT_COMPILED_FILES = [
"apps/web/src/components/auth/PairingRouteSurface.tsx",
"apps/web/src/components/Sidebar.logic.ts",
"apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
+ "apps/web/src/hooks/useLiveRefresh.ts",
] as const;
diff --git a/apps/web/src/hooks/useLiveRefresh.ts b/apps/web/src/hooks/useLiveRefresh.ts
index 18e4784932f..90d76d3ff78 100644
--- a/apps/web/src/hooks/useLiveRefresh.ts
+++ b/apps/web/src/hooks/useLiveRefresh.ts
@@ -1,4 +1,4 @@
-import { useEffect, useId, useRef } from "react";
+import { useEffect, useEffectEvent, useId } from "react";
export const LIVE_REFRESH_MIN_INTERVAL_MS = 10_000;
@@ -83,20 +83,19 @@ export function useLiveRefresh(
} = {},
): void {
const { enabled = true, key, intervalMs = LIVE_REFRESH_INTERVAL_MS } = options;
- // Held in a ref so a caller can pass a fresh closure every render without re-arming the
+ // An effect event so a caller can pass a fresh closure every render without re-arming the
// listeners, which would otherwise refresh on every render that changed anything at all.
- const latest = useRef(refresh);
- latest.current = refresh;
+ const runRefresh = useEffectEvent((viewId: string, now: number) => {
+ if (refresh === null) return;
+ lastRefreshedAtByView.set(viewId, now);
+ refresh();
+ });
const fallbackViewId = useId();
const viewId = key ?? fallbackViewId;
useEffect(() => {
if (!enabled) return;
- const read = (now: number) => {
- if (latest.current === null) return;
- lastRefreshedAtByView.set(viewId, now);
- latest.current();
- };
+ const read = (now: number) => runRefresh(viewId, now);
const visible = () => document.visibilityState === "visible";
const onArrival = () => {
const now = Date.now();