Skip to content

Commit

Permalink
Delay initializing stringifyCanon until canonicalStringify used (#…
Browse files Browse the repository at this point in the history
…8558)

Fixes #8557.
  • Loading branch information
benjamn committed Jul 29, 2021
1 parent e3b1cbb commit d5f1b48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Apollo Client 3.4.1

### Bug Fixes

- Initialize `stringifyCanon` lazily, when `canonicalStringify` is first called, fixing `Uncaught ReferenceError: __DEV__ is not defined` errors due to usage of `__DEV__` before declaration. <br/>
[@benjamn](https://github.com/benjamn) in [#8557](https://github.com/apollographql/apollo-client/pull/8557)

## Apollo Client 3.4.0

### New documentation
Expand Down
16 changes: 10 additions & 6 deletions src/cache/inmemory/object-canon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ type SortedKeysInfo = {
// version of JSON.stringify, which automatically sorts object keys.
export const canonicalStringify = Object.assign(function (value: any): string {
if (isObjectOrArray(value)) {
if (stringifyCanon === void 0) {
resetCanonicalStringify();
}
const canonical = stringifyCanon.admit(value);
let json = stringifyCache.get(canonical);
if (json === void 0) {
Expand All @@ -212,13 +215,14 @@ export const canonicalStringify = Object.assign(function (value: any): string {
}
return JSON.stringify(value);
}, {
reset() {
stringifyCanon = new ObjectCanon;
},
reset: resetCanonicalStringify,
});

// Can be reset by calling canonicalStringify.reset().
let stringifyCanon = new ObjectCanon;
let stringifyCanon: ObjectCanon;
let stringifyCache: WeakMap<object, string>;

// Needs no resetting, thanks to weakness.
const stringifyCache = new WeakMap<object, string>();
function resetCanonicalStringify() {
stringifyCanon = new ObjectCanon;
stringifyCache = new (canUseWeakMap ? WeakMap : Map)();
}

0 comments on commit d5f1b48

Please sign in to comment.