Skip to content

Commit

Permalink
Merge pull request #695 from termi/master
Browse files Browse the repository at this point in the history
$mol_compare_deep: fix cyclic reference bug with warmed cache
  • Loading branch information
nin-jin authored Sep 2, 2024
2 parents b5a8f2f + 2697234 commit 6c114a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions compare/deep/deep.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ namespace $ {

} ,

'same POJOs with cyclic reference with cache warmup'() {
const obj1 = { test: 1, obj3: null as unknown as Object }
const obj1_copy = { test: 1, obj3: null as unknown as Object }
const obj2 = { test: 2, obj1 }
const obj2_copy = { test: 2, obj1: obj1_copy }
const obj3 = { test: 3, obj2 }
const obj3_copy = { test: 3, obj2: obj2_copy }

obj1.obj3 = obj3
obj1_copy.obj3 = obj3_copy

// warmup cache
$mol_assert_not( $mol_compare_deep( obj1 , {} ) )
$mol_assert_not( $mol_compare_deep( obj2 , {} ) )
$mol_assert_not( $mol_compare_deep( obj3 , {} ) )

$mol_assert_ok( $mol_compare_deep( obj3 , obj3_copy ) )

} ,

'Date'() {
$mol_assert_ok( $mol_compare_deep( new Date( 12345 ) , new Date( 12345 ) ) )
$mol_assert_not( $mol_compare_deep( new Date( 12345 ) , new Date( 12346 ) ) )
Expand Down
4 changes: 3 additions & 1 deletion compare/deep/deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ namespace $ {

} else {

left_cache = new WeakMap< any , boolean >([[ right, true ]])
left_cache = new WeakMap< any , boolean >()
$mol_compare_deep_cache.set( left , left_cache )

}

left_cache.set(right, true);

let result!: boolean

try {
Expand Down

0 comments on commit 6c114a5

Please sign in to comment.