Skip to content

fix(lit-store): keep TanStackStoreSelector in sync with its host - #421

Open
oyobbeb wants to merge 1 commit into
TanStack:mainfrom
oyobbeb:fix/lit-selector-host-sync
Open

oyobbeb wants to merge 1 commit into
TanStack:mainfrom
oyobbeb:fix/lit-selector-host-sync

Conversation

@oyobbeb

@oyobbeb oyobbeb commented Nov 23, 2026 •

Copy link
Copy Markdown

🎯 Changes

TanStackStoreSelector, and TanStackStoreAtom which wraps it, could get out of sync with its host:

  • hostUpdate() returned early while the store stayed the same. A selector that reads host properties, like (state) => state[this.animal] in the Lit quick start, kept showing the old selection after the property changed.
  • hostDisconnected() unsubscribes, but nothing subscribed again on reconnect. After the element was moved in the DOM (for example by a keyed repeat reorder), store updates were ignored.

This change:

  • runs the selector on each host update, and keeps the previous value when compare says it is equal;
  • requests an update in hostConnected(), so the subscription is set up again after a move;
  • skips subscribing in an update that runs after the host was removed, so a removed element does not stay subscribed;
  • keeps the selected value across a disconnect, so a move does not replace .value when nothing changed.

The select-and-compare step moved into a #select() method that hostUpdate() and the subscription share. With the default === compare, a selector that builds a new object or array now returns a new .value on each host update, as useSelector does in React; compare: shallow keeps it stable.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

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

Summary by CodeRabbit

  • Bug Fixes
    • Store selectors now re-evaluate when their host updates, keeping selected values in sync with host properties.
    • Subscriptions continue working when an element moves in the DOM and stop triggering renders after it is removed.
    • Selections using shallow comparison remain stable when moving an element without changes to the selected value.

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e167e9b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/lit-store Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The selector now reevaluates its selected value on host updates and manages its store subscription as the host connects, moves, or disconnects. Tests cover host property changes, element moves, and removal. A patch changeset records the update.

Changes: Selector synchronization

Layer / File(s) Summary
Selector lifecycle and reevaluation
packages/lit-store/src/tan-stack-store-selector.ts, packages/lit-store/tests/selector.test.ts, .changeset/lit-selector-host-sync.md
The selector tracks whether its host is connected, reevaluates the selection on host updates, and requests updates when the selected value changes. It clears its subscription on disconnection while retaining the cached selection. Tests cover host property changes, moves, and removal. The changeset records the patch.

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

Change: Bug fix

Suggested reviewers: crutchcorn, kevinvandy

Merge Risk: Minimal · up to e167e

The selector now reruns on host updates and resubscribes when the host moves or reconnects. It does not subscribe once the host has been removed. Source tracing and a bounded model check found no stale selection or leaked subscription, and tests and a changeset accompany the change. No concrete merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: TanStackStoreSelector is kept in sync with its host. The changeset covers the selector re-running on host updates, resubscribing on reconnect, and skipping subscriptio…
Description check Passed The description follows the template. It has a Changes section that explains the motivation and the fix, with the stale-selection and missed-update problems described. The checklist is completed. The …
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.
Full details: Title check

Explanation

The title names the main change: TanStackStoreSelector is kept in sync with its host. The changeset covers the selector re-running on host updates, resubscribing on reconnect, and skipping subscriptions after removal. The fix(lit-store) prefix is a conventional commit form and names the affected package.

Full details: Description check

Explanation

The description follows the template. It has a Changes section that explains the motivation and the fix, with the stale-selection and missed-update problems described. The checklist is completed. The release impact section marks the changeset as generated. The description is thorough and specific.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between f333206 and e167e9b.

Files selected for processing: 3

  • .changeset/lit-selector-host-sync.md
  • packages/lit-store/src/tan-stack-store-selector.ts
  • packages/lit-store/tests/selector.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

hostUpdate returned early while the store stayed the same, so a selector
that reads host properties kept its old value. After the host was moved
in the DOM, nothing subscribed again, so later store updates were
ignored.

The selector now runs again on each host update, and the subscription
follows the host: it is set up again on reconnect, and an update that
runs after the host is removed no longer subscribes. The selected value
is kept across a move, so compare still decides when it changes.
@oyobbeb
oyobbeb force-pushed the fix/lit-selector-host-sync branch from 10d5c85 to e167e9b Compare November 23, 2026 05:50
@oyobbeb
oyobbeb marked this pull request as ready for review November 23, 2026 08:43
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