Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): watching multiple values - handle undefined as initial values (fix: #5032) #5033

Merged
merged 3 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test seems not testing the change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right , I'm only testing it's being called initially (which previously it wasn't) but I need to validate the arguments passed.

That actually made me find a part my fix did not cover - oldValue was not undefined when it should be.

},
{ 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