Skip to content

Commit

Permalink
fix(reactivity): fix regression for computed with mutation (#10119)
Browse files Browse the repository at this point in the history
close #10114
  • Loading branch information
Doctor-wu committed Jan 15, 2024
1 parent 3cd3a44 commit 20f62af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
28 changes: 19 additions & 9 deletions packages/reactivity/__tests__/computed.spec.ts
@@ -1,3 +1,4 @@
import { h, nextTick, nodeOps, render, serializeInner } from '@vue/runtime-test'
import {
type DebuggerEvent,
ITERATE_KEY,
Expand Down Expand Up @@ -470,15 +471,24 @@ describe('reactivity/computed', () => {
expect(c2.effect._dirtyLevel).toBe(DirtyLevels.NotDirty)
})

it('should work when chained(ref+computed)', () => {
const value = ref(0)
it('should be not dirty after deps mutate (mutate deps in computed)', async () => {
const state = reactive<any>({})
const consumer = computed(() => {
value.value++
return 'foo'
})
const provider = computed(() => value.value + consumer.value)
expect(provider.value).toBe('0foo')
expect(provider.effect._dirtyLevel).toBe(DirtyLevels.Dirty)
expect(provider.value).toBe('1foo')
if (!('a' in state)) state.a = 1
return state.a
})
const Comp = {
setup: () => {
nextTick().then(() => {
state.a = 2
})
return () => consumer.value
},
}
const root = nodeOps.createElement('div')
render(h(Comp), root)
await nextTick()
await nextTick()
expect(serializeInner(root)).toBe(`2`)
})
})
5 changes: 4 additions & 1 deletion packages/reactivity/src/effect.ts
Expand Up @@ -295,7 +295,10 @@ export function triggerEffects(
// when recurse effect is running, dep map could have outdated items
continue
}
if (effect._dirtyLevel < dirtyLevel) {
if (
effect._dirtyLevel < dirtyLevel &&
!(effect._runnings && !effect.allowRecurse)
) {
const lastDirtyLevel = effect._dirtyLevel
effect._dirtyLevel = dirtyLevel
if (lastDirtyLevel === DirtyLevels.NotDirty) {
Expand Down

0 comments on commit 20f62af

Please sign in to comment.