Skip to content

Commit 60a18c1

Browse files
Brian Vaughnfacebook-github-bot
Brian Vaughn
authored andcommitted
Don't disconnect DevTools WebSocket connection on Cmd+D
Summary: When the React Native Cmd+D menu is opened, something re-runs module initialization code (including DevTools backend initialization) which recreates the `WebSocket` and kills any already-connected frontend. It's not clear to me why this is done. (Intentional? Accident?) But it makes it difficult to connect the React Native Inspector and DevTools together without multiple reloads. This Diff prevents the Cmd+D menu from killing the `WebSocket` connection as a workaround. A better long-term fix would (probably) be to not eagerly re-run these modules. ## Changelog [General] [Fixed] - Don't disconnect DevTools WebSocket connection on Cmd+D Reviewed By: fkgozali, sammy-SC Differential Revision: D27742376 fbshipit-source-id: 60ab3e4763da6b055c28c7aafc6d460e7f4a601d
1 parent 73b9f78 commit 60a18c1

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Libraries/Core/setUpReactDevTools.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@
1111
'use strict';
1212

1313
if (__DEV__) {
14+
let isWebSocketOpen = false;
15+
let ws = null;
16+
1417
const reactDevTools = require('react-devtools-core');
1518
const connectToDevTools = () => {
19+
if (ws !== null && isWebSocketOpen) {
20+
// If the DevTools backend is already connected, don't recreate the WebSocket.
21+
// This would break the connection.
22+
// If there isn't an active connection, a backend may be waiting to connect,
23+
// in which case it's okay to make a new one.
24+
return;
25+
}
26+
1627
// not when debugging in chrome
1728
// TODO(t12832058) This check is broken
1829
if (!window.document) {
@@ -39,7 +50,13 @@ if (__DEV__) {
3950
: 8097;
4051

4152
const WebSocket = require('../WebSocket/WebSocket');
42-
const ws = new WebSocket('ws://' + host + ':' + port);
53+
ws = new WebSocket('ws://' + host + ':' + port);
54+
ws.addEventListener('close', event => {
55+
isWebSocketOpen = false;
56+
});
57+
ws.addEventListener('open', event => {
58+
isWebSocketOpen = true;
59+
});
4360

4461
const viewConfig = require('../Components/View/ReactNativeViewViewConfig');
4562
reactDevTools.connectToDevTools({

0 commit comments

Comments
 (0)