Skip to content

Commit

Permalink
fix(watch): fix deep watchers on refs containing primitives (vuejs#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Apr 29, 2020
1 parent 380d228 commit 7566c5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ describe('api: watch', () => {
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 () => {
const state = reactive({ count: 1 })
const count = ref(1)
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ export function instanceWatch(

function traverse(value: unknown, seen: Set<unknown> = new Set()) {
if (!isObject(value) || seen.has(value)) {
return value
}
if (seen.has(value)) {
return
}
seen.add(value)
Expand Down

0 comments on commit 7566c5a

Please sign in to comment.