Skip to content

Fix/live query record drop on subset unmount#1656

Open
jdday wants to merge 1 commit into
TanStack:mainfrom
jdday:fix/live-query-record-drop-on-unmount
Open

Fix/live query record drop on subset unmount#1656
jdday wants to merge 1 commit into
TanStack:mainfrom
jdday:fix/live-query-record-drop-on-unmount

Conversation

@jdday

@jdday jdday commented Jul 8, 2026

Copy link
Copy Markdown

🎯 Changes

When a row was first inserted by subset query A, write({ type: 'insert' }) cleared the row's pending metadata. setPersistedOwners was called before write, so the ownership it set was immediately overwritten by the insert's metadata-delete.

When a later live query called getHydratedOwnedRowsForQueryBaseline, it read the stale persisted state and overwrote the in-memory rowToQueries map, dropping query A's hash from the owner set. When that query was GC'd, it found zero remaining owners and deleted the row — removing it from unrelated list views.

✅ Checklist

  • [ x ] I have tested this code locally with pnpm test.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Notes

Currently facing the below issue with react-db and I believe the below re-ordering of lines fixes it for myself but need to understand the impact elsewhere or if there is a better place to add this fix.

  • Live query A contains a list of rows
  • Live query B contains a subset of those rows with a different where clause
  • When Live Query B unmounts then the rows are removed from the collection completely even with Live Query A still mounted and requires those rows.,

Any new live queries seem to take ownership of existing rows when mounting. Meaning that when the new live query unmounts it causes that record to be removed from the collection even if other useLiveQueries still should have that row existing.

I am unsure if this is a similar impact across the other vue/svelte db packages I am currently only using react-db so any advise if this change would negatively impact other areas would be helpful.

One thing noticed - my team have observed that the row will re-appear in the correct live query if we force a re-render during local development

Summary by CodeRabbit

  • Bug Fixes
    • Improved record syncing to ensure the correct insert/update behavior when the row already exists locally.
    • Fixed an ownership/sync ordering issue so records remain accurately tracked after updates.
    • Prevented unmounting a filtered live view from impacting records shown in a still-mounted broader list view.
  • Tests
    • Added coverage to verify that unmounting a narrow useLiveQuery does not remove records from an active broad useLiveQuery.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR reorders row tracking and ownership persistence in applySuccessfulResult, and adds a regression test showing that unmounting a narrow useLiveQuery does not remove records from a still-mounted broad query.

Changes

Query Collection Ownership Fix

Layer / File(s) Summary
Row writes and ownership persistence
packages/query-db-collection/src/query.ts
applySuccessfulResult now adds the row before write selection, chooses insert versus update from current synced state and prior ownership, and re-reads persisted owners after the write before updating ownership metadata.
Concurrent useLiveQuery regression test
packages/react-db/tests/useLiveQuery.test.tsx
Adds a test that mounts broad and narrow queries over one collection, unmounts the narrow query, waits for GC, and verifies the broad query still retains all items.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • TanStack/db#1636: Related useLiveQuery lifecycle coverage around unmount behavior and retained query state.

Suggested reviewers: kevin-dp, KyleAMathews

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description has the required sections, but the Release Impact section is incomplete because neither option is selected. Select whether this change affects published code or is docs/CI/dev-only; if it affects published code, add a changeset.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately describes the main fix: live query records no longer drop when a subset query unmounts.
✨ 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.

@jdday jdday force-pushed the fix/live-query-record-drop-on-unmount branch from 775e89d to 66f109f Compare July 8, 2026 06:48
…w updates, as it was previously reading stale owners before any updates causing rows contained within 2 subsets to be removed on unmount
@jdday jdday force-pushed the fix/live-query-record-drop-on-unmount branch from 66f109f to fb9fa21 Compare July 8, 2026 06:49
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.

1 participant