@@ -3,7 +3,7 @@ import { getNearestHostFiber } from '../instrumentation/fiber';
33import type { Render } from '../instrumentation/index' ;
44import { ReactScanInternals } from '../index' ;
55import { getLabelText } from '../utils' ;
6- import { isOutlineUnstable , throttle } from './utils' ;
6+ import { isOutlineUnstable , onIdle , throttle } from './utils' ;
77import { log } from './log' ;
88import { recalcOutlineColor } from './perf-observer' ;
99// import { recalcOutlineColor } from './perf-observer';
@@ -32,14 +32,21 @@ export interface PaintedOutline {
3232
3333export const MONO_FONT =
3434 'Menlo,Consolas,Monaco,Liberation Mono,Lucida Console,monospace' ;
35- const DEFAULT_THROTTLE_TIME = 8 ; // 2 frames
35+ const DEFAULT_THROTTLE_TIME = 16 ; // 1 frame
3636export const colorRef = { current : '115,97,230' } ;
3737
3838export const getOutlineKey = ( outline : PendingOutline ) : string => {
3939 return `${ outline . rect . top } -${ outline . rect . left } -${ outline . rect . width } -${ outline . rect . height } ` ;
4040} ;
4141
42+ const rectCache = new Map < HTMLElement , { rect : DOMRect ; timestamp : number } > ( ) ;
43+
4244export const getRect = ( domNode : HTMLElement ) : DOMRect | null => {
45+ const cached = rectCache . get ( domNode ) ;
46+ if ( cached && cached . timestamp > performance . now ( ) - DEFAULT_THROTTLE_TIME ) {
47+ return cached . rect ;
48+ }
49+
4350 const style = window . getComputedStyle ( domNode ) ;
4451 if (
4552 style . display === 'none' ||
@@ -61,6 +68,8 @@ export const getRect = (domNode: HTMLElement): DOMRect | null => {
6168 return null ;
6269 }
6370
71+ rectCache . set ( domNode , { rect, timestamp : performance . now ( ) } ) ;
72+
6473 return rect ;
6574} ;
6675
@@ -285,10 +294,13 @@ export const fadeOutOutline = (ctx: CanvasRenderingContext2D) => {
285294 const activeOutline = activeOutlines [ i ] ;
286295 if ( ! activeOutline ) continue ;
287296 const { outline, frame, totalFrames } = activeOutline ;
288- // const newRect = getRect(outline.domNode);
289- // if (newRect) {
290- // outline.rect = newRect;
291- // }
297+ requestAnimationFrame ( ( ) => {
298+ if ( ! outline ) return ;
299+ const newRect = getRect ( outline . domNode ) ;
300+ if ( newRect ) {
301+ outline . rect = newRect ;
302+ }
303+ } ) ;
292304 const { rect } = outline ;
293305 const unstable = isOutlineUnstable ( outline ) ;
294306
0 commit comments