Fixed bad Bridge disconnections
Brian Vaughn committed
May 30, 2019 at 09:27 UTC
a957031fe799938afc3e498e8644be5b3a22bc99
2 files changed
+14
-12
shells/browser/shared/src/main.js
+8
-1
@@ -62,9 +62,11 @@ function createPanelIfReactLoaded() {
62
let render = null;
63
let root = null;
64
65
+ const tabId = chrome.devtools.inspectedWindow.tabId;
66
+
67
function initBridgeAndStore() {
68
const port = chrome.runtime.connect({
67
- name: '' + chrome.devtools.inspectedWindow.tabId,
69
+ name: '' + tabId,
70
});
71
// Looks like `port.onDisconnect` does not trigger on in-tab navigation like new URL or back/forward navigation,
72
// so it makes no sense to handle it here.
@@ -284,6 +286,11 @@ function createPanelIfReactLoaded() {
286
// Shutdown bridge before a new page is loaded.
287
chrome.webNavigation.onBeforeNavigate.addListener(
288
function onBeforeNavigate(details) {
289
+ // Ignore navigation events from other tabs (or from within frames).
290
+ if (details.tabId !== tabId || details.frameId !== 0) {
291
+ return;
292
+ }
293
+
294
// `bridge.shutdown()` will remove all listeners we added, so we don't have to.
295
bridge.shutdown();
296
src/bridge.js
+6
-11
@@ -57,6 +57,12 @@ export default class Bridge extends EventEmitter {
57
return;
58
}
59
60
+ // Queue the shutdown outgoing message for subscribers.
61
+ this.send('shutdown');
62
+
63
+ // Mark this bridge as destroyed, i.e. disable its public API.
64
+ this._isShutdown = true;
65
+
66
// Disable the API inherited from EventEmitter that can add more listeners and send more messages.
67
this.addListener = function() {};
68
this.emit = function() {};
@@ -71,9 +77,6 @@ export default class Bridge extends EventEmitter {
77
wallUnlisten();
78
}
79
74
- // Queue the shutdown outgoing message for subscribers.
75
- this.send('shutdown');
76
-
80
// Synchronously flush all queued outgoing messages.
81
// At this step the subscribers' code may run in this call stack.
82
do {
@@ -83,17 +86,9 @@ export default class Bridge extends EventEmitter {
86
// Make sure once again that there is no dangling timer.
87
clearTimeout(this._timeoutID);
88
this._timeoutID = null;
86
-
87
- // Mark this bridge as destroyed, i.e. disable its public API.
88
- this._isShutdown = true;
89
}
90
91
_flush = () => {
92
- if (this._isShutdown) {
93
- console.warn(`Cannot flush a Bridge that has been shutdown.`);
94
- return;
95
- }
96
-
92
// This method is used after the bridge is marked as destroyed in shutdown sequence,
93
// so we do not bail out if the bridge marked as destroyed.
94
// It is a private method that the bridge ensures is only called at the right times.