fix(watch): should trigger watcher callback on triggerRef when watching ref source
fix #1736
This commit is contained in:
parent
09702e95b9
commit
fce2689ff1
@ -12,7 +12,8 @@ import {
|
||||
ITERATE_KEY,
|
||||
DebuggerEvent,
|
||||
TrackOpTypes,
|
||||
TriggerOpTypes
|
||||
TriggerOpTypes,
|
||||
triggerRef
|
||||
} from '@vue/reactivity'
|
||||
|
||||
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
|
||||
@ -637,4 +638,27 @@ describe('api: watch', () => {
|
||||
v.value++
|
||||
expect(calls).toBe(1)
|
||||
})
|
||||
|
||||
test('should force trigger on triggerRef when watching a ref', async () => {
|
||||
const v = ref({ a: 1 })
|
||||
let sideEffect = 0
|
||||
watch(v, obj => {
|
||||
sideEffect = obj.a
|
||||
})
|
||||
|
||||
v.value = v.value
|
||||
await nextTick()
|
||||
// should not trigger
|
||||
expect(sideEffect).toBe(0)
|
||||
|
||||
v.value.a++
|
||||
await nextTick()
|
||||
// should not trigger
|
||||
expect(sideEffect).toBe(0)
|
||||
|
||||
triggerRef(v)
|
||||
await nextTick()
|
||||
// should trigger now
|
||||
expect(sideEffect).toBe(2)
|
||||
})
|
||||
})
|
||||
|
@ -159,8 +159,9 @@ function doWatch(
|
||||
}
|
||||
|
||||
let getter: () => any
|
||||
if (isRef(source)) {
|
||||
getter = () => source.value
|
||||
const isRefSource = isRef(source)
|
||||
if (isRefSource) {
|
||||
getter = () => (source as Ref).value
|
||||
} else if (isReactive(source)) {
|
||||
getter = () => source
|
||||
deep = true
|
||||
@ -239,7 +240,7 @@ function doWatch(
|
||||
if (cb) {
|
||||
// watch(source, cb)
|
||||
const newValue = runner()
|
||||
if (deep || hasChanged(newValue, oldValue)) {
|
||||
if (deep || isRefSource || hasChanged(newValue, oldValue)) {
|
||||
// cleanup before running cb again
|
||||
if (cleanup) {
|
||||
cleanup()
|
||||
|
Loading…
Reference in New Issue
Block a user