Skip to content

Commit 1cff81b

Browse files
authored
Merge pull request #298 from Gongreg/apollo
Adding apollo dev tools.
2 parents 46a13ca + 57169aa commit 1cff81b

File tree

10 files changed

+1162
-5
lines changed

10 files changed

+1162
-5
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
"__PLATFORM__",
2525
"__REPORT_REACT_DEVTOOLS_PORT__",
2626
"__FETCH_SUPPORT__",
27-
"__NETWORK_INSPECT__"
27+
"__NETWORK_INSPECT__",
28+
"__APOLLO_CLIENT__",
29+
"__APOLLO_DEVTOOLS_SHOULD_DISPLAY_PANEL__"
2830
]
2931
}
3032
],

app/middlewares/debuggerAPI.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,23 @@ let host;
3030
let port;
3131
let socket;
3232

33+
const APOLLO_BACKEND = 'apollo-devtools-backend';
34+
const APOLLO_PROXY = 'apollo-devtools-proxy';
35+
3336
const workerOnMessage = message => {
3437
const { data } = message;
38+
39+
if (data && data.source === APOLLO_BACKEND) {
40+
if (!window.__APOLLO_DEVTOOLS_SHOULD_DISPLAY_PANEL__) {
41+
window.__APOLLO_DEVTOOLS_SHOULD_DISPLAY_PANEL__ = true;
42+
}
43+
44+
postMessage({
45+
source: APOLLO_BACKEND,
46+
payload: data,
47+
}, '*');
48+
}
49+
3550
if (data && (data.__IS_REDUX_NATIVE_MESSAGE__ || data.__REPORT_REACT_DEVTOOLS_PORT__)) {
3651
return true;
3752
}
@@ -43,14 +58,22 @@ const workerOnMessage = message => {
4358
socket.send(JSON.stringify(data));
4459
};
4560

61+
const onWindowMessage = e => {
62+
const { data } = e;
63+
if (data && data.source === APOLLO_PROXY) {
64+
const message = typeof data.payload === 'string' ? { event: data.payload } : data.payload;
65+
worker.postMessage({ source: APOLLO_PROXY, ...message });
66+
}
67+
};
68+
4669
const createJSRuntime = () => {
4770
// This worker will run the application javascript code,
4871
// making sure that it's run in an environment without a global
4972
// document, to make it consistent with the JSC executor environment.
5073
// eslint-disable-next-line
5174
worker = new Worker(`${__webpack_public_path__}RNDebuggerWorker.js`);
5275
worker.addEventListener('message', workerOnMessage);
53-
76+
window.addEventListener('message', onWindowMessage);
5477
actions.setDebuggerWorker(worker, 'connected');
5578
};
5679

@@ -59,6 +82,7 @@ const shutdownJSRuntime = () => {
5982
scriptExecuted = false;
6083
if (worker) {
6184
worker.terminate();
85+
window.removeEventListener('messsage', onWindowMessage);
6286
setDevMenuMethods([]);
6387
}
6488
worker = null;

app/worker/apollo.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Bridge from 'apollo-client-devtools/src/bridge';
2+
import { initBackend, sendBridgeReady } from 'apollo-client-devtools/src/backend';
3+
import { version as devToolsVersion } from 'apollo-client-devtools/package.json';
4+
import { getSafeAsyncStorage } from './asyncStorage';
5+
6+
export function handleApolloClient(modules) {
7+
const interval = setInterval(() => {
8+
if (!self.__APOLLO_CLIENT__) {
9+
return;
10+
}
11+
12+
clearInterval(interval);
13+
14+
const hook = {
15+
ApolloClient: self.__APOLLO_CLIENT__,
16+
devToolsVersion,
17+
};
18+
19+
let listener;
20+
21+
const bridge = new Bridge({
22+
listen(fn) {
23+
listener = self.addEventListener('message', evt => {
24+
if (evt.data.source === 'apollo-devtools-proxy') {
25+
return fn(evt.data);
26+
}
27+
});
28+
},
29+
send(data) {
30+
postMessage({
31+
...data,
32+
source: 'apollo-devtools-backend',
33+
});
34+
},
35+
});
36+
37+
bridge.on('init', () => {
38+
sendBridgeReady();
39+
});
40+
41+
bridge.on('shutdown', () => {
42+
self.removeEventListener('message', listener);
43+
});
44+
45+
initBackend(bridge, hook, getSafeAsyncStorage(modules.AsyncStorage));
46+
}, 1000);
47+
}

app/worker/asyncStorage.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ export const getClearAsyncStorageFn = AsyncStorage => {
33
return () => AsyncStorage.clear().catch(f => f);
44
};
55

6+
export const getSafeAsyncStorage = AsyncStorage => ({
7+
async getItem(key) {
8+
try {
9+
return AsyncStorage.getItem(key);
10+
} catch (e) {
11+
return null;
12+
}
13+
},
14+
async setItem(key, value) {
15+
try {
16+
return AsyncStorage.setItem(key, value);
17+
} catch (e) {
18+
return null;
19+
}
20+
},
21+
});
22+
623
export const getShowAsyncStorageFn = AsyncStorage => {
724
if (!AsyncStorage.getAllKeys || !AsyncStorage.getItem) return;
825
return async () => {

app/worker/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import devToolsEnhancer, { composeWithDevTools } from './reduxAPI';
1717
import * as RemoteDev from './remotedev';
1818
import { getRequiredModules, ignoreRNDIntervalSpy } from './utils';
1919
import { toggleNetworkInspect } from './networkInspect';
20+
import { handleApolloClient } from './apollo';
2021

2122
/* eslint-disable no-underscore-dangle */
2223
self.__REMOTEDEV__ = RemoteDev;
@@ -51,6 +52,8 @@ const setupRNDebugger = async message => {
5152
checkAvailableDevMenuMethods(modules, message.networkInspect);
5253
reportDefaultReactDevToolsPort(modules);
5354
}
55+
56+
handleApolloClient(modules);
5457
};
5558

5659
const messageHandlers = {
@@ -66,6 +69,7 @@ const messageHandlers = {
6669
} catch (err) {
6770
error = err.message;
6871
}
72+
6973
sendReply(null /* result */, error);
7074

7175
if (!error) {

dist/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"license": "MIT",
1313
"dependencies": {
1414
"adbkit": "^2.11.0",
15+
"apollo-client-devtools": "2.1.7-alpha.2",
1516
"electron-store": "^1.2.0",
1617
"react-devtools-core": "^3.4.3"
1718
},

0 commit comments

Comments
 (0)