Skip to content

Commit 7267351

Browse files
authored
Modify legacy registration mechanism (#1375)
1 parent f6f500e commit 7267351

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

.changeset/young-brooms-protect.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+
Use `Object.defineProperty` to register legacy clients to avoid the need to search for the client in a loop in initialization.

src/extension/tab/hook.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
getMutations,
2020
getMainDefinition,
2121
} from "./helpers";
22-
import type { QueryResult } from "../../types";
22+
import type { QueryResult, SafeAny } from "../../types";
2323
import { getPrivateAccess } from "../../privateAccess";
2424
import type { JSONObject } from "../../application/types/json";
2525
import { createWindowActor } from "../actor";
@@ -91,12 +91,6 @@ window.onbeforeunload = () => {
9191
tab.send({ type: "disconnectFromDevtools" });
9292
};
9393

94-
window.addEventListener("load", () => {
95-
if (hook.ApolloClient) {
96-
sendHookDataToDevTools("connectToDevtools");
97-
}
98-
});
99-
10094
function getClientData() {
10195
// We need to JSON stringify the data here in case the cache contains
10296
// references to irregular data such as `URL` instances which are not
@@ -301,4 +295,26 @@ if (Array.isArray(preExisting)) {
301295
(preExisting as Array<ApolloClient<any>>).forEach(registerClient);
302296
}
303297

304-
findClient();
298+
// Handles registering legacy clients (< v3.7) which do not use the new
299+
// registration mechanism above.
300+
let globalClient = window.__APOLLO_CLIENT__;
301+
Object.defineProperty(window, "__APOLLO_CLIENT__", {
302+
get() {
303+
return globalClient;
304+
},
305+
set(client: ApolloClient<SafeAny> | undefined) {
306+
if (client) {
307+
// We call this in a setTimeout because the client is not fully
308+
// instantiated before the property on window is assigned since it
309+
// connects from the constructor of ApolloClient. This allows
310+
// initialization to finish before we register it.
311+
setTimeout(() => registerClient(client));
312+
}
313+
314+
globalClient = client;
315+
},
316+
});
317+
318+
if (globalClient) {
319+
registerClient(globalClient);
320+
}

0 commit comments

Comments
 (0)