Skip to content

Commit

Permalink
refactor(form): hide 'parents' argument in immutableReconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored and rexxars committed Sep 14, 2022
1 parent f6a4c5b commit 5b9fdb9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/sanity/src/form/store/utils/immutableReconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
* @param previous - the previous value
* @param next - the next/current value
*/
export function immutableReconcile<T>(
export function immutableReconcile<T>(previous: unknown, next: T): T {
return _immutableReconcile(previous, next, new WeakSet())
}

function _immutableReconcile<T>(
previous: unknown,
next: T,
parents: WeakSet<any> = new Set()
/**
* Keep track of visited nodes to prevent infinite recursion in case of circular structures
*/
parents: WeakSet<any>
): T {
if (previous === next) return previous as T

Expand All @@ -33,7 +40,7 @@ export function immutableReconcile<T>(
return next
}

const nextItem = immutableReconcile(previous[index], next[index], parents)
const nextItem = _immutableReconcile(previous[index], next[index], parents)

if (nextItem !== previous[index]) {
allEqual = false
Expand All @@ -54,7 +61,7 @@ export function immutableReconcile<T>(
if (parents.has(next[key])) {
return next
}
const nextValue = immutableReconcile(previous[key], next[key]!, parents)
const nextValue = _immutableReconcile(previous[key], next[key]!, parents)
if (nextValue !== previous[key]) {
allEqual = false
}
Expand Down

0 comments on commit 5b9fdb9

Please sign in to comment.