Skip to content

Commit 269351b

Browse files
Sheraffautofix-ci[bot]TkDodo
authored
fix(query-core): replaceEqualDeep max depth (#10032)
* fix(query-core): replaceEqualDeep max depth * restore expected error in unit test * ci: apply automated fixes * Update max depth for replaceEqualDeep function Fixes the max depth for replaceEqualDeep in query-core. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 44c3cb9 commit 269351b

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

.changeset/swift-zoos-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/query-core": patch
3+
---
4+
5+
fix(query-core): replaceEqualDeep max depth

packages/query-core/src/__tests__/query.test.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,29 +1058,22 @@ describe('query', () => {
10581058

10591059
const queryFn = vi.fn()
10601060

1061-
const data: Array<{
1062-
id: number
1063-
name: string
1064-
link: null | { id: number; name: string; link: unknown }
1065-
}> = Array.from({ length: 5 })
1066-
.fill(null)
1067-
.map((_, index) => ({
1068-
id: index,
1069-
name: `name-${index}`,
1070-
link: null,
1071-
}))
1072-
1073-
if (data[0] && data[1]) {
1074-
data[0].link = data[1]
1075-
data[1].link = data[0]
1061+
const initialData = {
1062+
foo: 'bar',
1063+
}
1064+
1065+
const data = {
1066+
get foo(): void {
1067+
return this.foo
1068+
},
10761069
}
10771070

10781071
queryFn.mockImplementation(() => sleep(10).then(() => data))
10791072

10801073
queryClient.prefetchQuery({
10811074
queryKey: key,
10821075
queryFn,
1083-
initialData: structuredClone(data),
1076+
initialData,
10841077
})
10851078
await vi.advanceTimersByTimeAsync(10)
10861079

packages/query-core/src/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@ const hasOwn = Object.prototype.hasOwnProperty
264264
* If not, it will replace any deeply equal children of `b` with those of `a`.
265265
* This can be used for structural sharing between JSON values for example.
266266
*/
267-
export function replaceEqualDeep<T>(a: unknown, b: T): T
268-
export function replaceEqualDeep(a: any, b: any): any {
267+
export function replaceEqualDeep<T>(a: unknown, b: T, depth?: number): T
268+
export function replaceEqualDeep(a: any, b: any, depth = 0): any {
269269
if (a === b) {
270270
return a
271271
}
272272

273+
if (depth > 500) return b
274+
273275
const array = isPlainArray(a) && isPlainArray(b)
274276

275277
if (!array && !(isPlainObject(a) && isPlainObject(b))) return b
@@ -303,7 +305,7 @@ export function replaceEqualDeep(a: any, b: any): any {
303305
continue
304306
}
305307

306-
const v = replaceEqualDeep(aItem, bItem)
308+
const v = replaceEqualDeep(aItem, bItem, depth + 1)
307309
copy[key] = v
308310
if (v === aItem) equalItems++
309311
}

0 commit comments

Comments
 (0)