Skip to content

Commit

Permalink
Avoid serializing the payload before sending it in devtools messages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller authored Mar 7, 2024
1 parent a36f59d commit cb22c6f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-kiwis-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"apollo-client-devtools": patch
---

Don't `JSON.stringify` the Apollo Client instance data before sending it in the message payload when communicating between the various parts of the devtools extension. This was mostly redundant since message passing already relies on the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
16 changes: 2 additions & 14 deletions src/extension/devtools/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import browser from "webextension-polyfill";
import { QueryInfo } from "../tab/helpers";
import { JSONObject } from "../../application/types/json";
import { devtoolsMachine } from "../../application/machines";
import { createPortActor } from "../actor";
import { ClientMessage } from "../messages";
Expand Down Expand Up @@ -37,11 +35,7 @@ clientPort.on("connectToDevtools", (message) => {
devtoolsMachine.send({
type: "connect",
context: {
clientContext: JSON.parse(message.payload ?? "") as {
queries: QueryInfo[];
mutations: QueryInfo[];
cache: Record<string, JSONObject>;
},
clientContext: message.payload,
},
});
});
Expand Down Expand Up @@ -148,13 +142,7 @@ async function createDevtoolsPanel() {
}

removeUpdateListener = clientPort.on("update", (message) => {
const { queries, mutations, cache } = JSON.parse(
message.payload ?? ""
) as {
queries: QueryInfo[];
mutations: QueryInfo[];
cache: Record<string, JSONObject>;
};
const { queries, mutations, cache } = message.payload;

panelWindow.send({
type: "update",
Expand Down
7 changes: 5 additions & 2 deletions src/extension/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ export type ClientMessage =
| { type: "clientNotFound" }
| { type: "connectToClient" }
| { type: "connectToClientTimeout" }
| { type: "connectToDevtools"; payload: string }
| {
type: "connectToDevtools";
payload: GetContext<DevtoolsMachine>["clientContext"];
}
| { type: "disconnectFromDevtools" }
| ExplorerRequestMessage
| ExplorerResponseMessage
| ExplorerSubscriptionTerminationMessage
| { type: "requestData" }
| { type: "update"; payload: string };
| { type: "update"; payload: GetContext<DevtoolsMachine>["clientContext"] };

export type PanelMessage =
| ExplorerRequestMessage
Expand Down
4 changes: 2 additions & 2 deletions src/extension/tab/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ function initializeHook() {
function sendHookDataToDevTools(eventName: "update" | "connectToDevtools") {
tab.send({
type: eventName,
payload: JSON.stringify({
payload: {
queries: hook.getQueries(),
mutations: hook.getMutations(),
cache: hook.getCache(),
}),
},
});
}

Expand Down

0 comments on commit cb22c6f

Please sign in to comment.