-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat: replace JSON.stringify
with replaceDeepEqual
in structural sharing integrity check
#8030
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
73c1752
feat: throw when `replaceDeepEqual` contains circular references
jxom a610e3f
ci: apply automated fixes
autofix-ci[bot] 2bcece2
chore: up test
jxom 8de5540
chore: review changes
jxom 93b2d56
ci: apply automated fixes
autofix-ci[bot] 9fc0c0a
chore: add return
jxom cd2c14c
Merge branch 'main' into jxom/cyclic-check
TkDodo 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
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 |
---|---|---|
|
@@ -242,42 +242,54 @@ export function partialMatchKey(a: any, b: any): boolean { | |
*/ | ||
export function replaceEqualDeep<T>(a: unknown, b: T): T | ||
export function replaceEqualDeep(a: any, b: any): any { | ||
if (a === b) { | ||
return a | ||
} | ||
const seen = new WeakSet() | ||
|
||
function _replaceEqualDeep(a: any, b: any) { | ||
if (a === b) { | ||
return a | ||
} | ||
|
||
const array = isPlainArray(a) && isPlainArray(b) | ||
|
||
if (array || (isPlainObject(a) && isPlainObject(b))) { | ||
if (seen.has(a)) { | ||
throw new Error('circular reference detected.') | ||
} | ||
seen.add(a) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these lines were added |
||
|
||
const aItems = array ? a : Object.keys(a) | ||
const aSize = aItems.length | ||
const bItems = array ? b : Object.keys(b) | ||
const bSize = bItems.length | ||
const copy: any = array ? [] : {} | ||
|
||
const array = isPlainArray(a) && isPlainArray(b) | ||
|
||
if (array || (isPlainObject(a) && isPlainObject(b))) { | ||
const aItems = array ? a : Object.keys(a) | ||
const aSize = aItems.length | ||
const bItems = array ? b : Object.keys(b) | ||
const bSize = bItems.length | ||
const copy: any = array ? [] : {} | ||
|
||
let equalItems = 0 | ||
|
||
for (let i = 0; i < bSize; i++) { | ||
const key = array ? i : bItems[i] | ||
if ( | ||
((!array && aItems.includes(key)) || array) && | ||
a[key] === undefined && | ||
b[key] === undefined | ||
) { | ||
copy[key] = undefined | ||
equalItems++ | ||
} else { | ||
copy[key] = replaceEqualDeep(a[key], b[key]) | ||
if (copy[key] === a[key] && a[key] !== undefined) { | ||
let equalItems = 0 | ||
|
||
for (let i = 0; i < bSize; i++) { | ||
const key = array ? i : bItems[i] | ||
|
||
if ( | ||
((!array && aItems.includes(key)) || array) && | ||
a[key] === undefined && | ||
b[key] === undefined | ||
) { | ||
copy[key] = undefined | ||
equalItems++ | ||
} else { | ||
copy[key] = _replaceEqualDeep(a[key], b[key]) | ||
if (copy[key] === a[key] && a[key] !== undefined) { | ||
equalItems++ | ||
} | ||
} | ||
} | ||
|
||
return aSize === bSize && equalItems === aSize ? a : copy | ||
} | ||
|
||
return aSize === bSize && equalItems === aSize ? a : copy | ||
return b | ||
} | ||
|
||
return b | ||
return _replaceEqualDeep(a, b) | ||
} | ||
|
||
/** | ||
|
@@ -354,19 +366,17 @@ export function replaceData< | |
if (typeof options.structuralSharing === 'function') { | ||
return options.structuralSharing(prevData, data) as TData | ||
} else if (options.structuralSharing !== false) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
try { | ||
JSON.stringify(prevData) | ||
JSON.stringify(data) | ||
} catch (error) { | ||
console.error( | ||
`StructuralSharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`, | ||
) | ||
} | ||
try { | ||
// Structurally share data between prev and new data if needed | ||
return replaceEqualDeep(prevData, data) | ||
} catch (error) { | ||
console.error( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Realised this should be under a dev env guard. Will fix when I’m back at computer. |
||
`Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`, | ||
) | ||
throw new Error( | ||
`Query hash ${options.queryHash} contains non-serializable data. ${error}`, | ||
) | ||
} | ||
|
||
// Structurally share data between prev and new data if needed | ||
return replaceEqualDeep(prevData, data) | ||
} | ||
return data | ||
} | ||
|
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.
this line was added