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(runtime-core): computed refs should not trigger watch if value has no change #2290

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 24 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
WritableComputedRef,
isReadonly
} from '../src'
import { isComputed } from '../src/computed'

describe('reactivity/computed', () => {
it('should return updated value', () => {
Expand Down Expand Up @@ -193,4 +194,27 @@ describe('reactivity/computed', () => {
expect(isReadonly(z)).toBe(false)
expect(isReadonly(z.value.a)).toBe(false)
})

it('isComputed', () => {
expect(isComputed(computed(() => 1))).toBe(true)
expect(
isComputed(
computed({
get: () => 1,
set: () => undefined
})
)
).toBe(true)

expect(isComputed(ref(1))).toBe(false)
expect(isComputed(0)).toBe(false)
expect(isComputed(1)).toBe(false)
// an object that looks like a computed ref isn't necessarily a computed ref
expect(
isComputed({
value: 0,
effect: () => undefined
})
).toBe(false)
})
})
5 changes: 5 additions & 0 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ export function computed<T>(
isFunction(getterOrOptions) || !getterOrOptions.set
) as any
}

export function isComputed<T>(r: ComputedRef<T> | unknown): r is ComputedRef<T>
export function isComputed(r: any): r is ComputedRef {
return Boolean(r && r instanceof ComputedRefImpl)
}
13 changes: 13 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,17 @@ describe('api: watch', () => {
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})

// #2231
test('computed refs should not trigger watch if value has no change', async () => {
const spy = jest.fn()
const source = ref(0)
const price = computed(() => source.value === 0)
watch(price, spy)
source.value++
await nextTick()
source.value++
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
})
10 changes: 7 additions & 3 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from './errorHandling'
import { queuePostRenderEffect } from './renderer'
import { warn } from './warning'
import { isComputed } from '../../reactivity/src/computed'

export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void

Expand Down Expand Up @@ -161,9 +162,12 @@ function doWatch(
}

let getter: () => any
const isRefSource = isRef(source)
if (isRefSource) {
// Skip value comparison only for non-computed refs. because computed refs always
// trigger SET no matter whether the value has changed or not. #2231
let skipCompare = false
if (isRef(source)) {
getter = () => (source as Ref).value
skipCompare = !isComputed(source)
} else if (isReactive(source)) {
getter = () => source
deep = true
Expand Down Expand Up @@ -242,7 +246,7 @@ function doWatch(
if (cb) {
// watch(source, cb)
const newValue = runner()
if (deep || isRefSource || hasChanged(newValue, oldValue)) {
if (deep || skipCompare || hasChanged(newValue, oldValue)) {
// cleanup before running cb again
if (cleanup) {
cleanup()
Expand Down