Outcome
Rows from a pending optimistic transaction should appear at their sorted position, not after all synced rows. Today every adapter that wants that must re-sort each read result; the query engine could own it incrementally.
Behavior
With compare set on a collection, synced rows iterate in order, but rows from a pending optimistic transaction are appended after them. During a write's round trip, the user's own row sits at the end of the list, then moves to its sorted position when the write settles.
const collection = createCollection<{ id: string; rank: string }, string>({
id: 'demo',
getKey: (r) => r.id,
compare: (a, b) => (a.rank < b.rank ? -1 : a.rank > b.rank ? 1 : 0),
startSync: true,
sync: {
sync: ({ begin, write, commit, markReady }) => {
begin();
write({ type: 'insert', value: { id: 'a', rank: 'a1' } });
write({ type: 'insert', value: { id: 'c', rank: 'a3' } });
commit();
markReady();
},
},
});
const tx = createTransaction({ autoCommit: true, mutationFn: () => new Promise(() => {}) });
tx.mutate(() => collection.insert({ id: 'b', rank: 'a2' }));
console.log(collection.toArray.map((r) => r.id));
// actual: ['a', 'c', 'b'] — the pending row is appended
// expected: ['a', 'b', 'c'] — placed by the comparator
Ask
Either of:
- Collection-level: apply
compare (or a declarative orderBy on the collection config) to the merged view, optimistic rows included.
- Query-level: document that
orderBy on a live query sorts the overlay-merged rows, and provide a way to derive a query's orderBy from the source collection's declared ordering so the order is stated once.
Both remove the same duplication: today the ordering lives in compare and again in every consumer that re-sorts.
Outcome
Rows from a pending optimistic transaction should appear at their sorted position, not after all synced rows. Today every adapter that wants that must re-sort each read result; the query engine could own it incrementally.
Behavior
With
compareset on a collection, synced rows iterate in order, but rows from a pending optimistic transaction are appended after them. During a write's round trip, the user's own row sits at the end of the list, then moves to its sorted position when the write settles.Ask
Either of:
compare(or a declarativeorderByon the collection config) to the merged view, optimistic rows included.orderByon a live query sorts the overlay-merged rows, and provide a way to derive a query'sorderByfrom the source collection's declared ordering so the order is stated once.Both remove the same duplication: today the ordering lives in
compareand again in every consumer that re-sorts.