fix(watch): traverse refs in deep watch (#1939)

ref #1900
This commit is contained in:
Yang Mingshan 2020-08-24 02:41:11 +08:00 committed by GitHub
parent 31b99a9139
commit 10293c7a18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -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()

View File

@ -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)
}