-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
reset
method to print
, hook up to ApolloCache.gc
- Loading branch information
Showing
3 changed files
with
27 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Add `reset` method to `print`, hook up to `ApolloCache.gc` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import type { ASTNode } from "graphql"; | ||
import { print as origPrint } from "graphql"; | ||
import { canUseWeakMap } from "../common/canUse.js"; | ||
|
||
const printCache = canUseWeakMap ? new WeakMap() : undefined; | ||
export const print: typeof origPrint = (ast) => { | ||
let result; | ||
result = printCache?.get(ast); | ||
let printCache: undefined | WeakMap<ASTNode, string>; | ||
// further TODO: replace with `optimism` with a `WeakCache` once those are available | ||
export const print = Object.assign( | ||
(ast: ASTNode) => { | ||
let result; | ||
result = printCache?.get(ast); | ||
|
||
if (!result) { | ||
result = origPrint(ast); | ||
printCache?.set(ast, result); | ||
if (!result) { | ||
result = origPrint(ast); | ||
printCache?.set(ast, result); | ||
} | ||
return result; | ||
}, | ||
{ | ||
reset() { | ||
printCache = canUseWeakMap ? new WeakMap() : undefined; | ||
}, | ||
} | ||
return result; | ||
}; | ||
); | ||
|
||
print.reset(); |