Skip to content

Commit

Permalink
fix(reactivity): rely on dirty check only when computed has deps (#11931
Browse files Browse the repository at this point in the history
)

close #11929
  • Loading branch information
jh-leong authored Sep 16, 2024
1 parent 346bfaf commit aa5dafd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ref,
shallowRef,
toRaw,
triggerRef,
} from '../src'
import { EffectFlags, pauseTracking, resetTracking } from '../src/effect'
import type { ComputedRef, ComputedRefImpl } from '../src/computed'
Expand Down Expand Up @@ -1004,4 +1005,10 @@ describe('reactivity/computed', () => {
await nextTick()
expect(serializeInner(root)).toBe(`<button>Step</button><p>Step 2</p>`)
})

it('manual trigger computed', () => {
const cValue = computed(() => 1)
triggerRef(cValue)
expect(cValue.value).toBe(1)
})
})
7 changes: 6 additions & 1 deletion packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,12 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
// and therefore tracks no deps, thus we cannot rely on the dirty check.
// Instead, computed always re-evaluate and relies on the globalVersion
// fast path above for caching.
if (dep.version > 0 && !computed.isSSR && !isDirty(computed)) {
if (
dep.version > 0 &&
!computed.isSSR &&
computed.deps &&
!isDirty(computed)
) {
computed.flags &= ~EffectFlags.RUNNING
return
}
Expand Down

0 comments on commit aa5dafd

Please sign in to comment.