Skip to content

Commit

Permalink
Allow resetting stringifyCanon used by canonicalStringify.
Browse files Browse the repository at this point in the history
This reset will happen whenever cache.gc() is called, which makes sense
because it frees all memory associated with the replaced stringifyCanon,
at the cost of temporarily slowing down canonicalStringify (but without
any logical changes in the behavior/output of canonicalStringify).
  • Loading branch information
benjamn committed May 17, 2021
1 parent 842eb5e commit ac74d8e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
TypePolicies,
} from './policies';
import { hasOwn } from './helpers';
import { resetStringifyCanon } from './object-canon';

export interface InMemoryCacheConfig extends ApolloReducerConfig {
resultCaching?: boolean;
Expand Down Expand Up @@ -262,6 +263,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {

// Request garbage collection of unreachable normalized entities.
public gc() {
resetStringifyCanon();
return this.optimisticData.gc();
}

Expand Down Expand Up @@ -322,6 +324,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
public reset(): Promise<void> {
this.init();
this.broadcastWatches();
resetStringifyCanon();
return Promise.resolve();
}

Expand Down
5 changes: 4 additions & 1 deletion src/cache/inmemory/object-canon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ type SortedKeysInfo = {
json: string;
};

const stringifyCanon = new ObjectCanon;
let stringifyCanon = new ObjectCanon;
export function resetStringifyCanon() {
stringifyCanon = new ObjectCanon;
}
const stringifyCache = new WeakMap<object, string>();

// Since the keys of canonical objects are always created in lexicographically
Expand Down

0 comments on commit ac74d8e

Please sign in to comment.