]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): do not remove dep from depsMap when cleaning up deps of computed...
authorJürg Lehni <juerg@scratchdisk.com>
Sun, 22 Sep 2024 03:49:35 +0000 (05:49 +0200)
committerGitHub <noreply@github.com>
Sun, 22 Sep 2024 03:49:35 +0000 (11:49 +0800)
packages/reactivity/__tests__/computed.spec.ts
packages/reactivity/src/effect.ts

index 873fd11619c922c99788376f2b205ada648a2b9a..e4a337cb8b6a68ff773eb66a79c74845421b2a55 100644 (file)
@@ -1022,4 +1022,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)
+  })
 })
index d0b7c7bd9fbdc94d149bae8ae1f7555ccad9c232..20bbada505fdbc55e98f67055cfc6db6e37afcbb 100644 (file)
@@ -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
@@ -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 {
@@ -394,7 +394,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
   } finally {
     activeSub = prevSub
     shouldTrack = prevShouldTrack
-    cleanupDeps(computed)
+    cleanupDeps(computed, true)
     computed.flags &= ~EffectFlags.RUNNING
   }
 }