Skip to content

Commit

Permalink
fix(reactivity): do not remove dep from depsMap when unsubbed by comp…
Browse files Browse the repository at this point in the history
…uted

follow up of 235ea47 after discovering regression in vant ecosystem-ci tests
  • Loading branch information
yyx990803 committed Sep 20, 2024
1 parent b030c8b commit 960706e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 19 additions & 1 deletion packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,9 +1006,27 @@ describe('reactivity/computed', () => {
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 2</p>`)
})

it('manual trigger computed', () => {
test('manual trigger computed', () => {
const cValue = computed(() => 1)
triggerRef(cValue)
expect(cValue.value).toBe(1)
})

test('computed should remain live after losing all subscribers', () => {
const toggle = ref(true)
const state = reactive({
a: 1,
})
const p = computed(() => state.a + 1)
const pp = computed(() => {
return toggle.value ? p.value : 111
})

const { effect: e } = effect(() => pp.value)
e.stop()

expect(p.value).toBe(2)
state.a++
expect(p.value).toBe(3)
})
})
6 changes: 3 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
}
}

function removeSub(link: Link) {
function removeSub(link: Link, fromComputed = false) {
const { dep, prevSub, nextSub } = link
if (prevSub) {
prevSub.nextSub = nextSub
Expand All @@ -425,9 +425,9 @@ function removeSub(link: Link) {
// value can be GCed
dep.computed.flags &= ~EffectFlags.TRACKING
for (let l = dep.computed.deps; l; l = l.nextDep) {
removeSub(l)
removeSub(l, true)
}
} else if (dep.map) {
} else if (dep.map && !fromComputed) {
// property dep, remove it from the owner depsMap
dep.map.delete(dep.key)
if (!dep.map.size) targetMap.delete(dep.target!)
Expand Down

0 comments on commit 960706e

Please sign in to comment.