Skip to content

Commit c9764ea

Browse files
authored
Clean up some redundant logic for connecting to the client (#1392)
1 parent 59a4500 commit c9764ea

File tree

5 files changed

+20
-70
lines changed

5 files changed

+20
-70
lines changed

.changeset/polite-brooms-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"apollo-client-devtools": patch
3+
---
4+
5+
Remove some redundant logic for connecting to the client instance from the devtools.

src/extension/devtools/devtools.ts

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ const inspectedTabId = browser.devtools.inspectedWindow.tabId;
1717
const devtoolsMachine = interpret(
1818
createDevtoolsMachine({
1919
actions: {
20-
connectToClient: () => {
21-
clientPort.send({ type: "connectToClient" });
22-
startConnectTimeout();
23-
},
20+
connectToClient,
2421
startRequestInterval: () => {
2522
clearTimeout(connectTimeoutId);
2623

@@ -40,7 +37,6 @@ const devtoolsMachine = interpret(
4037

4138
let panelHidden = true;
4239
let connectTimeoutId: NodeJS.Timeout;
43-
let disconnectTimeoutId: NodeJS.Timeout;
4440

4541
const portAdapter = createPortMessageAdapter(() =>
4642
browser.runtime.connect({ name: inspectedTabId.toString() })
@@ -51,24 +47,21 @@ const rpcClient = createRpcClient<DevtoolsRPCMessage>(portAdapter);
5147

5248
devtoolsMachine.subscribe(({ value }) => {
5349
if (value === "connected") {
54-
clearTimeout(disconnectTimeoutId);
50+
clearTimeout(connectTimeoutId);
5551
}
5652
});
5753

58-
// In case we can't connect to the tab, we should at least show something to the
59-
// user when we've attempted to connect a max number of times.
60-
function startConnectTimeout(attempts = 0) {
54+
function connectToClient() {
55+
clientPort.send({ type: "connectToClient" });
56+
startConnectTimeout();
57+
}
58+
59+
function startConnectTimeout() {
60+
clearTimeout(connectTimeoutId);
61+
6162
connectTimeoutId = setTimeout(() => {
62-
if (attempts < 3) {
63-
clientPort.send({ type: "connectToClient" });
64-
startConnectTimeout(attempts + 1);
65-
} else {
66-
devtoolsMachine.send("timeout");
67-
}
68-
// Pick a threshold above the time it takes to determine if the client is
69-
// found on the page. This ensures we don't reset that counter and provide a
70-
// proper "not found" message.
71-
}, 11_000);
63+
devtoolsMachine.send("clientNotFound");
64+
}, 10_000);
7265
}
7366

7467
clientPort.on("connectToDevtools", (message) => {
@@ -83,20 +76,11 @@ clientPort.on("registerClient", (message) => {
8376
});
8477

8578
clientPort.on("disconnectFromDevtools", () => {
86-
clearTimeout(disconnectTimeoutId);
8779
devtoolsMachine.send("disconnect");
88-
89-
disconnectTimeoutId = setTimeout(() => {
90-
devtoolsMachine.send("clientNotFound");
91-
}, 10_000);
92-
});
93-
94-
clientPort.on("clientNotFound", () => {
95-
clearTimeout(connectTimeoutId);
96-
devtoolsMachine.send("clientNotFound");
80+
startConnectTimeout();
9781
});
9882

99-
clientPort.send({ type: "connectToClient" });
83+
connectToClient();
10084

10185
function startRequestInterval(ms = 500) {
10286
let id: NodeJS.Timeout;
@@ -163,11 +147,6 @@ async function createDevtoolsPanel() {
163147
connectedToPanel = true;
164148
}
165149

166-
if (devtoolsMachine.state.value === "initialized") {
167-
clientPort.send({ type: "connectToClient" });
168-
startConnectTimeout();
169-
}
170-
171150
if (devtoolsMachine.state.value === "connected" && panelHidden) {
172151
unsubscribers.add(startRequestInterval());
173152
}

src/extension/messages.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ type ExplorerSubscriptionTerminationMessage = {
8383

8484
export type ClientMessage =
8585
| { type: "registerClient"; payload: ClientContext }
86-
| { type: "clientNotFound" }
8786
| { type: "connectToClient" }
8887
| {
8988
type: "connectToDevtools";

src/extension/tab/hook.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,9 @@ function getClientData() {
117117

118118
handleRpc("getClientOperations", getClientData);
119119

120-
function sendHookDataToDevTools(eventName: "connectToDevtools") {
121-
tab.send({
122-
type: eventName,
123-
payload: getClientData(),
124-
});
125-
}
126-
127120
tab.on("connectToClient", () => {
128121
if (hook.ApolloClient) {
129-
sendHookDataToDevTools("connectToDevtools");
130-
} else {
131-
findClient();
122+
tab.send({ type: "connectToDevtools", payload: getClientData() });
132123
}
133124
});
134125

@@ -223,28 +214,6 @@ tab.on("explorerRequest", (message) => {
223214
}
224215
});
225216

226-
/**
227-
* Attempt to find the client on a 1-second interval for 10 seconds max
228-
*/
229-
let interval: NodeJS.Timeout;
230-
function findClient() {
231-
let count = 0;
232-
233-
function initializeDevtoolsHook() {
234-
if (count++ > 10) {
235-
clearInterval(interval);
236-
tab.send({ type: "clientNotFound" });
237-
}
238-
if (window.__APOLLO_CLIENT__) {
239-
registerClient(window.__APOLLO_CLIENT__);
240-
}
241-
}
242-
243-
clearInterval(interval);
244-
interval = setInterval(initializeDevtoolsHook, 1000);
245-
initializeDevtoolsHook(); // call immediately to reduce lag if devtools are already available
246-
}
247-
248217
function watchForClientTermination(client: ApolloClient<any>) {
249218
const originalStop = client.stop;
250219

@@ -283,7 +252,6 @@ function registerClient(client: ApolloClient<any>) {
283252
// }
284253
// });
285254

286-
clearInterval(interval);
287255
loadErrorCodes(rpcClient, client.version);
288256
}
289257

src/extension/tab/tab.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ devtools.forward("explorerSubscriptionTermination", tab);
2424
devtools.forward("explorerRequest", tab);
2525

2626
tab.forward("registerClient", devtools);
27-
tab.forward("clientNotFound", devtools);
2827
tab.forward("connectToDevtools", devtools);
2928
tab.forward("disconnectFromDevtools", devtools);
3029
tab.forward("explorerResponse", devtools);

0 commit comments

Comments
 (0)