@@ -57,26 +57,33 @@ const strategies: Record<string, Strategy> = {
5757
5858type ElementLike = Element | Document | ShadowRoot
5959
60- const timers = new WeakMap < ElementLike , number > ( )
60+ const pendingElements = new Set < ElementLike > ( )
61+ let scanTimer : number | null = null
62+
6163function scan ( element : ElementLike ) {
62- cancelAnimationFrame ( timers . get ( element ) || 0 )
63- timers . set (
64- element ,
65- requestAnimationFrame ( ( ) => {
64+ pendingElements . add ( element )
65+ if ( scanTimer != null ) return
66+ scanTimer = requestAnimationFrame ( ( ) => {
67+ scanTimer = null
68+ const elements = new Set ( pendingElements )
69+ pendingElements . clear ( )
70+ if ( ! dynamicElements . size ) {
71+ return
72+ }
73+ outer: for ( const el of elements ) {
6674 for ( const tagName of dynamicElements . keys ( ) ) {
67- const child : Element | null =
68- element instanceof Element && element . matches ( tagName ) ? element : element . querySelector ( tagName )
75+ const child : Element | null = el instanceof Element && el . matches ( tagName ) ? el : el . querySelector ( tagName )
6976 if ( customElements . get ( tagName ) || child ) {
7077 const strategyName = ( child ?. getAttribute ( 'data-load-on' ) || 'ready' ) as keyof typeof strategies
7178 const strategy = strategyName in strategies ? strategies [ strategyName ] : strategies . ready
7279 // eslint-disable-next-line github/no-then
7380 for ( const cb of dynamicElements . get ( tagName ) || [ ] ) strategy ( tagName ) . then ( cb )
7481 dynamicElements . delete ( tagName )
75- timers . delete ( element )
82+ if ( ! dynamicElements . size ) break outer
7683 }
7784 }
78- } )
79- )
85+ }
86+ } )
8087}
8188
8289let elementLoader : MutationObserver
0 commit comments