Skip to content

Commit

Permalink
fix(watch): traverse refs in deep watch (#1939)
Browse files Browse the repository at this point in the history
ref #1900
  • Loading branch information
yangmingshan authored Aug 23, 2020
1 parent 31b99a9 commit 10293c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,25 @@ describe('api: watch', () => {
expect(dummy).toEqual([1, 2, 2, false])
})

it('watching deep ref', async () => {
const count = ref(0)
const double = computed(() => count.value * 2)
const state = reactive([count, double])

let dummy
watch(
() => state,
state => {
dummy = [state[0].value, state[1].value]
},
{ deep: true }
)

count.value++
await nextTick()
expect(dummy).toEqual([1, 2])
})

it('immediate', async () => {
const count = ref(0)
const cb = jest.fn()
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ function traverse(value: unknown, seen: Set<unknown> = new Set()) {
return value
}
seen.add(value)
if (isArray(value)) {
if (isRef(value)) {
traverse(value.value, seen)
} else if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
traverse(value[i], seen)
}
Expand Down

0 comments on commit 10293c7

Please sign in to comment.