Skip to content

Commit 768bcaf

Browse files
committed
Successfully connects to devtools even if devtools are started after connecting the debugger.
Only displays apollo tab if apollo is added inside the project.
1 parent 8a22e49 commit 768bcaf

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"__PLATFORM__",
2525
"__REPORT_REACT_DEVTOOLS_PORT__",
2626
"__FETCH_SUPPORT__",
27-
"__NETWORK_INSPECT__"
27+
"__NETWORK_INSPECT__",
28+
"__APOLLO_DEVTOOLS_SHOULD_DISPLAY_PANEL__"
2829
]
2930
}
3031
],

app/middlewares/debuggerAPI.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ const workerOnMessage = message => {
3434
const { data } = message;
3535

3636
if (data.source === 'apollo-devtools-backend') {
37+
if (!window.__APOLLO_DEVTOOLS_SHOULD_DISPLAY_PANEL__) {
38+
window.__APOLLO_DEVTOOLS_SHOULD_DISPLAY_PANEL__ = true;
39+
}
40+
3741
postMessage({
3842
source: 'apollo-devtools-backend',
3943
payload: data,
40-
}, "*");
44+
}, '*');
4145
}
4246

4347
if (data && (data.__IS_REDUX_NATIVE_MESSAGE__ || data.__REPORT_REACT_DEVTOOLS_PORT__)) {
@@ -53,9 +57,10 @@ const workerOnMessage = message => {
5357

5458
const onWindowMessage = e => {
5559
if (e.data && e.data.source === 'apollo-devtools-proxy') {
56-
worker.postMessage({source: 'apollo-devtools-proxy', event: e.data.payload.event, payload: e.data.payload.payload});
60+
const message = typeof e.data.payload === 'string' ? { event: e.data.payload } : e.data.payload;
61+
worker.postMessage({ source: 'apollo-devtools-proxy', ...message });
5762
}
58-
}
63+
};
5964

6065
const createJSRuntime = () => {
6166
// This worker will run the application javascript code,

app/worker/apollo/backend/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
// This is the agent that is injected into the page that an Apollo Client app lives in
22
// when the Apollo Devtools panel is activated.
3-
import { initBroadCastEvents } from "./broadcastQueries";
4-
import { initLinkEvents } from "./links";
5-
import { checkVersions } from "./checkVersions";
3+
import { initBroadCastEvents } from './broadcastQueries';
4+
import { initLinkEvents } from './links';
5+
import { checkVersions } from './checkVersions';
66

77
// hook should have been injected before this executes.
88
let hook;
99
let bridge;
1010
let connected;
1111

12+
export const sendBridgeReady = () => {
13+
bridge.send('ready', hook.ApolloClient.version);
14+
};
15+
1216
const connect = () => {
1317
if (connected) return;
1418
connected = true;
1519
if (Number(hook.ApolloClient.version[0]) !== 1) {
1620
initLinkEvents(hook, bridge);
1721
initBroadCastEvents(hook, bridge);
1822
}
19-
bridge.log("backend ready.");
20-
bridge.send("ready", hook.ApolloClient.version);
23+
bridge.log('backend ready.');
24+
sendBridgeReady();
2125
checkVersions(hook, bridge);
2226
};
2327

24-
export const initBackend = (b, h) => {
28+
export const initBackend = (b, h, uuid) => {
2529
bridge = b;
2630
hook = h;
27-
connect();
31+
connect(uuid);
2832
};

app/worker/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as RemoteDev from './remotedev';
1818
import { getRequiredModules, ignoreRNDIntervalSpy } from './utils';
1919
import { toggleNetworkInspect } from './networkInspect';
2020
import Bridge from './apollo/bridge';
21-
import { initBackend } from './apollo/backend';
21+
import { initBackend, sendBridgeReady } from './apollo/backend';
2222

2323
/* eslint-disable no-underscore-dangle */
2424
self.__REMOTEDEV__ = RemoteDev;
@@ -59,29 +59,31 @@ const setupRNDebugger = async message => {
5959
clearInterval(interval);
6060

6161
const hook = {
62-
ApolloClient: self.__APOLLO_CLIENT__
62+
ApolloClient: self.__APOLLO_CLIENT__,
6363
};
6464

6565
const bridge = new Bridge({
6666
listen(fn) {
67-
self.addEventListener("message", evt =>
68-
{
69-
if (evt.data.source === "apollo-devtools-proxy" && evt.data.payload) {
67+
self.addEventListener('message', evt => {
68+
if (evt.data.source === 'apollo-devtools-proxy') {
7069
return fn(evt.data);
7170
}
7271
});
7372
},
7473
send(data) {
7574
postMessage({
7675
...data,
77-
source: 'apollo-devtools-backend'
76+
source: 'apollo-devtools-backend',
7877
});
7978
},
8079
});
8180

81+
bridge.on('init', () => {
82+
sendBridgeReady();
83+
});
84+
8285
initBackend(bridge, hook);
8386
}
84-
8587
}, 1000);
8688
};
8789

0 commit comments

Comments
 (0)