@@ -26,7 +26,6 @@ import {
2626 type KeyboardEvent ,
2727 type PointerEvent ,
2828 type Ref ,
29- useCallback ,
3029 useEffect ,
3130 useEffectEvent ,
3231 useImperativeHandle ,
@@ -216,11 +215,13 @@ export function ServerBrowserSurface(props: {
216215 const popupOpened = useEffectEvent ( ( popupTabId : string ) => onPopup ?.( popupTabId ) ) ;
217216 const [ cap , setCap ] = useState < { width : number ; height : number } | null > ( null ) ;
218217
219- const send = useCallback ( ( input : PreviewStreamInput ) => {
218+ // These read only refs, so each is cached once. The stream effect keys on
219+ // clearInput and would reconnect if it changed.
220+ const send = ( input : PreviewStreamInput ) => {
220221 clientRef . current ?. send ( input ) ;
221- } , [ ] ) ;
222+ } ;
222223
223- const clearInput = useCallback ( ( ) => {
224+ const clearInput = ( ) => {
224225 if ( inputFrameRef . current !== null ) cancelAnimationFrame ( inputFrameRef . current ) ;
225226 inputFrameRef . current = null ;
226227 pendingMoveRef . current = pendingWheelRef . current = null ;
@@ -231,9 +232,9 @@ export function ServerBrowserSurface(props: {
231232 keySentRef . current = false ;
232233 if ( inputRef . current ) inputRef . current . value = INPUT_SENTINEL ;
233234 inputRef . current ?. blur ( ) ;
234- } , [ ] ) ;
235+ } ;
235236
236- const flushInput = useCallback ( ( ) => {
237+ const flushInput = ( ) => {
237238 if ( inputFrameRef . current !== null ) cancelAnimationFrame ( inputFrameRef . current ) ;
238239 inputFrameRef . current = null ;
239240 const move = pendingMoveRef . current ;
@@ -242,52 +243,46 @@ export function ServerBrowserSurface(props: {
242243 pendingWheelRef . current = null ;
243244 if ( move ) send ( move ) ;
244245 if ( wheel ) send ( wheel ) ;
245- } , [ send ] ) ;
246-
247- const scheduleFlush = useCallback ( ( ) => {
248- inputFrameRef . current ??= requestAnimationFrame ( flushInput ) ;
249- } , [ flushInput ] ) ;
250-
251- const queueWheel = useCallback (
252- ( point : PagePoint , deltaX : number , deltaY : number , modifiers : number ) => {
253- const pending = pendingWheelRef . current ;
254- pendingWheelRef . current = {
255- type : "wheel" ,
256- x : point . x ,
257- y : point . y ,
258- deltaX : ( pending ?. deltaX ?? 0 ) + deltaX ,
259- deltaY : ( pending ?. deltaY ?? 0 ) + deltaY ,
260- modifiers,
261- } ;
262- scheduleFlush ( ) ;
263- } ,
264- [ scheduleFlush ] ,
265- ) ;
246+ } ;
266247
267- const pagePoint = useCallback (
268- ( clientX : number , clientY : number , clamp : boolean ) : PagePoint | null => {
269- const canvas = canvasRef . current ;
270- const viewport = viewportRef . current ;
271- if ( ! canvas || ! viewport || ! hasFrameRef . current || controlRef . current ?. controller !== "you" )
272- return null ;
273- const rect = canvas . getBoundingClientRect ( ) ;
274- const fit = Math . min ( rect . width / canvas . width , rect . height / canvas . height ) ;
275- const width = canvas . width * fit ;
276- const height = canvas . height * fit ;
277- if ( ! ( width > 0 && height > 0 ) ) return null ;
278- const scale = viewport . width / width ;
279- const x = ( clientX - rect . left - ( rect . width - width ) / 2 ) * scale ;
280- const y = ( clientY - rect . top - ( rect . height - height ) / 2 ) * ( viewport . height / height ) ;
281- const inside = x >= 0 && y >= 0 && x <= viewport . width && y <= viewport . height ;
282- if ( ! inside && ! clamp ) return null ;
283- return {
284- x : Math . min ( Math . max ( x , 0 ) , viewport . width ) ,
285- y : Math . min ( Math . max ( y , 0 ) , viewport . height ) ,
286- scale,
287- } ;
288- } ,
289- [ ] ,
290- ) ;
248+ const scheduleFlush = ( ) => {
249+ if ( inputFrameRef . current === null ) inputFrameRef . current = requestAnimationFrame ( flushInput ) ;
250+ } ;
251+
252+ const queueWheel = ( point : PagePoint , deltaX : number , deltaY : number , modifiers : number ) => {
253+ const pending = pendingWheelRef . current ;
254+ pendingWheelRef . current = {
255+ type : "wheel" ,
256+ x : point . x ,
257+ y : point . y ,
258+ deltaX : ( pending ?. deltaX ?? 0 ) + deltaX ,
259+ deltaY : ( pending ?. deltaY ?? 0 ) + deltaY ,
260+ modifiers,
261+ } ;
262+ scheduleFlush ( ) ;
263+ } ;
264+
265+ const pagePoint = ( clientX : number , clientY : number , clamp : boolean ) : PagePoint | null => {
266+ const canvas = canvasRef . current ;
267+ const viewport = viewportRef . current ;
268+ if ( ! canvas || ! viewport || ! hasFrameRef . current || controlRef . current ?. controller !== "you" )
269+ return null ;
270+ const rect = canvas . getBoundingClientRect ( ) ;
271+ const fit = Math . min ( rect . width / canvas . width , rect . height / canvas . height ) ;
272+ const width = canvas . width * fit ;
273+ const height = canvas . height * fit ;
274+ if ( ! ( width > 0 && height > 0 ) ) return null ;
275+ const scale = viewport . width / width ;
276+ const x = ( clientX - rect . left - ( rect . width - width ) / 2 ) * scale ;
277+ const y = ( clientY - rect . top - ( rect . height - height ) / 2 ) * ( viewport . height / height ) ;
278+ const inside = x >= 0 && y >= 0 && x <= viewport . width && y <= viewport . height ;
279+ if ( ! inside && ! clamp ) return null ;
280+ return {
281+ x : Math . min ( Math . max ( x , 0 ) , viewport . width ) ,
282+ y : Math . min ( Math . max ( y , 0 ) , viewport . height ) ,
283+ scale,
284+ } ;
285+ } ;
291286
292287 const countClick = ( button : PreviewStreamMouseButton , x : number , y : number , time : number ) => {
293288 const last = lastClickRef . current ;
0 commit comments