Skip to content
TypeScript

perf(web): the server browser surface compiles with the React Compiler (#20451)

Julius Marmingefd3420f3805a
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index ec21aa9e1b3..51be1866ced 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -26,4 +26,5 @@ export const REACT_COMPILED_FILES = [
"apps/web/src/hooks/useRightPanelSurfaceClose.ts",
"apps/web/src/components/ui/tooltip.tsx",
"apps/web/src/components/ComposerPromptEditorTiptap.tsx",
+ "apps/web/src/browser/ServerBrowserSurface.tsx",
] as const;
diff --git a/apps/web/src/browser/ServerBrowserSurface.tsx b/apps/web/src/browser/ServerBrowserSurface.tsx
index ef5070752b8..45dad35db16 100644
--- a/apps/web/src/browser/ServerBrowserSurface.tsx
+++ b/apps/web/src/browser/ServerBrowserSurface.tsx
@@ -26,7 +26,6 @@ import {
type KeyboardEvent,
type PointerEvent,
type Ref,
- useCallback,
useEffect,
useEffectEvent,
useImperativeHandle,
@@ -216,11 +215,13 @@ export function ServerBrowserSurface(props: {
const popupOpened = useEffectEvent((popupTabId: string) => onPopup?.(popupTabId));
const [cap, setCap] = useState<{ width: number; height: number } | null>(null);
- const send = useCallback((input: PreviewStreamInput) => {
+ // These read only refs, so each is cached once. The stream effect keys on
+ // clearInput and would reconnect if it changed.
+ const send = (input: PreviewStreamInput) => {
clientRef.current?.send(input);
- }, []);
+ };
- const clearInput = useCallback(() => {
+ const clearInput = () => {
if (inputFrameRef.current !== null) cancelAnimationFrame(inputFrameRef.current);
inputFrameRef.current = null;
pendingMoveRef.current = pendingWheelRef.current = null;
@@ -231,9 +232,9 @@ export function ServerBrowserSurface(props: {
keySentRef.current = false;
if (inputRef.current) inputRef.current.value = INPUT_SENTINEL;
inputRef.current?.blur();
- }, []);
+ };
- const flushInput = useCallback(() => {
+ const flushInput = () => {
if (inputFrameRef.current !== null) cancelAnimationFrame(inputFrameRef.current);
inputFrameRef.current = null;
const move = pendingMoveRef.current;
@@ -242,52 +243,46 @@ export function ServerBrowserSurface(props: {
pendingWheelRef.current = null;
if (move) send(move);
if (wheel) send(wheel);
- }, [send]);
-
- const scheduleFlush = useCallback(() => {
- inputFrameRef.current ??= requestAnimationFrame(flushInput);
- }, [flushInput]);
-
- const queueWheel = useCallback(
- (point: PagePoint, deltaX: number, deltaY: number, modifiers: number) => {
- const pending = pendingWheelRef.current;
- pendingWheelRef.current = {
- type: "wheel",
- x: point.x,
- y: point.y,
- deltaX: (pending?.deltaX ?? 0) + deltaX,
- deltaY: (pending?.deltaY ?? 0) + deltaY,
- modifiers,
- };
- scheduleFlush();
- },
- [scheduleFlush],
- );
+ };
- const pagePoint = useCallback(
- (clientX: number, clientY: number, clamp: boolean): PagePoint | null => {
- const canvas = canvasRef.current;
- const viewport = viewportRef.current;
- if (!canvas || !viewport || !hasFrameRef.current || controlRef.current?.controller !== "you")
- return null;
- const rect = canvas.getBoundingClientRect();
- const fit = Math.min(rect.width / canvas.width, rect.height / canvas.height);
- const width = canvas.width * fit;
- const height = canvas.height * fit;
- if (!(width > 0 && height > 0)) return null;
- const scale = viewport.width / width;
- const x = (clientX - rect.left - (rect.width - width) / 2) * scale;
- const y = (clientY - rect.top - (rect.height - height) / 2) * (viewport.height / height);
- const inside = x >= 0 && y >= 0 && x <= viewport.width && y <= viewport.height;
- if (!inside && !clamp) return null;
- return {
- x: Math.min(Math.max(x, 0), viewport.width),
- y: Math.min(Math.max(y, 0), viewport.height),
- scale,
- };
- },
- [],
- );
+ const scheduleFlush = () => {
+ if (inputFrameRef.current === null) inputFrameRef.current = requestAnimationFrame(flushInput);
+ };
+
+ const queueWheel = (point: PagePoint, deltaX: number, deltaY: number, modifiers: number) => {
+ const pending = pendingWheelRef.current;
+ pendingWheelRef.current = {
+ type: "wheel",
+ x: point.x,
+ y: point.y,
+ deltaX: (pending?.deltaX ?? 0) + deltaX,
+ deltaY: (pending?.deltaY ?? 0) + deltaY,
+ modifiers,
+ };
+ scheduleFlush();
+ };
+
+ const pagePoint = (clientX: number, clientY: number, clamp: boolean): PagePoint | null => {
+ const canvas = canvasRef.current;
+ const viewport = viewportRef.current;
+ if (!canvas || !viewport || !hasFrameRef.current || controlRef.current?.controller !== "you")
+ return null;
+ const rect = canvas.getBoundingClientRect();
+ const fit = Math.min(rect.width / canvas.width, rect.height / canvas.height);
+ const width = canvas.width * fit;
+ const height = canvas.height * fit;
+ if (!(width > 0 && height > 0)) return null;
+ const scale = viewport.width / width;
+ const x = (clientX - rect.left - (rect.width - width) / 2) * scale;
+ const y = (clientY - rect.top - (rect.height - height) / 2) * (viewport.height / height);
+ const inside = x >= 0 && y >= 0 && x <= viewport.width && y <= viewport.height;
+ if (!inside && !clamp) return null;
+ return {
+ x: Math.min(Math.max(x, 0), viewport.width),
+ y: Math.min(Math.max(y, 0), viewport.height),
+ scale,
+ };
+ };
const countClick = (button: PreviewStreamMouseButton, x: number, y: number, time: number) => {
const last = lastClickRef.current;