Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): do not remove dep from depsMap when cleaning up deps of computed #11995

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,4 +1029,19 @@ describe('reactivity/computed', () => {
state.a++
expect(p.value).toBe(3)
})

test('computed dep cleanup should not cause property dep to be deleted', () => {
const toggle = ref(true)
const state = reactive({ a: 1 })
const p = computed(() => {
return toggle.value ? state.a : 111
})
const pp = computed(() => state.a)
effect(() => p.value)

expect(pp.value).toBe(1)
toggle.value = false
state.a++
expect(pp.value).toBe(2)
})
})
6 changes: 3 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function prepareDeps(sub: Subscriber) {
}
}

function cleanupDeps(sub: Subscriber) {
function cleanupDeps(sub: Subscriber, fromComputed = false) {
// Cleanup unsued deps
let head
let tail = sub.depsTail
Expand All @@ -302,7 +302,7 @@ function cleanupDeps(sub: Subscriber) {
if (link.version === -1) {
if (link === tail) tail = prev
// unused - remove it from the dep's subscribing effect list
removeSub(link)
removeSub(link, fromComputed)
// also remove it from this effect's dep list
removeDep(link)
} else {
Expand Down Expand Up @@ -394,7 +394,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
} finally {
activeSub = prevSub
shouldTrack = prevShouldTrack
cleanupDeps(computed)
cleanupDeps(computed, true)
computed.flags &= ~EffectFlags.RUNNING
}
}
Expand Down