Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print: use WeakCache instead of WeakMap #11367

Merged
merged 9 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-avocados-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

`print`: use `WeakCache` instead of `WeakMap`
1 change: 1 addition & 0 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const checks = [
"react",
"react-dom",
"@graphql-typed-document-node/core",
"@wry/caches",
"@wry/context",
"@wry/equality",
"@wry/trie",
Expand Down
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 38164,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32188
"dist/apollo-client.min.cjs": 38178,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32206
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
},
"dependencies": {
"@graphql-typed-document-node/core": "^3.1.1",
"@wry/caches": "^1.0.0",
"@wry/context": "^0.7.3",
"@wry/equality": "^0.5.6",
"@wry/trie": "^0.4.3",
Expand Down
16 changes: 8 additions & 8 deletions src/utilities/graphql/print.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { ASTNode } from "graphql";
import { print as origPrint } from "graphql";
import { canUseWeakMap } from "../common/canUse.js";
import { WeakCache } from "@wry/caches";

let printCache: undefined | WeakMap<ASTNode, string>;
// further TODO: replace with `optimism` with a `WeakCache` once those are available
let printCache!: WeakCache<ASTNode, string>;
export const print = Object.assign(
(ast: ASTNode) => {
let result;
result = printCache?.get(ast);
let result = printCache.get(ast);

if (!result) {
result = origPrint(ast);
printCache?.set(ast, result);
printCache.set(ast, (result = origPrint(ast)));
phryneas marked this conversation as resolved.
Show resolved Hide resolved
}
return result;
},
{
reset() {
printCache = canUseWeakMap ? new WeakMap() : undefined;
printCache = new WeakCache<
ASTNode,
string
>(/** TODO: decide on a maximum size (will do all max sizes in a combined separate PR) */);
},
}
);
Expand Down
Loading