File tree Expand file tree Collapse file tree 2 files changed +4
-26
lines changed Expand file tree Collapse file tree 2 files changed +4
-26
lines changed Original file line number Diff line number Diff line change @@ -62,12 +62,11 @@ let isPageLifecycleInitialized = false;
6262function onPageReady ( callback : ( ) => void ) {
6363 if ( typeof window === 'undefined' ) return ;
6464
65- if ( isPageLifecycleInitialized ) {
66- return ;
67- }
68- isPageLifecycleInitialized = true ;
69-
7065 if ( document . readyState === 'complete' ) {
66+ if ( isPageLifecycleInitialized ) {
67+ return ;
68+ }
69+ isPageLifecycleInitialized = true ;
7170 callback ( ) ;
7271 } else {
7372 document . addEventListener ( 'readystatechange' , function onReadyStateChange ( ) {
Original file line number Diff line number Diff line change @@ -232,25 +232,4 @@ describe('pageLifecycle', () => {
232232 global . window = originalWindow ;
233233 } ) ;
234234 } ) ;
235-
236- describe ( 'preventing duplicate initialization' , ( ) => {
237- it ( 'should not initialize listeners multiple times' , ( ) => {
238- setReadyState ( 'loading' ) ;
239- const { onPageLoaded } = importPageLifecycle ( ) ;
240- const callback1 = jest . fn ( ) ;
241- const callback2 = jest . fn ( ) ;
242-
243- // First call should initialize and call addEventListener
244- onPageLoaded ( callback1 ) ;
245- expect ( addEventListenerSpy ) . toHaveBeenCalledTimes ( 1 ) ;
246-
247- // Second call should not add more listeners (isPageLifecycleInitialized is true)
248- onPageLoaded ( callback2 ) ;
249- expect ( addEventListenerSpy ) . toHaveBeenCalledTimes ( 1 ) ;
250-
251- // Both callbacks should be called
252- expect ( callback1 ) . not . toHaveBeenCalled ( ) ;
253- expect ( callback2 ) . not . toHaveBeenCalled ( ) ;
254- } ) ;
255- } ) ;
256235} ) ;
You can’t perform that action at this time.
0 commit comments