Skip to content

Commit

Permalink
print: use WeakCache instead of WeakMap (#11367)
Browse files Browse the repository at this point in the history
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
Co-authored-by: phryneas <phryneas@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 29, 2023
1 parent cc4ac7e commit 30d17bf
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
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 @@ -36,6 +36,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": 38600,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32187
"dist/apollo-client.min.cjs": 38603,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32203
}
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.5.0",
Expand Down
15 changes: 8 additions & 7 deletions src/utilities/graphql/print.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
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);
}
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

0 comments on commit 30d17bf

Please sign in to comment.