Skip to content

Commit 0267a58

Browse files
authored
fix(reactivity): do not remove dep from depsMap when cleaning up deps of computed (#11995)
1 parent d1764a1 commit 0267a58

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/reactivity/__tests__/computed.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,19 @@ describe('reactivity/computed', () => {
10221022
state.a++
10231023
expect(p.value).toBe(3)
10241024
})
1025+
1026+
test('computed dep cleanup should not cause property dep to be deleted', () => {
1027+
const toggle = ref(true)
1028+
const state = reactive({ a: 1 })
1029+
const p = computed(() => {
1030+
return toggle.value ? state.a : 111
1031+
})
1032+
const pp = computed(() => state.a)
1033+
effect(() => p.value)
1034+
1035+
expect(pp.value).toBe(1)
1036+
toggle.value = false
1037+
state.a++
1038+
expect(pp.value).toBe(2)
1039+
})
10251040
})

packages/reactivity/src/effect.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function prepareDeps(sub: Subscriber) {
292292
}
293293
}
294294

295-
function cleanupDeps(sub: Subscriber) {
295+
function cleanupDeps(sub: Subscriber, fromComputed = false) {
296296
// Cleanup unsued deps
297297
let head
298298
let tail = sub.depsTail
@@ -302,7 +302,7 @@ function cleanupDeps(sub: Subscriber) {
302302
if (link.version === -1) {
303303
if (link === tail) tail = prev
304304
// unused - remove it from the dep's subscribing effect list
305-
removeSub(link)
305+
removeSub(link, fromComputed)
306306
// also remove it from this effect's dep list
307307
removeDep(link)
308308
} else {
@@ -394,7 +394,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
394394
} finally {
395395
activeSub = prevSub
396396
shouldTrack = prevShouldTrack
397-
cleanupDeps(computed)
397+
cleanupDeps(computed, true)
398398
computed.flags &= ~EffectFlags.RUNNING
399399
}
400400
}

0 commit comments

Comments
 (0)