Ordered, limited live query over a string key commits `loading` first (and `orderBy().findOne()` since 0.11.1)
維護者通常 1 天內回覆
已經有一個關聯 PR 被合併了。
- #2159 來自 @KyleAMathews —— 已合併
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 18/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- react, typescript
研究方向
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.
由索引模型根據 Issue 內容生成。
描述
- 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).
- 主要語言
- TypeScript
- 星號
- 3.9k
- 分支
- 272
- 平均合併
- 1 天 21 分鐘
- 30 天內合併 PR
- 241
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
TanStack/db 的其他 Issue
-
難度 5/5 一週以上 新手友好度 8/100
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 25/100
維護者通常 1 天內回覆
-
難度 5/5 一週以上 新手友好度 5/100
維護者通常 1 天內回覆
-
query-db: a Query refetch that joins a loader's refetch acquisition does not report shifted pages未關閉
難度 5/5 一週以上 新手友好度 8/100
維護者通常 1 天內回覆
-
A live query's preload on an on-demand collection waits for any persisting optimistic transaction未關閉
難度 5/5 一週以上 新手友好度 12/100
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 74/100
QuantumNous/new-api#7839 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
bug p2 triaged
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
test: make the #3408 Claude OAuth token fixture obviously fake so secret scanners stop flagging it未關閉
難度 2/5 1-3 小時 新手友好度 76/100
code-yeongyu/senpi#3436 ·
維護者通常 1 天內回覆
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
難度 1/5 1 小時以內 新手友好度 78/100
lingdojo/kana-dojo#32505 · 1 則留言 · 5 個 reaction ·
維護者通常 1 天內回覆