Skip to content
Source
Sign inSign up
TanStack/db

fix(db): emit change on order-only reorder in ordered live queries #1601

Closedv-anton(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/1601/headopened

Showing 5 changed files with 78 additions and 0 deletions (loading 2).

x
0 of 5 viewed, 5 left
5 additions and 0 deletions.changeset/orderby-reorder-emission.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+"@tanstack/db": patch
+---
+​
+Fix live queries with `orderBy` not emitting a change when a row's position changes but its projected value does not. Previously, a query like `q.from(...).orderBy((r) => r.value).select((r) => ({ id: r.id }))` would silently keep the stale order after a reorder (the collection's value-diff suppressed the move because the projected value was unchanged), so `useLiveQuery` rendered the old order. Order-only moves are now detected from the orderBy operator's retract/insert pair and emitted directly, with no effect on collections without `orderBy` and no double-emit when the sort field is part of the projection.
68 additions and 0 deletionspackages/db/src/query/live/collection-config-builder.tsModified
View file
@@ -5,6 +5,7 @@
compileQuery,
} from '../compiler/index.js'
import { createCollection } from '../../collection/index.js'
+import { deepEquals } from '../../utils.js'
import {
MissingAliasInputsError,
SetWindowRequiresOrderByError,
@@ -797,6 +798,11 @@
existing.orderByIndex = changes.orderByIndex
+ // Keep the previous value/index from the retract side (the old row)
+ if (changes.deletes > 0 && changes.previousValue !== undefined) {
+ existing.previousValue = changes.previousValue