Skip to content

Commit

Permalink
Record connectors requests until devtools is connected
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Oct 22, 2024
1 parent 6325fa9 commit 502b753
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
34 changes: 23 additions & 11 deletions src/application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { RouterProvider } from "react-router-dom";
import { router } from "./router";
import { getPanelActor } from "../extension/devtools/panelActor";
import { connectorsRequestsVar } from "./vars";
import type { ConnectorsDebuggingResultPayload } from "../types";

loadDevMessages();
loadErrorMessages();
Expand Down Expand Up @@ -99,6 +100,13 @@ export const AppProvider = () => {
export const initDevTools = () => {
const root = createRoot(document.getElementById("devtools") as HTMLElement);

rpcClient
.withTimeout(3000)
.request("getConnectorsRequests")
.then((results) => {
connectorsRequestsVar(results.map(assignConnectorsIds));
});

root.render(<AppProvider />);
};

Expand All @@ -108,19 +116,23 @@ const actor = getPanelActor(window);
actor.on("connectorsDebuggingResult", ({ payload }) => {
connectorsRequestsVar([
...connectorsRequestsVar(),
{
...payload,
id: ++nextPayloadId,
debuggingResult: {
...payload.debuggingResult,
data: payload.debuggingResult.data.map((request, idx) => ({
...request,
id: idx + 1,
})),
},
},
assignConnectorsIds(payload),
]);
});

actor.on("pageNavigated", () => connectorsRequestsVar([]));
actor.on("clientTerminated", () => connectorsRequestsVar([]));

function assignConnectorsIds(result: ConnectorsDebuggingResultPayload) {
return {
...result,
id: ++nextPayloadId,
debuggingResult: {
...result.debuggingResult,
data: result.debuggingResult.data.map((data, idx) => ({
...data,
id: idx + 1,
})),
},
};
}
8 changes: 7 additions & 1 deletion src/extension/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { ErrorCodes } from "@apollo/client/invariantErrorCodes";
import type { JSONObject } from "../application/types/json";
import type { ApolloClientInfo, NoInfer, SafeAny } from "../types";
import type {
ApolloClientInfo,
ConnectorsDebuggingResultPayload,
NoInfer,
SafeAny,
} from "../types";
import { createId } from "../utils/createId";
import { RPC_MESSAGE_TIMEOUT } from "./errorMessages";
import { deserializeError, serializeError } from "./errorSerialization";
Expand All @@ -15,6 +20,7 @@ export type RPCRequest = {
getMutations(clientId: string): MutationDetails[];
getCache(clientId: string): JSONObject;
getErrorCodes(version: string): Promise<ErrorCodes | undefined>;
getConnectorsRequests(): ConnectorsDebuggingResultPayload[];
};

export interface RpcClient {
Expand Down
26 changes: 25 additions & 1 deletion src/extension/tab/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import * as manifest from "../chrome/manifest.json";
const { version: devtoolsVersion } = manifest;
import type { MutationDetails, QueryDetails } from "./helpers";
import { getQueries, getQueriesLegacy, getMutations } from "./helpers";
import type { ApolloClientInfo, SafeAny } from "../../types";
import type {
ApolloClientInfo,
ConnectorsDebuggingResultPayload,
SafeAny,
} from "../../types";
import { getPrivateAccess } from "../../privateAccess";
import type { JSONObject } from "../../application/types/json";
import { createWindowActor } from "../actor";
Expand Down Expand Up @@ -131,6 +135,26 @@ handleRpc("getCache", (clientId) => {
return getClientById(clientId)?.cache.extract(true) ?? {};
});

// Temporarily hold all connectors requests until the devtools connects and asks
// for them. After these are requests, we clear this array since we no longer
// need to hold onto the requests here.
let connectorsRequests: ConnectorsDebuggingResultPayload[] = [];
handleRpc("getConnectorsRequests", () => {
try {
return connectorsRequests;
} finally {
disconnectFromDebuggingResults();
connectorsRequests = [];
}
});

const disconnectFromDebuggingResults = tab.on(
"connectorsDebuggingResult",
({ payload }) => {
connectorsRequests.push(payload);
}
);

function getClientById(clientId: string) {
const [client] =
[...knownClients.entries()].find(([, id]) => id === clientId) ?? [];
Expand Down

0 comments on commit 502b753

Please sign in to comment.