core icon indicating copy to clipboard operation
core copied to clipboard

fix(compat): watch arrays as deep: 1

Open skirtles-code opened this issue 7 months ago • 7 comments

  • Fix #11872.
  • Close #12236.

This PR builds on the work done in #12236 to try to make the compat build more accurately emulate Vue 2.

Watching arrays in Vue 2 behaved like deep: 1, rather than deep: true, so mutating nested objects should not trigger the watcher. Support for a numeric depth was added in 3.5 and wasn't available when the compat build was first implemented.

But there are several other inconsistencies in the compat build. Some of these seem to have been introduced by #9927, and worked correctly prior to 3.5:

  • The special handling for arrays should not depend on the initial value of the property, only the current value needs to be an array. In practice, it's quite common for a property to have an initial value of null while data is loading.
  • The special handling is also supposed to apply to this.$watch(), not just the watch option.
  • The compat warning should not be shown when deep: true has been set.

There were relatively few tests for WATCH_ARRAY, so it's understandable that these regressions were introduced. I've added a lot of extra tests to try to avoid future problems.

I've introduced a compatWatchArray flag to the internal watcher options as a way to tell @vue/reactivity that we want the special handling of arrays. This only impacts the equality check, which needs to behave like a deep watcher only if the current value is an array. I didn't want to add this flag to the public types, so there's a bit of as any hackery to access it.

Summary by CodeRabbit

  • New Features

    • Improved compatibility for watching arrays, ensuring watchers are triggered as expected when using legacy array watching behavior in compatibility mode.
  • Bug Fixes

    • Enhanced handling of array watchers to better support legacy scenarios and prevent missed updates.
  • Tests

    • Expanded and reorganized test coverage for array watching compatibility, including more scenarios and dynamic behaviors.

skirtles-code avatar May 25 '25 20:05 skirtles-code

Walkthrough

Compatibility logic for array watching was added and refactored across the reactivity and runtime-core modules. The watch callback triggering conditions were extended to support legacy array semantics under a compatibility flag. The watcher getter logic was centralized and reused, and the test suite for the WATCH_ARRAY deprecation was significantly expanded to cover more scenarios.

Changes

File(s) Change Summary
packages/reactivity/src/watch.ts Extended watch callback triggering to include compat mode with compatWatchArray for arrays.
packages/runtime-core/src/apiWatch.ts Added createCompatWatchGetter for compat array watching; updated instanceWatch for compat array logic.
packages/runtime-core/src/componentOptions.ts Refactored compat array watch logic to use createCompatWatchGetter; made createWatcher internal.
packages/vue-compat/tests/misc.spec.ts Replaced single test with comprehensive suite for WATCH_ARRAY deprecation and compat behaviors.

Sequence Diagram(s)

sequenceDiagram
    participant Component
    participant Watcher
    participant CompatGetter
    participant Callback

    Component->>Watcher: Register watch on array property (compat mode)
    Watcher->>CompatGetter: Get value (array)
    CompatGetter->>Watcher: Return deeply traversed array (depth 1)
    Watcher->>Callback: Invoke callback if compatWatchArray flag and array detected

Poem

A bunny with code in its paws,
Watches arrays with legacy laws.
Compat flags set, old flows revive,
Deep or shallow, the tests now thrive.
With every tick, the watcher’s aware—
Arrays in harmony, handled with care!
🐇✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar May 25 '25 20:05 coderabbitai[bot]

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13377
@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13377
@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13377
@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13377
@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13377
@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13377
@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13377
@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13377
@vue/shared

npm i https://pkg.pr.new/@vue/shared@13377
vue

npm i https://pkg.pr.new/vue@13377
@vue/compat

npm i https://pkg.pr.new/@vue/compat@13377

commit: 351ce06

pkg-pr-new[bot] avatar May 25 '25 20:05 pkg-pr-new[bot]

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.3 kB 34.5 kB
vue.global.prod.js 159 kB 58.5 kB 52 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.8 kB 23 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24.1 kB

github-actions[bot] avatar May 25 '25 20:05 github-actions[bot]

/ecosystem-ci run

edison1105 avatar May 25 '25 23:05 edison1105

📝 Ran ecosystem CI: Open

suite result latest scheduled
pinia :white_check_mark: success :white_check_mark: success
nuxt :white_check_mark: success :white_check_mark: success
language-tools :white_check_mark: success :white_check_mark: success
radix-vue :white_check_mark: success :white_check_mark: success
primevue :white_check_mark: success :white_check_mark: success
vitepress :white_check_mark: success :white_check_mark: success
quasar :white_check_mark: success :white_check_mark: success
vueuse :white_check_mark: success :white_check_mark: success
vue-i18n :white_check_mark: success :white_check_mark: success
vite-plugin-vue :white_check_mark: success :white_check_mark: success
vant :white_check_mark: success :white_check_mark: success
router :white_check_mark: success :white_check_mark: success
test-utils :white_check_mark: success :white_check_mark: success
vue-simple-compiler :white_check_mark: success :white_check_mark: success
vuetify :white_check_mark: success :white_check_mark: success
vue-macros :white_check_mark: success :white_check_mark: success

vue-bot avatar May 26 '25 00:05 vue-bot

@skirtles-code the tests are all green after re-run. I think the document https://v3-migration.vuejs.org/breaking-changes/watch.html should also be updated. It is also possible that it should be merged into the minor branch This PR looks good to me.

edison1105 avatar May 26 '25 00:05 edison1105