-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Efficiently canonicalize InMemoryCache result objects. #7439
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7ff1b31
Efficiently canonicalize InMemoryCache result objects.
benjamn 789f46b
Pass scalar field values through without canonicalizing them.
benjamn 05c362f
Test === equality for partial results from different queries.
benjamn 3059ee3
Bump bundlesize limit from 24.5kB to 24.9kB.
benjamn 8989ad3
Test that canonicalization does not modify scalar objects.
benjamn cad1a06
Improve Canon class comments.
benjamn 9c6a09c
Rename Canon class to ObjectCanon.
benjamn e8f3ef8
Avoid wrapping non-object values with Pass wrappers.
benjamn 9fe2173
Update optimism and import Trie from @wry/trie instead.
benjamn bdf3491
Use firstValueIndex variable to make array indexing clearer.
benjamn d6c5826
Add more direct unit tests of ObjectCanon.
benjamn 48bd0b1
Mention PR #7439 in CHANGELOG.md.
benjamn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
18 changes: 18 additions & 0 deletions
18
src/cache/inmemory/__tests__/__snapshots__/readFromStore.ts.snap
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,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`reading from the store returns === results for different queries 1`] = ` | ||
Object { | ||
"ROOT_QUERY": Object { | ||
"__typename": "Query", | ||
"a": Array [ | ||
"a", | ||
"y", | ||
"y", | ||
], | ||
"b": Object { | ||
"c": "C", | ||
"d": "D", | ||
}, | ||
}, | ||
} | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { ObjectCanon } from "../object-canon"; | ||
|
||
describe("ObjectCanon", () => { | ||
it("can canonicalize objects and arrays", () => { | ||
const canon = new ObjectCanon; | ||
|
||
const obj1 = { | ||
a: [1, 2], | ||
b: { | ||
c: [{ | ||
d: "dee", | ||
e: "ee", | ||
}, "f"], | ||
g: "gee", | ||
}, | ||
}; | ||
|
||
const obj2 = { | ||
b: { | ||
g: "gee", | ||
c: [{ | ||
e: "ee", | ||
d: "dee", | ||
}, "f"], | ||
}, | ||
a: [1, 2], | ||
}; | ||
|
||
expect(obj1).toEqual(obj2); | ||
expect(obj1).not.toBe(obj2); | ||
|
||
const c1 = canon.admit(obj1); | ||
const c2 = canon.admit(obj2); | ||
|
||
expect(c1).toBe(c2); | ||
expect(c1).toEqual(obj1); | ||
expect(c1).toEqual(obj2); | ||
expect(c2).toEqual(obj1); | ||
expect(c2).toEqual(obj2); | ||
expect(c1).not.toBe(obj1); | ||
expect(c1).not.toBe(obj2); | ||
expect(c2).not.toBe(obj1); | ||
expect(c2).not.toBe(obj2); | ||
|
||
expect(canon.admit(c1)).toBe(c1); | ||
expect(canon.admit(c2)).toBe(c2); | ||
}); | ||
|
||
it("preserves custom prototypes", () => { | ||
const canon = new ObjectCanon; | ||
|
||
class Custom { | ||
constructor(public value: any) {} | ||
getValue() { return this.value } | ||
} | ||
|
||
const customs = [ | ||
new Custom("oyez"), | ||
new Custom(1234), | ||
new Custom(true), | ||
]; | ||
|
||
const admitted = canon.admit(customs); | ||
expect(admitted).not.toBe(customs); | ||
expect(admitted).toEqual(customs); | ||
|
||
function check(i: number) { | ||
expect(admitted[i]).toEqual(customs[i]); | ||
expect(admitted[i]).not.toBe(customs[i]); | ||
expect(admitted[i].getValue()).toBe(customs[i].getValue()); | ||
expect(Object.getPrototypeOf(admitted[i])).toBe(Custom.prototype); | ||
expect(admitted[i]).toBeInstanceOf(Custom); | ||
} | ||
check(0); | ||
check(1); | ||
check(2); | ||
|
||
expect(canon.admit(customs)).toBe(admitted); | ||
}); | ||
|
||
it("unwraps Pass wrappers as-is", () => { | ||
const canon = new ObjectCanon; | ||
|
||
const cd = { | ||
c: "see", | ||
d: "dee", | ||
}; | ||
|
||
const obj = { | ||
a: cd, | ||
b: canon.pass(cd), | ||
e: cd, | ||
}; | ||
|
||
function check() { | ||
const admitted = canon.admit(obj); | ||
expect(admitted).not.toBe(obj); | ||
expect(admitted.b).toBe(cd); | ||
expect(admitted.e).toEqual(cd); | ||
expect(admitted.e).not.toBe(cd); | ||
expect(admitted.e).toEqual(admitted.b); | ||
expect(admitted.e).not.toBe(admitted.b); | ||
expect(admitted.e).toBe(admitted.a); | ||
} | ||
check(); | ||
check(); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very Shakespearean of you