Skip to content

Commit

Permalink
fix: handle undefined as initial value when watching multiple values
Browse files Browse the repository at this point in the history
fix: #5032
  • Loading branch information
LinusBorg committed Dec 3, 2021
1 parent 2d4f455 commit c474805
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Expand Up @@ -176,6 +176,21 @@ describe('api: watch', () => {
])
})

it('watching multiple sources: undefined initial values and immediate: true', async () => {
const a = ref()
const b = ref()
let called = false
watch(
[a, b],
() => {
called = true
},
{ immediate: true }
)
await nextTick()
expect(called).toBe(true)
})

it('watching multiple sources: readonly array', async () => {
const state = reactive({ count: 1 })
const status = ref(false)
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Expand Up @@ -295,7 +295,9 @@ function doWatch(
return NOOP
}

let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
let oldValue = isMultiSource
? new Array((source as []).length).fill(INITIAL_WATCHER_VALUE)
: INITIAL_WATCHER_VALUE
const job: SchedulerJob = () => {
if (!effect.active) {
return
Expand Down

0 comments on commit c474805

Please sign in to comment.