fix(watch): fix deep watchers on refs containing primitives (#984)

This commit is contained in:
Carlos Rodrigues 2020-04-17 14:55:41 +01:00 committed by GitHub
parent c0adb67c2e
commit 99fd158d09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -86,6 +86,23 @@ describe('api: watch', () => {
expect(dummy).toMatchObject([2, 1]) expect(dummy).toMatchObject([2, 1])
}) })
it('watching primitive with deep: true', async () => {
const count = ref(0)
let dummy
watch(
count,
(c, prevCount) => {
dummy = [c, prevCount]
},
{
deep: true
}
)
count.value++
await nextTick()
expect(dummy).toMatchObject([1, 0])
})
it('watching multiple sources', async () => { it('watching multiple sources', async () => {
const state = reactive({ count: 1 }) const state = reactive({ count: 1 })
const count = ref(1) const count = ref(1)

View File

@ -286,6 +286,9 @@ export function instanceWatch(
function traverse(value: unknown, seen: Set<unknown> = new Set()) { function traverse(value: unknown, seen: Set<unknown> = new Set()) {
if (!isObject(value) || seen.has(value)) { if (!isObject(value) || seen.has(value)) {
return value
}
if (seen.has(value)) {
return return
} }
seen.add(value) seen.add(value)