From 30d17bfebe44dbfa7b78c8982cfeb49afd37129c Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic <lorenz.weber-tronic@apollographql.com> Date: Wed, 29 Nov 2023 11:46:47 +0100 Subject: [PATCH] `print`: use `WeakCache` instead of `WeakMap` (#11367) Co-authored-by: Jerel Miller <jerelmiller@gmail.com> Co-authored-by: phryneas <phryneas@users.noreply.github.com> --- .changeset/polite-avocados-warn.md | 5 +++++ .size-limit.cjs | 1 + .size-limits.json | 4 ++-- package-lock.json | 12 ++++++++++++ package.json | 1 + src/utilities/graphql/print.ts | 15 ++++++++------- 6 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 .changeset/polite-avocados-warn.md diff --git a/.changeset/polite-avocados-warn.md b/.changeset/polite-avocados-warn.md new file mode 100644 index 00000000000..dd04015cf3d --- /dev/null +++ b/.changeset/polite-avocados-warn.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +`print`: use `WeakCache` instead of `WeakMap` diff --git a/.size-limit.cjs b/.size-limit.cjs index 7c7b71da42f..6faa1c00aca 100644 --- a/.size-limit.cjs +++ b/.size-limit.cjs @@ -36,6 +36,7 @@ const checks = [ "react", "react-dom", "@graphql-typed-document-node/core", + "@wry/caches", "@wry/context", "@wry/equality", "@wry/trie", diff --git a/.size-limits.json b/.size-limits.json index 7bc50667da7..d5dd8296590 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -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 } diff --git a/package-lock.json b/package-lock.json index 886832fd3e9..e879e9a11bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "license": "MIT", "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", @@ -3294,6 +3295,17 @@ "url": "https://opencollective.com/typescript-eslint" } }, + "node_modules/@wry/caches": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@wry/caches/-/caches-1.0.0.tgz", + "integrity": "sha512-FHRUDe2tqrXAj6A/1D39No68lFWbbnh+NCpG9J/6idhL/2Mb/AaxBTYg/sbUVImEo8a4mWeOewUlB1W7uLjByA==", + "dependencies": { + "tslib": "^2.3.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/@wry/context": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/@wry/context/-/context-0.7.3.tgz", diff --git a/package.json b/package.json index 29972ae6010..64adc0f0f3c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utilities/graphql/print.ts b/src/utilities/graphql/print.ts index d90a15611d0..3ba1134c968 100644 --- a/src/utilities/graphql/print.ts +++ b/src/utilities/graphql/print.ts @@ -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) */); }, } );