Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay initializing stringifyCanon until canonicalStringify used. #8558

Merged
merged 2 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)();
}