Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions packages/react-devtools-extensions/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ chrome.runtime.onConnect.addListener(function(port) {
ports[tab][name] = port;

if (ports[tab].devtools && ports[tab]['content-script']) {
doublePipe(ports[tab].devtools, ports[tab]['content-script']);
doublePipe(ports[tab].devtools, ports[tab]['content-script'], tab);
// clean up so that we can rebuild the double pipe if the page is reloaded
ports[tab] = null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be moved to shutdown? On line 176, ports[id] is accessed. (although I don't know what it does)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

}
});

Expand All @@ -70,14 +72,28 @@ function installProxy(tabId: number) {
}
}

function doublePipe(one, two) {
function doublePipe(one, two, tabId) {
one.onMessage.addListener(lOne);
function lOne(message) {
two.postMessage(message);
try {
two.postMessage(message);
} catch (e) {
if (__DEV__) {
console.log(`Broken pipe ${tabId}: `, e);
}
shutdown();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the postMessage fail because of a JS exception? I'm not sure if it will happen. I think if we shutdown here not during a page load, the devtools would stop working?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a JS exception. It's a service worker exception when postMessage is fired on a tab that's offloaded. The error message looks like this:
image

It's weird that the listener was not removed since we already have one.onDisconnect.addListener(shutdown). Maybe Chrome doesn't treat reload as disconnect anymore?

}
}
two.onMessage.addListener(lTwo);
function lTwo(message) {
one.postMessage(message);
try {
one.postMessage(message);
} catch (e) {
if (__DEV__) {
console.log(`Broken pipe ${tabId}: `, e);
}
shutdown();
}
}
function shutdown() {
one.onMessage.removeListener(lOne);
Expand Down