Skip to content

Declared ordering applied over the optimistic overlay #1792

Description

@averypelle

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:

  1. Collection-level: apply compare (or a declarative orderBy on the collection config) to the merged view, optimistic rows included.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions