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,
|
ITERATE_KEY,
|
||||||
DebuggerEvent,
|
DebuggerEvent,
|
||||||
TrackOpTypes,
|
TrackOpTypes,
|
||||||
TriggerOpTypes
|
TriggerOpTypes,
|
||||||
|
triggerRef
|
||||||
} from '@vue/reactivity'
|
} from '@vue/reactivity'
|
||||||
|
|
||||||
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
|
// reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
|
||||||
@ -637,4 +638,27 @@ describe('api: watch', () => {
|
|||||||
v.value++
|
v.value++
|
||||||
expect(calls).toBe(1)
|
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
|
let getter: () => any
|
||||||
if (isRef(source)) {
|
const isRefSource = isRef(source)
|
||||||
getter = () => source.value
|
if (isRefSource) {
|
||||||
|
getter = () => (source as Ref).value
|
||||||
} else if (isReactive(source)) {
|
} else if (isReactive(source)) {
|
||||||
getter = () => source
|
getter = () => source
|
||||||
deep = true
|
deep = true
|
||||||
@ -239,7 +240,7 @@ function doWatch(
|
|||||||
if (cb) {
|
if (cb) {
|
||||||
// watch(source, cb)
|
// watch(source, cb)
|
||||||
const newValue = runner()
|
const newValue = runner()
|
||||||
if (deep || hasChanged(newValue, oldValue)) {
|
if (deep || isRefSource || hasChanged(newValue, oldValue)) {
|
||||||
// cleanup before running cb again
|
// cleanup before running cb again
|
||||||
if (cleanup) {
|
if (cleanup) {
|
||||||
cleanup()
|
cleanup()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user