88 * @public
99 */
1010export function requestIdleCallback (
11- callback : ( args : { didTimeout : boolean } ) => void ,
11+ callback : ( deadline : IdleDeadline ) => void ,
1212 options ?: { timeout : number } ,
1313) : ReturnType < typeof globalThis . requestIdleCallback | typeof setTimeout > {
1414 if ( 'requestIdleCallback' in globalThis ) {
@@ -19,6 +19,7 @@ export function requestIdleCallback(
1919 return setTimeout ( ( ) => {
2020 callback ( {
2121 didTimeout : options ?. timeout ? Date . now ( ) - start >= options . timeout : false ,
22+ timeRemaining : ( ) => 0 ,
2223 } ) ;
2324 } , 1 ) ;
2425}
@@ -58,30 +59,25 @@ export function cancelIdleCallback(id: ReturnType<typeof globalThis.requestIdleC
5859export function waitForConnectedDescendants (
5960 target : HTMLElement ,
6061 callback : ( ) => void ,
61- options = { shallow : false , timeout : 50 } ,
62+ options ?: { shallow ?: boolean ; timeout ?: number } ,
6263) : void {
63- let idleCallbackId : ReturnType < typeof requestIdleCallback > | void ;
64- const query = options . shallow ? ':scope > *' : '*' ;
64+ let idleCallbackId : ReturnType < typeof requestIdleCallback > | undefined ;
65+ const shallow = options ?. shallow ?? false ;
66+ const query = `${ shallow ? ':scope > ' : '' } :not(:defined)` ;
67+ const timeout = options ?. timeout ?? 50 ;
6568
66- if ( ! target . isConnected ) {
67- return ;
68- }
69-
70- const scheduleCheck = ( ) => {
69+ const scheduleCheck = ( deadline ?: IdleDeadline ) => {
7170 if ( idleCallbackId ) {
72- idleCallbackId = cancelIdleCallback ( idleCallbackId ) ;
71+ cancelIdleCallback ( idleCallbackId ) ;
72+ idleCallbackId = undefined ;
7373 }
7474
75- if (
76- Array . from ( target . querySelectorAll ( query ) )
77- . filter ( el => el . tagName . includes ( '-' ) )
78- . every ( el => el . isConnected )
79- ) {
75+ if ( ! target . querySelector ( query ) || ( deadline && deadline . timeRemaining ( ) <= 0 ) ) {
8076 callback ( ) ;
8177 return ;
8278 }
8379
84- idleCallbackId = requestIdleCallback ( scheduleCheck , { timeout : options . timeout } ) ;
80+ idleCallbackId = requestIdleCallback ( scheduleCheck , { timeout } ) ;
8581 } ;
8682
8783 scheduleCheck ( ) ;
0 commit comments