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): fix side effect computed dirty level changed unexpected #11183

Merged
merged 1 commit into from
Jun 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
57 changes: 57 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,63 @@ describe('reactivity/computed', () => {
expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('should chained computeds keep reactivity when computed effect happens', async () => {
const v = ref('Hello')
const c = computed(() => {
v.value += ' World'
return v.value
})
const d = computed(() => c.value)
const e = computed(() => d.value)
const Comp = {
setup: () => {
return () => d.value + ' | ' + e.value
},
}
const root = nodeOps.createElement('div')

render(h(Comp), root)
await nextTick()
expect(serializeInner(root)).toBe('Hello World | Hello World')

v.value += ' World'
await nextTick()
expect(serializeInner(root)).toBe(
'Hello World World World | Hello World World World',
)
expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('should keep dirty level when side effect computed value changed', () => {
const v = ref(0)
const c = computed(() => {
v.value += 1
return v.value
})
const d = computed(() => {
return { d: c.value }
})

const Comp = {
setup: () => {
return () => {
return [d.value.d, d.value.d]
}
},
}

const root = nodeOps.createElement('div')
render(h(Comp), root)

expect(d.value.d).toBe(1)
expect(serializeInner(root)).toBe('11')
expect(c.effect._dirtyLevel).toBe(
DirtyLevels.MaybeDirty_ComputedSideEffect_Origin,
)
expect(d.effect._dirtyLevel).toBe(DirtyLevels.MaybeDirty_ComputedSideEffect)
expect(COMPUTED_SIDE_EFFECT_WARN).toHaveBeenWarned()
})

it('debug: onTrigger (ref)', () => {
let events: DebuggerEvent[] = []
const onTrigger = vi.fn((e: DebuggerEvent) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ export class ComputedRefImpl<T> {
get value() {
// the computed ref may get wrapped by other proxies e.g. readonly() #3376
const self = toRaw(this)
const lastDirtyLevel = self.effect._dirtyLevel
if (
(!self._cacheable || self.effect.dirty) &&
hasChanged(self._value, (self._value = self.effect.run()!))
) {
triggerRefValue(self, DirtyLevels.Dirty)
// keep dirty level when side effect computed's value changed
if (lastDirtyLevel !== DirtyLevels.MaybeDirty_ComputedSideEffect) {
triggerRefValue(self, DirtyLevels.Dirty)
}
}
trackRefValue(self)
if (
Expand Down
4 changes: 3 additions & 1 deletion packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ export class ReactiveEffect<T = any> {
if (
dep.computed.effect._dirtyLevel ===
DirtyLevels.MaybeDirty_ComputedSideEffect_Origin
)
) {
resetTracking()
return true
}
triggerComputed(dep.computed)
if (this._dirtyLevel >= DirtyLevels.Dirty) {
break
Expand Down