Skip to content

Commit 8369170

Browse files
hawkgsAndrewKushnir
authored andcommitted
refactor(devtools): slightly optimize extension initialization messaging
- Stop indefinite `detectAngular` messages after the backend is installed. - Do not attempt handshake with the BE (from content scripts) until it's installed.
1 parent bb65520 commit 8369170

3 files changed

Lines changed: 33 additions & 23 deletions

File tree

devtools/projects/protocol/src/lib/messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ export interface Events {
447447
enableFrameConnection: (frameId: number, tabId: number) => void;
448448
frameConnected: (frameId: number) => void;
449449
detectAngular: (detectionResult: AngularDetection) => void;
450+
backendInstalled: () => void;
450451
backendReady: () => void;
451452

452453
log: (logEvent: {message: string; level: 'log' | 'warn' | 'debug' | 'error'}) => void;

devtools/projects/shell-browser/src/app/content-script.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,35 @@ import {BACKEND_URI, CONTENT_SCRIPT_URI, DETECT_ANGULAR_SCRIPT_URI} from './comm
1313
import {SamePageMessageBus} from './same-page-message-bus';
1414

1515
let backgroundDisconnected = false;
16-
let backendInstalled = false;
1716
let backendInitialized = false;
1817

1918
const port = chrome.runtime.connect({
2019
name: `${document.title || location.href}`,
2120
});
2221

2322
const 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+
3145
port.onDisconnect.addListener(handleDisconnect);
3246

3347
const detectAngularMessageBus = new SamePageMessageBus(
@@ -36,11 +50,6 @@ const detectAngularMessageBus = new SamePageMessageBus(
3650
);
3751

3852
detectAngularMessageBus.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

6779
const 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
7487
chromeMessageBus.onAny((topic, args) => {
7588
localMessageBus.emit(topic, args);
7689
});
7790

91+
// Relaying messages from BE to FE
7892
localMessageBus.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

97100
const proxyEventFromWindowToDevToolsExtension = (event: MessageEvent) => {
98101
if (event.source === window && event.data && event.data.__NG_DEVTOOLS_EVENT__) {

devtools/projects/shell-browser/src/app/detect-angular-for-extension-icon.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const detectAngularMessageBus = new SamePageMessageBus(
2222
CONTENT_SCRIPT_URI,
2323
);
2424

25+
let detectAngularTimeout: ReturnType<typeof setTimeout>;
26+
2527
function detectAngular(win: Window): void {
2628
const isAngular = appIsAngular();
2729
const isSupportedAngularVersion = appIsSupportedAngularVersion();
@@ -50,7 +52,11 @@ function detectAngular(win: Window): void {
5052
},
5153
]);
5254

53-
setTimeout(() => detectAngular(win), 1000);
55+
detectAngularTimeout = setTimeout(() => detectAngular(win), 1000);
5456
}
5557

58+
detectAngularMessageBus.on('backendInstalled', () => {
59+
clearTimeout(detectAngularTimeout);
60+
});
61+
5662
detectAngular(window);

0 commit comments

Comments
 (0)