Skip to content

bug(react): viewport tracking rescans and retains removed descendants #380

Description

@vamgan

Summary

React viewport tracking has mutation-scaling and stale-state problems in dynamic/virtualized UIs.

Evidence

packages/react/src/useAskableViewport.ts:

  1. Relevant additions call observeAll(), rescanning every annotation. Sequential inserts become approximately O(existing annotations × mutations).
  2. When an annotated parent is removed, cleanup processes the parent or descendants, not both. Annotated descendants can remain in the visible map.
  3. Changing only scope updates a ref but does not flush output, leaving stale visibleItems until another observer callback.

Proposed fix

  • Observe only each added node plus annotated descendants.
  • On removal, process both an annotated root and every annotated descendant.
  • Flush immediately on scope changes or derive filtered output from unfiltered state.
  • Consider sharing core viewport machinery to avoid duplicate implementations.

Acceptance criteria

  • Removing an annotated parent clears all annotated descendants
  • Scope changes update visible output immediately
  • Adding one node does not rescan the full document
  • Sequential-insertion benchmark counts selector scans and observer registrations
  • Virtualized-list regression coverage is added

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: reactReact adapter workbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions