Skip to content

fix(reactivity): trigger immediate watch for empty sources#14928

Closed
leno23 wants to merge 1 commit into
vuejs:mainfrom
leno23:fix-14898-watch-empty-array-immediate
Closed

fix(reactivity): trigger immediate watch for empty sources#14928
leno23 wants to merge 1 commit into
vuejs:mainfrom
leno23:fix-14898-watch-empty-array-immediate

Conversation

@leno23
Copy link
Copy Markdown
Contributor

@leno23 leno23 commented Jun 5, 2026

Fixes #14898.\n\nWhen watching an empty array of sources with immediate: true, the multi-source change check used Array#some(), which never passes for an empty array. Treat the first immediate job as a callback trigger so empty multi-source watchers follow the same immediate semantics as other sources.\n\nTest: pnpm vitest packages/runtime-core/tests/apiWatch.spec.ts --run

Summary by CodeRabbit

  • Bug Fixes
    • Fixed the watch API to properly invoke callbacks when the immediate option is enabled, ensuring callbacks execute as expected during initial setup.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 5, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7afadd80-f4f4-4c35-a0f4-81fecde4c86a

📥 Commits

Reviewing files that changed from the base of the PR and between 9d92dbd and a426cd3.

📒 Files selected for processing (2)
  • packages/reactivity/src/watch.ts
  • packages/runtime-core/__tests__/apiWatch.spec.ts

📝 Walkthrough

Walkthrough

The watch callback now runs immediately when immediate: true even if the source array is empty or no change is detected on the first execution. The job condition checks immediateFirstRun to invoke the callback during the initial scheduled run, and a test validates this behavior.

Changes

Watch immediate execution fix

Layer / File(s) Summary
Job condition update and immediate execution test
packages/reactivity/src/watch.ts, packages/runtime-core/__tests__/apiWatch.spec.ts
The watch job callback's decision condition now includes immediateFirstRun to ensure the callback executes on immediate-first scheduled execution even when change-detection would skip it. A test verifies that watch([], cb, { immediate: true }) invokes the callback with empty arrays.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • vuejs/core#12296: Both PRs modify the watch() job logic that governs when the callback (cb) is executed and what value state it observes.

Suggested labels

ready to merge, scope: reactivity, :hammer: p3-minor-bug

Poem

🐰 An empty array now watches bright,
With immediate: true, it runs just right,
No changes needed, the callback fires,
Empty sources grant their desires,
Variables fade, but semantics stay right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically describes the main fix: triggering immediate watch callbacks for empty source arrays.
Linked Issues check ✅ Passed The code changes directly address the requirement from issue #14898: enabling immediate callbacks to run when watching empty source arrays.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to fixing the immediate watch behavior for empty sources and include a corresponding test case.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 5, 2026

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 106 kB (+3 B) 40.1 kB (+5 B) 36 kB (-2 B)
vue.global.prod.js 164 kB (+3 B) 60.1 kB (+9 B) 53.5 kB (-31 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 48.8 kB 19 kB 17.4 kB
createApp 56.9 kB (+3 B) 22 kB (+9 B) 20.1 kB (+14 B)
createSSRApp 61.2 kB (+3 B) 23.8 kB (+10 B) 21.7 kB (+3 B)
defineCustomElement 63.1 kB (+3 B) 23.9 kB (+9 B) 21.8 kB (+3 B)
overall 71.7 kB (+3 B) 27.4 kB (+4 B) 25 kB (+8 B)

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jun 5, 2026

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@14928
npm i https://pkg.pr.new/@vue/compiler-core@14928
yarn add https://pkg.pr.new/@vue/compiler-core@14928.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@14928
npm i https://pkg.pr.new/@vue/compiler-dom@14928
yarn add https://pkg.pr.new/@vue/compiler-dom@14928.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@14928
npm i https://pkg.pr.new/@vue/compiler-sfc@14928
yarn add https://pkg.pr.new/@vue/compiler-sfc@14928.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@14928
npm i https://pkg.pr.new/@vue/compiler-ssr@14928
yarn add https://pkg.pr.new/@vue/compiler-ssr@14928.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@14928
npm i https://pkg.pr.new/@vue/reactivity@14928
yarn add https://pkg.pr.new/@vue/reactivity@14928.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@14928
npm i https://pkg.pr.new/@vue/runtime-core@14928
yarn add https://pkg.pr.new/@vue/runtime-core@14928.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@14928
npm i https://pkg.pr.new/@vue/runtime-dom@14928
yarn add https://pkg.pr.new/@vue/runtime-dom@14928.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@14928
npm i https://pkg.pr.new/@vue/server-renderer@14928
yarn add https://pkg.pr.new/@vue/server-renderer@14928.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@14928
npm i https://pkg.pr.new/@vue/shared@14928
yarn add https://pkg.pr.new/@vue/shared@14928.tgz

vue

pnpm add https://pkg.pr.new/vue@14928
npm i https://pkg.pr.new/vue@14928
yarn add https://pkg.pr.new/vue@14928.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@14928
npm i https://pkg.pr.new/@vue/compat@14928
yarn add https://pkg.pr.new/@vue/compat@14928.tgz

commit: a426cd3

@leno23 leno23 marked this pull request as ready for review June 5, 2026 09:13
@edison1105
Copy link
Copy Markdown
Member

duplicate of #14914

@edison1105 edison1105 closed this Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

watch([], cb, { immediate: true }) // cb not runing as expected

2 participants