From e167e9bccdd627ceb7c42db497ac16575c60630a Mon Sep 17 00:00:00 2001 From: Yeongbin Jeong Date: Mon, 23 Nov 2026 14:10:56 +0900 Subject: [PATCH] fix(lit-store): keep TanStackStoreSelector in sync with its host 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. --- .changeset/lit-selector-host-sync.md | 5 + .../lit-store/src/tan-stack-store-selector.ts | 37 +++++--- packages/lit-store/tests/selector.test.ts | 94 ++++++++++++++++++- 3 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 .changeset/lit-selector-host-sync.md diff --git a/.changeset/lit-selector-host-sync.md b/.changeset/lit-selector-host-sync.md new file mode 100644 index 00000000..f0bdd6ac --- /dev/null +++ b/.changeset/lit-selector-host-sync.md @@ -0,0 +1,5 @@ +--- +'@tanstack/lit-store': patch +--- + +Keep `TanStackStoreSelector` and `TanStackStoreAtom` in sync with their host. The selector runs again on each host update, so selectors that read host properties stay current, and the subscription follows the host when it is moved in the DOM or removed. diff --git a/packages/lit-store/src/tan-stack-store-selector.ts b/packages/lit-store/src/tan-stack-store-selector.ts index f9b62901..2422f948 100644 --- a/packages/lit-store/src/tan-stack-store-selector.ts +++ b/packages/lit-store/src/tan-stack-store-selector.ts @@ -68,6 +68,7 @@ export class TanStackStoreSelector< #subscribedStore?: SelectionSource #hasSelected = false #lastSelected?: TSelected + #connected = false constructor( host: ReactiveControllerHost, @@ -86,9 +87,19 @@ export class TanStackStoreSelector< return this.#lastSelected } + hostConnected() { + this.#connected = true + this.#host.requestUpdate() + } + hostUpdate() { + // An update can still run after the host is removed; do not subscribe then. + if (!this.#connected) return const store = this.#getStore() - if (store === this.#subscribedStore) return + if (store && store === this.#subscribedStore) { + this.#select(store.get()) + return + } this.#unsubscribe?.() this.#subscribedStore = store if (!store) { @@ -97,24 +108,26 @@ export class TanStackStoreSelector< this.#lastSelected = undefined return } - this.#lastSelected = this.#selector(store.get()) - this.#hasSelected = true + this.#select(store.get()) this.#unsubscribe = store.subscribe((value) => { - const next = this.#selector(value) - if (this.#hasSelected && this.#compare(this.#lastSelected!, next)) { - return - } - this.#lastSelected = next - this.#hasSelected = true - this.#host.requestUpdate() + if (this.#select(value)) this.#host.requestUpdate() }).unsubscribe } hostDisconnected() { + this.#connected = false this.#unsubscribe?.() this.#unsubscribe = undefined this.#subscribedStore = undefined - this.#hasSelected = false - this.#lastSelected = undefined + } + + #select(value: TSource) { + const next = this.#selector(value) + if (this.#hasSelected && this.#compare(this.#lastSelected!, next)) { + return false + } + this.#lastSelected = next + this.#hasSelected = true + return true } } diff --git a/packages/lit-store/tests/selector.test.ts b/packages/lit-store/tests/selector.test.ts index 6bd56ce7..a2302184 100644 --- a/packages/lit-store/tests/selector.test.ts +++ b/packages/lit-store/tests/selector.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest' import '@testing-library/jest-dom/vitest' import { userEvent } from '@testing-library/user-event' import { LitElement, html } from 'lit' -import { createStore } from '@tanstack/store' +import { createStore, shallow } from '@tanstack/store' import { TanStackStoreSelector } from '../src' import { defineOnce, mount } from './utils' @@ -123,4 +123,96 @@ describe('Lit Store Tests', () => { expect(getBtn()).toHaveTextContent('1') expect(getIgnore()).toHaveTextContent('1') }) + + it('should select again when a host property read by the selector changes', async () => { + const store = createStore({ dogs: 3, cats: 7 }) + + class TestForm extends LitElement { + static properties = { animal: {} } + animal: 'dogs' | 'cats' = 'dogs' + + #count = new TanStackStoreSelector( + this, + () => store, + (state) => state[this.animal], + ) + + render() { + return html`

${this.#count.value}

` + } + } + + const tag = defineOnce('test-form', TestForm) + + const element = await mount(tag) + + const getCount = () => element.shadowRoot!.querySelector('#count') + + expect(getCount()).toHaveTextContent('3') + + element.animal = 'cats' + await element.updateComplete + expect(getCount()).toHaveTextContent('7') + }) + + it('should keep updating after the element is moved', async () => { + const counter = createStore({ count: 0 }) + + class TestForm extends LitElement { + selection = new TanStackStoreSelector( + this, + () => counter, + (state) => ({ count: state.count }), + { compare: shallow }, + ) + + render() { + return html`

${this.selection.value?.count}

` + } + } + + const tag = defineOnce('test-form', TestForm) + + const element = await mount(tag) + + const getCount = () => element.shadowRoot!.querySelector('#count') + + expect(getCount()).toHaveTextContent('0') + + const selected = element.selection.value + document.body.appendChild(element) + await element.updateComplete + expect(element.selection.value).toBe(selected) + + counter.setState(() => ({ count: 1 })) + await element.updateComplete + expect(getCount()).toHaveTextContent('1') + }) + + it('should stop updating after the element is removed', async () => { + const counter = createStore(0) + let renders = 0 + + class TestForm extends LitElement { + #count = new TanStackStoreSelector(this, () => counter) + + render() { + renders++ + return html`

${this.#count.value}

` + } + } + + const tag = defineOnce('test-form', TestForm) + + const element = await mount(tag) + + counter.setState(() => 1) + element.remove() + await element.updateComplete + const rendersAfterRemove = renders + + counter.setState(() => 2) + await element.updateComplete + expect(renders).toBe(rendersAfterRemove) + }) })