@@ -22,6 +22,7 @@ import {
22
22
setBrowserSelectionFromReact ,
23
23
setReactSelectionFromBrowser ,
24
24
} from './elementSelection' ;
25
+ import { startReactPolling } from './reactPolling' ;
25
26
import cloneStyleTags from './cloneStyleTags' ;
26
27
import injectBackendManager from './injectBackendManager' ;
27
28
import syncSavedPreferences from './syncSavedPreferences' ;
@@ -30,60 +31,6 @@ import getProfilingFlags from './getProfilingFlags';
30
31
import debounce from './debounce' ;
31
32
import './requestAnimationFramePolyfill' ;
32
33
33
- // Try polling for at least 5 seconds, in case if it takes too long to load react
34
- const REACT_POLLING_TICK_COOLDOWN = 250 ;
35
- const REACT_POLLING_ATTEMPTS_THRESHOLD = 20 ;
36
-
37
- let reactPollingTimeoutId = null ;
38
- export function clearReactPollingTimeout ( ) {
39
- clearTimeout ( reactPollingTimeoutId ) ;
40
- reactPollingTimeoutId = null ;
41
- }
42
-
43
- export function executeIfReactHasLoaded ( callback , attempt = 1 ) {
44
- clearReactPollingTimeout ( ) ;
45
-
46
- if ( attempt > REACT_POLLING_ATTEMPTS_THRESHOLD ) {
47
- return ;
48
- }
49
-
50
- chrome . devtools . inspectedWindow . eval (
51
- 'window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.size > 0' ,
52
- ( pageHasReact , exceptionInfo ) => {
53
- if ( exceptionInfo ) {
54
- const { code, description, isError, isException, value} = exceptionInfo ;
55
-
56
- if ( isException ) {
57
- console . error (
58
- `Received error while checking if react has loaded: ${ value } ` ,
59
- ) ;
60
- return ;
61
- }
62
-
63
- if ( isError ) {
64
- console . error (
65
- `Received error with code ${ code } while checking if react has loaded: ${ description } ` ,
66
- ) ;
67
- return ;
68
- }
69
- }
70
-
71
- if ( pageHasReact ) {
72
- callback ( ) ;
73
- } else {
74
- reactPollingTimeoutId = setTimeout (
75
- executeIfReactHasLoaded ,
76
- REACT_POLLING_TICK_COOLDOWN ,
77
- callback ,
78
- attempt + 1 ,
79
- ) ;
80
- }
81
- } ,
82
- ) ;
83
- }
84
-
85
- let lastSubscribedBridgeListener = null ;
86
-
87
34
function createBridge ( ) {
88
35
bridge = new Bridge ( {
89
36
listen ( fn ) {
@@ -370,6 +317,7 @@ function ensureInitialHTMLIsCleared(container) {
370
317
function createComponentsPanel ( ) {
371
318
if ( componentsPortalContainer ) {
372
319
// Panel is created and user opened it at least once
320
+ ensureInitialHTMLIsCleared ( componentsPortalContainer ) ;
373
321
render ( 'components' ) ;
374
322
375
323
return ;
@@ -389,7 +337,7 @@ function createComponentsPanel() {
389
337
390
338
createdPanel . onShown . addListener ( portal => {
391
339
componentsPortalContainer = portal . container ;
392
- if ( componentsPortalContainer != null ) {
340
+ if ( componentsPortalContainer != null && render ) {
393
341
ensureInitialHTMLIsCleared ( componentsPortalContainer ) ;
394
342
395
343
render ( 'components' ) ;
@@ -408,6 +356,7 @@ function createComponentsPanel() {
408
356
function createProfilerPanel ( ) {
409
357
if ( profilerPortalContainer ) {
410
358
// Panel is created and user opened it at least once
359
+ ensureInitialHTMLIsCleared ( profilerPortalContainer ) ;
411
360
render ( 'profiler' ) ;
412
361
413
362
return ;
@@ -427,7 +376,7 @@ function createProfilerPanel() {
427
376
428
377
createdPanel . onShown . addListener ( portal => {
429
378
profilerPortalContainer = portal . container ;
430
- if ( profilerPortalContainer != null ) {
379
+ if ( profilerPortalContainer != null && render ) {
431
380
ensureInitialHTMLIsCleared ( profilerPortalContainer ) ;
432
381
433
382
render ( 'profiler' ) ;
@@ -442,7 +391,7 @@ function createProfilerPanel() {
442
391
443
392
function performInTabNavigationCleanup ( ) {
444
393
// Potentially, if react hasn't loaded yet and user performs in-tab navigation
445
- clearReactPollingTimeout ( ) ;
394
+ clearReactPollingInstance ( ) ;
446
395
447
396
if ( store !== null ) {
448
397
// Store profiling data, so it can be used later
@@ -479,7 +428,7 @@ function performInTabNavigationCleanup() {
479
428
480
429
function performFullCleanup ( ) {
481
430
// Potentially, if react hasn't loaded yet and user closed the browser DevTools
482
- clearReactPollingTimeout ( ) ;
431
+ clearReactPollingInstance ( ) ;
483
432
484
433
if ( ( componentsPortalContainer || profilerPortalContainer ) && root ) {
485
434
// This should also emit bridge.shutdown, but only if this root was mounted
@@ -531,6 +480,8 @@ function connectExtensionPort() {
531
480
}
532
481
533
482
function mountReactDevTools ( ) {
483
+ reactPollingInstance = null ;
484
+
534
485
registerEventsLogger ( ) ;
535
486
536
487
createBridgeAndStore ( ) ;
@@ -541,18 +492,36 @@ function mountReactDevTools() {
541
492
createProfilerPanel ( ) ;
542
493
}
543
494
544
- // TODO: display some disclaimer if user performs in-tab navigation to non-react application
545
- // when React DevTools panels are already opened, currently we will display just blank white block
546
- function mountReactDevToolsWhenReactHasLoaded ( ) {
547
- function onReactReady ( ) {
548
- clearReactPollingTimeout ( ) ;
549
- mountReactDevTools ( ) ;
495
+ let reactPollingInstance = null ;
496
+ function clearReactPollingInstance ( ) {
497
+ reactPollingInstance ?. abort ( ) ;
498
+ reactPollingInstance = null ;
499
+ }
500
+
501
+ function showNoReactDisclaimer ( ) {
502
+ if ( componentsPortalContainer ) {
503
+ componentsPortalContainer . innerHTML =
504
+ '<h1 class="no-react-disclaimer">Looks like this page doesn\'t have React, or it hasn\'t been loaded yet.</h1>' ;
505
+ delete componentsPortalContainer . _hasInitialHTMLBeenCleared ;
506
+ }
507
+
508
+ if ( profilerPortalContainer ) {
509
+ profilerPortalContainer . innerHTML =
510
+ '<h1 class="no-react-disclaimer">Looks like this page doesn\'t have React, or it hasn\'t been loaded yet.</h1>' ;
511
+ delete profilerPortalContainer . _hasInitialHTMLBeenCleared ;
550
512
}
513
+ }
551
514
552
- executeIfReactHasLoaded ( onReactReady , 1 ) ;
515
+ function mountReactDevToolsWhenReactHasLoaded ( ) {
516
+ reactPollingInstance = startReactPolling (
517
+ mountReactDevTools ,
518
+ 5 , // ~5 seconds
519
+ showNoReactDisclaimer ,
520
+ ) ;
553
521
}
554
522
555
523
let bridge = null ;
524
+ let lastSubscribedBridgeListener = null ;
556
525
let store = null ;
557
526
558
527
let profilingData = null ;
0 commit comments