Files
angular__angular/devtools/projects/shell-browser/src/app/backend.ts
T
Matthieu Riegler 263b819a75 refactor(devtools): prevent spamming the message bus
This commit adds some state on the bus to prevent sending unecessary messages to the main window.
2026-03-09 10:45:58 -07:00

55 lines
1.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {initializeMessageBus} from '../../../ng-devtools-backend';
import {unHighlight} from '../../../ng-devtools-backend/src/lib/highlighter';
import {initializeExtendedWindowOperations} from './chrome-window-extensions';
import {getBackendUri, getContentScriptUri} from './comm-utils';
import {SamePageMessageBus} from './same-page-message-bus';
const messageBus = new SamePageMessageBus(
'[Backend=>ContentScript]',
getBackendUri(),
getContentScriptUri(),
);
let initialized = false;
messageBus.on('handshake', () => {
if (initialized) {
return;
}
initialized = true;
initializeMessageBus(messageBus);
initializeExtendedWindowOperations();
let inspectorRunning = false;
messageBus.on('inspectorStart', () => {
inspectorRunning = true;
});
messageBus.on('inspectorEnd', () => {
inspectorRunning = false;
});
// handles case when mouse leaves chrome extension too quickly. unHighlight() is not a very
// expensive function and has an if check so it's DOM api call is not called more than necessary
document.addEventListener(
'mousemove',
() => {
if (!inspectorRunning) {
unHighlight();
}
},
false,
);
messageBus.emit('backendReady');
});