Skip to content

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

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

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

@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

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

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