@@ -13,21 +13,35 @@ import {BACKEND_URI, CONTENT_SCRIPT_URI, DETECT_ANGULAR_SCRIPT_URI} from './comm
1313import { SamePageMessageBus } from './same-page-message-bus' ;
1414
1515let backgroundDisconnected = false ;
16- let backendInstalled = false ;
1716let backendInitialized = false ;
1817
1918const port = chrome . runtime . connect ( {
2019 name : `${ document . title || location . href } ` ,
2120} ) ;
2221
2322const handleDisconnect = ( ) : void => {
24- // console.log('Background disconnected', new Date());
2523 localMessageBus . emit ( 'shutdown' ) ;
2624 localMessageBus . destroy ( ) ;
2725 chromeMessageBus . destroy ( ) ;
2826 backgroundDisconnected = true ;
2927} ;
3028
29+ function attemptBackendHandshake ( ) {
30+ if ( ! backendInitialized ) {
31+ // tslint:disable-next-line:no-console
32+ console . log ( 'Attempting handshake with backend' , new Date ( ) ) ;
33+
34+ const retry = ( ) => {
35+ if ( backendInitialized || backgroundDisconnected ) {
36+ return ;
37+ }
38+ handshakeWithBackend ( ) ;
39+ setTimeout ( retry , 500 ) ;
40+ } ;
41+ retry ( ) ;
42+ }
43+ }
44+
3145port . onDisconnect . addListener ( handleDisconnect ) ;
3246
3347const detectAngularMessageBus = new SamePageMessageBus (
@@ -36,11 +50,6 @@ const detectAngularMessageBus = new SamePageMessageBus(
3650) ;
3751
3852detectAngularMessageBus . on ( 'detectAngular' , ( detectionResult ) => {
39- // only install backend once
40- if ( backendInstalled ) {
41- return ;
42- }
43-
4453 if ( detectionResult . isAngularDevTools !== true ) {
4554 return ;
4655 }
@@ -61,7 +70,10 @@ detectAngularMessageBus.on('detectAngular', (detectionResult) => {
6170 script . src = chrome . runtime . getURL ( 'app/backend_bundle.js' ) ;
6271 document . documentElement . appendChild ( script ) ;
6372 document . documentElement . removeChild ( script ) ;
64- backendInstalled = true ;
73+
74+ detectAngularMessageBus . emit ( 'backendInstalled' ) ;
75+
76+ attemptBackendHandshake ( ) ;
6577} ) ;
6678
6779const localMessageBus = new SamePageMessageBus ( CONTENT_SCRIPT_URI , BACKEND_URI ) ;
@@ -71,28 +83,19 @@ const handshakeWithBackend = (): void => {
7183 localMessageBus . emit ( 'handshake' ) ;
7284} ;
7385
86+ // Relaying messages from FE to BE
7487chromeMessageBus . onAny ( ( topic , args ) => {
7588 localMessageBus . emit ( topic , args ) ;
7689} ) ;
7790
91+ // Relaying messages from BE to FE
7892localMessageBus . onAny ( ( topic , args ) => {
79- backendInitialized = true ;
8093 chromeMessageBus . emit ( topic , args ) ;
8194} ) ;
8295
83- if ( ! backendInitialized ) {
84- // tslint:disable-next-line:no-console
85- console . log ( 'Attempting initialization' , new Date ( ) ) ;
86-
87- const retry = ( ) => {
88- if ( backendInitialized || backgroundDisconnected ) {
89- return ;
90- }
91- handshakeWithBackend ( ) ;
92- setTimeout ( retry , 500 ) ;
93- } ;
94- retry ( ) ;
95- }
96+ localMessageBus . on ( 'backendReady' , ( ) => {
97+ backendInitialized = true ;
98+ } ) ;
9699
97100const proxyEventFromWindowToDevToolsExtension = ( event : MessageEvent ) => {
98101 if ( event . source === window && event . data && event . data . __NG_DEVTOOLS_EVENT__ ) {
0 commit comments