@@ -141,28 +141,22 @@ export const wrapGrid = (
141141 } ;
142142 recordPositions ( container . children as HTMLCollectionOf < HTMLElement > ) ;
143143
144- const createMutationListener = (
145- eventType : string ,
146- throttleDuration : number ,
147- ) => {
148- const throttledListener = throttle ( ( ) => {
149- const bodyElement = document . querySelector ( 'body' ) ;
150- const containerIsNoLongerInPage =
151- bodyElement && ! bodyElement . contains ( container ) ;
152- if ( ! container || containerIsNoLongerInPage ) {
153- window . removeEventListener ( eventType , throttledListener ) ;
154- }
155- recordPositions ( container . children as HTMLCollectionOf < HTMLElement > ) ;
156- } , throttleDuration ) ;
157- return throttledListener ;
158- } ;
159-
160- const resizeListener = createMutationListener ( 'resize' , 250 ) ;
161- window . addEventListener ( 'resize' , resizeListener ) ;
144+ const throttledResizeListener = throttle ( ( ) => {
145+ const bodyElement = document . querySelector ( 'body' ) ;
146+ const containerIsNoLongerInPage =
147+ bodyElement && ! bodyElement . contains ( container ) ;
148+ if ( ! container || containerIsNoLongerInPage ) {
149+ window . removeEventListener ( 'resize' , throttledResizeListener ) ;
150+ }
151+ recordPositions ( container . children as HTMLCollectionOf < HTMLElement > ) ;
152+ } , 250 ) ;
153+ window . addEventListener ( 'resize' , throttledResizeListener ) ;
162154
163- const scrollListener = createMutationListener ( 'scroll' , 20 ) ;
155+ const throttledScrollListener = throttle ( ( ) => {
156+ recordPositions ( container . children as HTMLCollectionOf < HTMLElement > ) ;
157+ } , 20 ) ;
164158 if ( watchScroll ) {
165- window . addEventListener ( 'scroll' , scrollListener ) ;
159+ container . addEventListener ( 'scroll' , throttledScrollListener ) ;
166160 }
167161
168162 const mutationCallback = (
@@ -335,9 +329,9 @@ export const wrapGrid = (
335329 attributeFilter : [ 'class' ] ,
336330 } ) ;
337331 const unwrapGrid = ( ) => {
338- window . removeEventListener ( 'resize' , resizeListener ) ;
332+ window . removeEventListener ( 'resize' , throttledResizeListener ) ;
339333 if ( watchScroll ) {
340- window . removeEventListener ( 'scroll' , scrollListener ) ;
334+ container . removeEventListener ( 'scroll' , throttledScrollListener ) ;
341335 }
342336 observer . disconnect ( ) ;
343337 } ;
0 commit comments