Skip to content

fix(watch): use maximum depth for duplicates #13434

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

skirtles-code
Copy link
Contributor

@skirtles-code skirtles-code commented Jun 4, 2025

While recursing for deep watchers, we keep track of which objects have already been 'seen', to avoid duplicated effort and prevent cycles. But currently we don't take account of the depth.

If we encounter an object twice during the traversal, with different values for depth, we need to traverse it at the greater depth.

I think the fix for this is relatively simple, using a Map instead of a Set to store the seen objects. If we encounter an object again then we may need to traverse it again if the previous traversal wasn't deep enough.

While this may lead to wasted effort, traversing the same object multiple times, in practice it happens very rarely. It needs a pretty specific data structure and the value passed for deep needs to be an unusually large but finite number. The simplest broken example I could find uses deep: 3:

I did briefly test the performance impact, but I didn't see a noticeable difference.

#13433 would also fix this problem, because it processes the items in depth order. But I think that PR may prove difficult to merge.

Summary by CodeRabbit

  • Tests

    • Added a new test to verify that watchers with different depth levels respond appropriately to changes in deeply nested reactive arrays.
  • Refactor

    • Improved internal handling of deep watching by refining how nested values are tracked, enhancing the accuracy of change detection for different watcher depths.

Copy link

coderabbitai bot commented Jun 4, 2025

Walkthrough

The traverse function in the reactivity system was refactored to use a Map for tracking seen values and their traversal depths instead of a Set. Correspondingly, a new test was added to verify the behavior of watchers with varying deep levels, ensuring correct triggering based on nested mutations.

Changes

File(s) Change Summary
packages/reactivity/src/watch.ts Refactored traverse to use Map<unknown, number> for seen parameter; updated logic accordingly.
packages/runtime-core/tests/apiWatch.spec.ts Added test for watching the same object at different depths with various deep options.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Watcher
    participant ReactiveArray

    User->>Watcher: Set up watchers with deep: 1, 2, 3, 4
    User->>ReactiveArray: Mutate nested properties at various depths
    ReactiveArray-->>Watcher: Notify relevant watchers based on deep level
    Watcher-->>User: Trigger callbacks for matching deep levels
Loading

Poem

In the warren of code, a Map takes the stage,
Remembering depths as we traverse each page.
Watchers now listen with ears tuned just right,
Deep or shallow, they trigger on sight.
With each nested hop, our tests leap and bound—
Hooray for new depths that the rabbits have found! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e53a4ff and a004e23.

📒 Files selected for processing (2)
  • packages/reactivity/src/watch.ts (1 hunks)
  • packages/runtime-core/__tests__/apiWatch.spec.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/reactivity/src/watch.ts (3)
packages/shared/src/general.ts (1)
  • isObject (53-54)
packages/runtime-core/src/index.ts (1)
  • ReactiveFlags (210-210)
packages/reactivity/src/index.ts (1)
  • ReactiveFlags (82-82)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (3)
packages/reactivity/src/watch.ts (2)

334-334: LGTM: Parameter type change enables depth tracking.

The type change from Set<unknown> to Map<unknown, number> correctly supports storing both the traversed object and its depth, which is essential for the fix.


340-344: LGTM: Depth-aware traversal logic correctly implemented.

The implementation properly handles depth tracking:

  • Map initialization is correct
  • The condition (seen.get(value) || 0) >= depth ensures objects are re-traversed when encountered at greater depths
  • Storing seen.set(value, depth) maintains the maximum depth for each object

This elegantly solves the issue where objects encountered multiple times at different depths might miss deeper nested changes.

packages/runtime-core/__tests__/apiWatch.spec.ts (1)

1692-1741: Excellent test coverage for the depth-aware watcher fix.

This test case comprehensively validates the fix by:

  • Creating a complex nested structure where the same object (arr3) appears at multiple depths
  • Setting up watchers with varying deep levels (1-4) on the same root array
  • Testing mutations at different nesting levels and verifying selective watcher triggering
  • Ensuring shallow watchers don't trigger on deep mutations while deeper watchers do

The test scenarios effectively demonstrate that the fix resolves the issue where objects encountered at different depths now properly trigger watchers based on their configured depth sensitivity.

✨ 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.

Copy link

github-actions bot commented Jun 4, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+10 B) 38.3 kB (+8 B) 34.6 kB (+23 B)
vue.global.prod.js 159 kB (+10 B) 58.5 kB (+8 B) 52 kB (-74 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.6 kB
createApp 54.6 kB (+10 B) 21.2 kB (+8 B) 19.4 kB (+11 B)
createSSRApp 58.8 kB (+10 B) 23 kB (+6 B) 20.9 kB (+4 B)
defineCustomElement 59.5 kB (+10 B) 22.8 kB (+9 B) 20.8 kB (+1 B)
overall 68.7 kB (+10 B) 26.4 kB (+5 B) 24 kB (-68 B)

Copy link

pkg-pr-new bot commented Jun 4, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13434

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

npm i https://pkg.pr.new/vue@13434

@vue/compat

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

commit: a004e23

@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: reactivity labels Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to merge The PR is ready to be merged. scope: reactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants