Skip to content

Commit

Permalink
fix: handle circular references in HMR check
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jul 23, 2024
1 parent aa41e9d commit df28f79
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/core/handleHotUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,25 @@ export function isOnlyTemplateChanged(
)
}

function deepEqual(obj1: any, obj2: any, excludeProps: string[] = []): boolean {
function deepEqual(
obj1: any,
obj2: any,
excludeProps: string[] = [],
deepParentsOfObj1: any[] = [],
): boolean {
// Check if both objects are of the same type
if (typeof obj1 !== typeof obj2) {
return false
}

// Check if both objects are primitive types or null
if (obj1 == null || obj2 == null || typeof obj1 !== 'object') {
// or circular reference
if (
obj1 == null ||
obj2 == null ||
typeof obj1 !== 'object' ||
deepParentsOfObj1.includes(obj1)
) {
return obj1 === obj2
}

Expand All @@ -224,7 +235,12 @@ function deepEqual(obj1: any, obj2: any, excludeProps: string[] = []): boolean {
continue
}

if (!deepEqual(obj1[key], obj2[key], excludeProps)) {
if (
!deepEqual(obj1[key], obj2[key], excludeProps, [
...deepParentsOfObj1,
obj1,
])
) {
return false
}
}
Expand Down

0 comments on commit df28f79

Please sign in to comment.