Ordered, limited live query over a string key commits `loading` first (and `orderBy().findOne()` since 0.11.1)
Mantenedores costumam responder em até 1 dia
Um pull request relacionado já foi integrado.
- #2159 de @KyleAMathews — integrado
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 18/100
- Tipo de issue
- Bug
- Clareza
- Razoavelmente clara
- Status de atividade
- Estagnada
- Stack de tecnologia
- react, typescript
Direção de pesquisa
The fallback is in OrderedSourceLoader.loadBoundary, and observe() decides sync versus async around isInitialFullSource and canRetryRepair in the same loader file. Run the repro test in packages/react-db/tests to see the loading-then-ready commit. Done when a string-ordered limit or findOne is ready in the first commit and the db test suite stays green, but PR #2159 already targets this and the fix needs maintainer-level understanding of the sync cut.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
- I've validated the bug against the latest version of DB packages (
@tanstack/db0.14.0,@tanstack/react-db0.5.9)
Describe the bug
An ordered, limited live query whose first orderBy term is a string commits loading with no rows on its first render, even when the source already holds every row and answers synchronously. It becomes ready one task later. The same query ordered by a number is ready in the first commit, which is what #1896 set out to guarantee.
Since 0.11.1 this also hits orderBy(...).findOne(), because #1968 made findOne() imply limit(1) (compiler/order-by.ts). On 0.11.0, orderBy(stringField).findOne() was ready in the first commit; from 0.11.1 it commits loading first. Ordering by an ISO date string and taking the latest row is a common shape, so screens that branch on "no row yet" render their empty state for a frame.
Cause
With locale string collation, canExpressCursorOrder cannot express the boundary as a cursor, so OrderedSourceLoader.loadBoundary falls back to loadFullSource(). observe() treats every full-source request as an authoritative repair. Only a full-source request made before any request has settled (isInitialFullSource) shares the initial synchronous cut, so this fallback, which is still part of the initial load, settles asynchronously.
Logged observe() decisions for the same query:
number: ordered (sync) -> boundary (sync) => ready in first commit
string: ordered (sync) -> full-source (async) => loading, then ready
To Reproduce
packages/react-db/tests (jsdom, @testing-library/react):
import { renderHook } from '@testing-library/react'
import { useLayoutEffect } from 'react'
import { createCollection, localOnlyCollectionOptions } from '@tanstack/db'
import { expect, it } from 'vitest'
import { useLiveQuery } from '../src/useLiveQuery'
it(`publishes a synchronous ordered limit over a string key in the first layout commit`, () => {
const source = createCollection(
localOnlyCollectionOptions<{ id: string; at: string }, string>({
getKey: (row) => row.id,
initialData: [
{ id: `1`, at: `2026-10-01` },
{ id: `2`, at: `2026-10-02` },
],
}),
)
const commits: Array<string> = []
renderHook(() => {
const result = useLiveQuery((q) =>
q.from({ row: source }).orderBy(({ row }) => row.at, `desc`).findOne(),
)
useLayoutEffect(() => {
commits.push(`${result.status}:${result.data?.id}`)
})
})
expect(commits[0]).toBe(`ready:2`) // receives `loading:undefined`
})
The same happens with .limit(n) instead of .findOne(), with or without an index on the field (autoIndex: 'eager', BasicIndex), and over eager sync collections as well as local-only ones. Changing at to a number makes it pass.
Expected behavior
A full-source fallback made before the live query is ready is part of the initial load, so it should share the initial synchronous cut like the first full-source request of a requiresFullSource plan.
Possible fix
canRetryRepair (the loader's "live query is ready and no window operation is active" predicate) already distinguishes the initial load:
@@ -624,9 +624,13 @@ export class OrderedSourceLoader {
const orderingInvalidationGeneration = this.orderingInvalidationGeneration
const settlesAsync = result instanceof Promise
// A requiresFullSource plan's first full-source request is its initial
- // load, not a repair, so it shares the initial synchronous cut.
+ // load, not a repair, so it shares the initial synchronous cut. So does a
+ // full-source fallback made before the query is ready, such as a boundary
+ // that a locale string order cannot express as a cursor.
const isInitialFullSource =
- isFullSource && !this.hasSettledSourceRequest && !this.needsOrderingRepair
+ isFullSource &&
+ !this.needsOrderingRepair &&
+ (!this.hasSettledSourceRequest || !this.canRetryRepair())
const canSettleSynchronously =
!settlesAsync &&
windowOperationGeneration === undefined &&
With this change at 0.14.0, the repro passes, all 383 react-db tests pass, and 12,905 of 12,906 db tests pass. The one failure, local-storage-peer-oracle.test.ts > publishes through the default same-tab Storage object, fails identically without the change.
Additional context
Related: #1894 / #1896 (same symptom for numeric keys). We are running this as a local patch on 0.14.0 in a React Native app (Expo, new architecture).
- Linguagem predominante
- TypeScript
- Estrelas
- 3.9k
- Forks
- 272
- Merge médio
- 22h 54min
- PRs com merge (30d)
- 236
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de TanStack/db
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 22/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 12/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 8/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 25/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 5/100
TanStack/db#2186 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
Todas as issues de TanStack/db
Issues semelhantes
-
Endpoint links in the tag operations list are below the 24px target sizeTalvez já em andamento @AdzerKI assumiu hoje. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 1 dia
-
ai-driven-qa bug Minor
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 80/100
linagora/twake-tasks#435 ·
Mantenedores costumam responder em até 1 dia
-
bug domain: database priority: p1 Stellar Wave
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
StellarState/SS-backend#632 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
A2A audit timestamps end in +00:00Z (invalid ISO 8601)Talvez já em andamento @O1sumitkumar assumiu hoje. Abertabug good first issue help wanted python
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 1 dia
-
`cf complete zsh >> .zshrc` fails on vanilla zshTalvez livre de novo Um pull request para esta issue foi fechado sem ser mesclado. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
cloudflare/cf#340 ·
Mantenedores costumam responder em até 1 dia