Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(reactivity): should not recompute if computed does not track reactive data #12341

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
36 changes: 36 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,42 @@ describe('reactivity/computed', () => {
expect(cValue.value).toBe(1)
})

test('should not recompute when condition changes if computed does not track reactive data', async () => {
const spy = vi.fn()
const lookup = computed(() => {
spy()
return {
g: 'foo',
}
})

const unit = ref('')
const display = computed(() => {
if (!unit.value) return 'empty'
// @ts-expect-error
return lookup.value[unit.value]
})

function toggle() {
unit.value = unit.value === 'g' ? '' : 'g'
}

expect(display.value).toBe('empty')
expect(spy).toHaveBeenCalledTimes(0)

toggle()
expect(display.value).toBe('foo')
expect(spy).toHaveBeenCalledTimes(1)

toggle()
expect(display.value).toBe('empty')
expect(spy).toHaveBeenCalledTimes(1)

toggle()
expect(display.value).toBe('foo')
expect(spy).toHaveBeenCalledTimes(1)
})

test('computed should remain live after losing all subscribers', () => {
const state = reactive({ a: 1 })
const p = computed(() => state.a + 1)
Expand Down
8 changes: 5 additions & 3 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export enum EffectFlags {
DIRTY = 1 << 4,
ALLOW_RECURSE = 1 << 5,
PAUSED = 1 << 6,
FORCE_TRIGGER = 1 << 7,
}

/**
Expand Down Expand Up @@ -364,7 +365,8 @@ function isDirty(sub: Subscriber): boolean {
export function refreshComputed(computed: ComputedRefImpl): undefined {
if (
computed.flags & EffectFlags.TRACKING &&
!(computed.flags & EffectFlags.DIRTY)
!(computed.flags & EffectFlags.DIRTY) &&
!(computed.flags & EffectFlags.FORCE_TRIGGER)
) {
return
}
Expand All @@ -386,13 +388,12 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
if (
dep.version > 0 &&
!computed.isSSR &&
computed.deps &&
!(computed.flags & EffectFlags.FORCE_TRIGGER) &&
!isDirty(computed)
) {
computed.flags &= ~EffectFlags.RUNNING
return
}

const prevSub = activeSub
const prevShouldTrack = shouldTrack
activeSub = computed
Expand All @@ -413,6 +414,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
shouldTrack = prevShouldTrack
cleanupDeps(computed)
computed.flags &= ~EffectFlags.RUNNING
computed.flags &= ~EffectFlags.FORCE_TRIGGER
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import type { ComputedRef, WritableComputedRef } from './computed'
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
import { warn } from './warning'
import { EffectFlags } from './effect'

declare const RefSymbol: unique symbol
export declare const RawSymbol: unique symbol
Expand Down Expand Up @@ -186,6 +187,9 @@ class RefImpl<T = any> {
export function triggerRef(ref: Ref): void {
// ref may be an instance of ObjectRefImpl
if ((ref as unknown as RefImpl).dep) {
if ((ref as unknown as RefImpl).dep.computed) {
;(ref as any).flags |= EffectFlags.FORCE_TRIGGER
}
if (__DEV__) {
;(ref as unknown as RefImpl).dep.trigger({
target: ref,
Expand Down