Skip to content

Commit 2520821

Browse files
perf(web): the device stream view compiles without manual memoization (#20645)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent fcda161 commit 2520821

1 file changed

Lines changed: 49 additions & 43 deletions

File tree

‎apps/web/src/components/device/DeviceStreamView.tsx‎

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DevicePlatform, EnvironmentId } from "@t3tools/contracts";
2-
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
2+
import { useEffect, useRef, useState, type ReactNode } from "react";
33

44
import { cn } from "~/lib/utils";
55
import { Button } from "~/components/ui/button";
@@ -23,10 +23,40 @@ import {
2323
type DeviceStreamStatus,
2424
} from "@t3tools/client-runtime/device/stream";
2525
import { observeResize } from "~/lib/observeResize";
26+
import { useStableCallback } from "~/hooks/useStableCallback";
2627

2728
const AX_POLL_INTERVAL_MS = 2_000;
2829
const CONTROLS_RAIL_WIDTH = 56;
2930

31+
function screenAspect(platform: DevicePlatform, screen: DeviceScreenSize | null): number {
32+
if (!screen) return platform === "ios" ? 9 / 19.5 : 9 / 20;
33+
const landscape =
34+
screen.orientation === "landscape_left" || screen.orientation === "landscape_right";
35+
const w = landscape
36+
? Math.max(screen.width, screen.height)
37+
: Math.min(screen.width, screen.height);
38+
const h = landscape
39+
? Math.min(screen.width, screen.height)
40+
: Math.max(screen.width, screen.height);
41+
return w / h;
42+
}
43+
44+
// serve-sim streams the raw framebuffer; rotate the display for a device
45+
// that reports landscape while its frames stay portrait.
46+
function screenRotation(platform: DevicePlatform, screen: DeviceScreenSize | null): number {
47+
if (platform !== "ios" || !screen || screen.width > screen.height) return 0;
48+
switch (screen.orientation) {
49+
case "landscape_left":
50+
return 90;
51+
case "landscape_right":
52+
return -90;
53+
case "portrait_upside_down":
54+
return 180;
55+
default:
56+
return 0;
57+
}
58+
}
59+
3060
export interface DeviceViewControls {
3161
readonly phone: boolean;
3262
readonly streaming: boolean;
@@ -68,20 +98,22 @@ export function DeviceStreamView(props: {
6898
const [presentation, setPresentation] = useState<"phone" | "flat">("phone");
6999
const [keyboardAttached, setKeyboardAttached] = useState(false);
70100
const [phoneUnavailable, setPhoneUnavailable] = useState(false);
71-
const onPhoneUnavailable = useCallback(() => setPhoneUnavailable(true), []);
101+
// The stream effect and the viewports' scene setup depend on these; a new
102+
// identity per render would restart the stream and rebuild the 3D scene.
103+
const onPhoneUnavailable = useStableCallback(() => setPhoneUnavailable(true));
72104
const cancelPhoneInputRef = useRef<(() => void) | null>(null);
73-
const cancelPhoneInput = useCallback(() => cancelPhoneInputRef.current?.(), []);
105+
const cancelPhoneInput = useStableCallback(() => cancelPhoneInputRef.current?.());
74106
const resetViewRef = useRef<(() => void) | null>(null);
75-
const onResetReady = useCallback((reset: (() => void) | null) => {
107+
const onResetReady = useStableCallback((reset: (() => void) | null) => {
76108
resetViewRef.current = reset;
77-
}, []);
109+
});
78110
const frameListenerRef = useRef<(() => void) | null>(null);
79-
const onFrameListener = useCallback((listener: (() => void) | null) => {
111+
const onFrameListener = useStableCallback((listener: (() => void) | null) => {
80112
frameListenerRef.current = listener;
81-
}, []);
82-
const onInputCancel = useCallback((cancel: (() => void) | null) => {
113+
});
114+
const onInputCancel = useStableCallback((cancel: (() => void) | null) => {
83115
cancelPhoneInputRef.current = cancel;
84-
}, []);
116+
});
85117
const access = useDeviceHubAccess(props.environmentId, props.hostId);
86118
const canvasRef = useRef<HTMLCanvasElement | null>(null);
87119
const clientRef = useRef<DeviceStreamClient | null>(null);
@@ -93,9 +125,10 @@ export function DeviceStreamView(props: {
93125
const [foldAngle, setFoldAngle] = useState<number | null>(null);
94126
const [mjpegUrl, setMjpegUrl] = useState<string | null>(null);
95127
const [mjpegGeneration, setMjpegGeneration] = useState(0);
96-
const attachMjpegImage = useCallback((image: HTMLImageElement | null) => {
128+
// A new ref callback per render would detach and reattach the MJPEG image.
129+
const attachMjpegImage = useStableCallback((image: HTMLImageElement | null) => {
97130
clientRef.current?.setMjpegImage(image);
98-
}, []);
131+
});
99132
const [inputState, setInputState] = useState<{ connected: boolean; detail?: string }>({
100133
connected: false,
101134
});
@@ -169,18 +202,7 @@ export function DeviceStreamView(props: {
169202
props.visible,
170203
]);
171204

172-
const aspect = useMemo(() => {
173-
if (!screen) return props.platform === "ios" ? 9 / 19.5 : 9 / 20;
174-
const landscape =
175-
screen.orientation === "landscape_left" || screen.orientation === "landscape_right";
176-
const w = landscape
177-
? Math.max(screen.width, screen.height)
178-
: Math.min(screen.width, screen.height);
179-
const h = landscape
180-
? Math.min(screen.width, screen.height)
181-
: Math.max(screen.width, screen.height);
182-
return w / h;
183-
}, [props.platform, screen]);
205+
const aspect = screenAspect(props.platform, screen);
184206

185207
const retainingFrame =
186208
status === "connecting" && hasFrame && inputState.connected && screen !== null;
@@ -220,25 +242,9 @@ export function DeviceStreamView(props: {
220242
update();
221243
return observeResize(element, update);
222244
}, []);
223-
const frame = useMemo(() => {
224-
return fitDeviceFrame(aspect, host.width, host.height, controlsInset);
225-
}, [aspect, controlsInset, host]);
245+
const frame = fitDeviceFrame(aspect, host.width, host.height, controlsInset);
226246

227-
// serve-sim streams the raw framebuffer; rotate the display for a device
228-
// that reports landscape while its frames stay portrait.
229-
const rotation = useMemo(() => {
230-
if (props.platform !== "ios" || !screen || screen.width > screen.height) return 0;
231-
switch (screen.orientation) {
232-
case "landscape_left":
233-
return 90;
234-
case "landscape_right":
235-
return -90;
236-
case "portrait_upside_down":
237-
return 180;
238-
default:
239-
return 0;
240-
}
241-
}, [props.platform, screen]);
247+
const rotation = screenRotation(props.platform, screen);
242248

243249
const sideways = rotation === 90 || rotation === -90;
244250
const mediaStyle: React.CSSProperties = sideways
@@ -299,13 +305,13 @@ export function DeviceStreamView(props: {
299305
: null;
300306

301307
const keyboardSource = deviceKeyboard(props.platform, props.deviceName ?? "");
302-
const resetView = useCallback(() => {
308+
const resetView = () => {
303309
if (!isDuo) {
304310
const orientation = keyboardAttached ? "landscape_right" : "portrait";
305311
if (screen?.orientation !== orientation) clientRef.current?.setOrientation(orientation);
306312
}
307313
resetViewRef.current?.();
308-
}, [isDuo, keyboardAttached, screen?.orientation]);
314+
};
309315
const profile = resolveDeviceShape({
310316
platform: props.platform,
311317
name: props.deviceName ?? "",

0 commit comments

Comments
 (0)