@@ -91,23 +91,26 @@ const DEFAULTS = {
9191// Helpers
9292// ---------------------------------------------------------------------------
9393
94- function getWebVital ( name : string ) : number | null {
94+ function getFCP ( ) : number | null {
9595 if ( typeof performance === 'undefined' || ! performance . getEntriesByType ) return null ;
9696 try {
9797 const entries = performance . getEntriesByType ( 'paint' ) ;
98- const entry = entries . find ( ( e ) => e . name === name ) ;
98+ const entry = entries . find ( ( e ) => e . name === 'first-contentful-paint' ) ;
9999 return entry ? Math . round ( entry . startTime ) : null ;
100100 } catch {
101101 return null ;
102102 }
103103}
104104
105105function getLCP ( ) : number | null {
106- return getWebVital ( 'largest-contentful-paint' ) ;
107- }
108-
109- function getFCP ( ) : number | null {
110- return getWebVital ( 'first-contentful-paint' ) ;
106+ if ( typeof performance === 'undefined' || ! performance . getEntriesByType ) return null ;
107+ try {
108+ const entries = performance . getEntriesByType ( 'largest-contentful-paint' ) ;
109+ const entry = entries . length > 0 ? entries [ entries . length - 1 ] : undefined ;
110+ return entry ? Math . round ( entry . startTime ) : null ;
111+ } catch {
112+ return null ;
113+ }
111114}
112115
113116// ---------------------------------------------------------------------------
@@ -205,23 +208,33 @@ export function usePerformance(userConfig: PerformanceConfig = {}): PerformanceR
205208 } , [ ] ) ;
206209
207210 const debounceMs = config . debounceMs ;
208- const timerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
211+ const timersRef = useRef < Set < ReturnType < typeof setTimeout > > > ( new Set ( ) ) ;
209212
210213 const debounce = useCallback (
211214 < T extends ( ...args : unknown [ ] ) => void > ( fn : T ) : T => {
215+ let timer : ReturnType < typeof setTimeout > | null = null ;
212216 const debounced = ( ...args : unknown [ ] ) => {
213- if ( timerRef . current ) clearTimeout ( timerRef . current ) ;
214- timerRef . current = setTimeout ( ( ) => fn ( ...args ) , debounceMs ) ;
217+ if ( timer ) {
218+ clearTimeout ( timer ) ;
219+ timersRef . current . delete ( timer ) ;
220+ }
221+ timer = setTimeout ( ( ) => {
222+ fn ( ...args ) ;
223+ if ( timer ) timersRef . current . delete ( timer ) ;
224+ } , debounceMs ) ;
225+ timersRef . current . add ( timer ) ;
215226 } ;
216227 return debounced as unknown as T ;
217228 } ,
218229 [ debounceMs ] ,
219230 ) ;
220231
221- // Cleanup debounce timer
232+ // Cleanup all debounce timers
222233 useEffect ( ( ) => {
234+ const timers = timersRef . current ;
223235 return ( ) => {
224- if ( timerRef . current ) clearTimeout ( timerRef . current ) ;
236+ timers . forEach ( ( t ) => clearTimeout ( t ) ) ;
237+ timers . clear ( ) ;
225238 } ;
226239 } , [ ] ) ;
227240
0 commit comments