Skip to content

Commit b86b7c7

Browse files
🐛 [devext] synchronize network rules on extension start (DataDog#3560)
1 parent 61824b8 commit b86b7c7

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

developer-extension/src/panel/backgroundScriptConnection.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ export const onBackgroundDisconnection = new EventListeners<void>()
1010

1111
let backgroundScriptConnection: chrome.runtime.Port | undefined
1212

13+
// Buffer messages while the background script is not connected
14+
const backgroundScriptMessageBuffer: DevtoolsToBackgroundMessage[] = []
15+
1316
export function connectToBackgroundScript() {
1417
try {
1518
backgroundScriptConnection = chrome.runtime.connect({
1619
name: `devtools-panel-for-tab-${chrome.devtools.inspectedWindow.tabId}`,
1720
})
1821

1922
backgroundScriptConnection.onDisconnect.addListener(() => {
23+
backgroundScriptConnection = undefined
2024
// The background script can be disconnected for (at least) two main reasons:
2125
// * the extension is updated and its context is invalidated
2226
// * the background script has been idle for too long
@@ -35,7 +39,12 @@ export function connectToBackgroundScript() {
3539
backgroundScriptConnection.onMessage.addListener((backgroundMessage) =>
3640
onBackgroundMessage.notify(backgroundMessage)
3741
)
42+
43+
for (const message of backgroundScriptMessageBuffer.splice(0)) {
44+
backgroundScriptConnection.postMessage(message)
45+
}
3846
} catch (error) {
47+
backgroundScriptConnection = undefined
3948
if (isDisconnectError(error)) {
4049
onBackgroundDisconnection.notify()
4150
} else {
@@ -47,5 +56,7 @@ export function connectToBackgroundScript() {
4756
export function sendMessageToBackground(message: DevtoolsToBackgroundMessage) {
4857
if (backgroundScriptConnection) {
4958
backgroundScriptConnection.postMessage(message)
59+
} else {
60+
backgroundScriptMessageBuffer.push(message)
5061
}
5162
}

0 commit comments

Comments
 (0)