Skip to content
This repository was archived by the owner on Nov 24, 2026. It is now read-only.

feat(db): expose an acknowledged state for optimistic mutations - #2

Draft
mhsnook wants to merge 1 commit into
mainfrom
claude/optimistic-acknowledge-layer
Draft

mhsnook wants to merge 1 commit into
mainfrom
claude/optimistic-acknowledge-layer

Conversation

@mhsnook

@mhsnook mhsnook commented Jun 27, 2026 •

Copy link
Copy Markdown
Owner

Discussion PR — depth 1 of 2. This is the minimal, strictly additive "acknowledge layer." A companion PR adds the optional settleWith handler-decoupling on top, which may be a better pattern to move to overall. Opening both so we can compare. Design write-up: docs/proposals/acknowledged-and-settled.md.

The idea

An optimistic mutation against a realtime-sync backend has two confirmations, not one:

  • acknowledged (A) — the server accepted the write (the POST/RPC resolved; the txid is known).
  • settled (S) — the change echoed back through sync, so the optimistic overlay can be dropped with no flicker.

Today both are awaited inside one handler, so every UI signal ($synced, isPersisted) flips only at S. But for most UIs A is the signal that matters — once the server has the write, drop the spinner. The library already tracks A internally (recomputeOptimisticState separates active from completed-but-not-settled overlays); this PR just exposes it.

What this adds (all additive, non-breaking)

Core — @tanstack/db:

  • Transaction.acknowledge() — a no-op-safe setter a collection adapter calls when the server confirms a write.
  • Transaction.isAcknowledged — resolves at the ack; resolves together with isPersisted when no adapter calls acknowledge(); rejects on failure. Always safe to await, never resolves later than isPersisted.
  • $acknowledged virtual property — true once acknowledged, always true when $synced is true. Wired through row enrichment, the virtual-prop cache, and group-by aggregation, and emitted as a virtual-prop-only update when it flips mid-flight.

Adapter — @tanstack/electric-db-collection:

  • The wrapped insert/update/delete handlers call transaction.acknowledge() after the server responds, before awaiting the txid. One line each.

Note: It is entirely up to the Electric SQL and Tanstack folks whether to make this change. I included it here because it's the clearest demonstration of the new feature.

// AFTER — one added line; non-breaking
const wrappedOnInsert = async ({ transaction, ...p }) => {
  const result = await config.onInsert!({ transaction, ...p }) // ack
  transaction.acknowledge()                                    // ← flips $acknowledged now
  await processMatchingStrategy(result)                        // settle (still blocks)
  return result                                                // isPersisted STILL at settle — unchanged
}

Why it's non-breaking at every layer

  • isPersisted / $synced are untouched — the handler still blocks on the echo, so their timing is identical to today. The snappiness lives entirely on the new $acknowledged / isAcknowledged, which didn't exist before.
  • A collection adapter that doesn't adopt it: no change. An app developer who doesn't read the new signal: no change.
  • Adopting is one line for the adapter, and its users opt in by reading $acknowledged.

Consuming it

// imperative
const tx = todos.insert(draft)
await tx.isAcknowledged   // fires at the ack; ~a sync round-trip earlier than isPersisted
toast.success(`Saved`)

// reactive, three phases
const label =
  !row.$acknowledged ? `Saving…`
  : !row.$synced     ? `Saved`   // server has it; quietly reconciling
  :                    `Saved`   // fully settled

Tests

  • packages/db/tests/transaction-acknowledge.test.ts (new): ack flips $acknowledged while persisting without settling; emits a virtual-prop-only update; ack/settle coincide when no adapter calls acknowledge(); failure rejects isAcknowledged; synced rows are acknowledged; idempotent.
  • Updated the hasVirtualProps shape test and the virtual-prop strip helpers for the new prop.

Verification: @tanstack/db typechecks clean, builds, and passes the full suite (2406 passed). @tanstack/electric-db-collection passes (463 passed). The only tsc errors in the electric package are pre-existing Cannot find module '@tanstack/electric-db-collection' from the unbuilt e2e suite, present on main.

🤖 Generated with Claude Code


Generated by Claude Code

@mhsnook
mhsnook force-pushed the claude/optimistic-acknowledge-layer branch 2 times, most recently from b7ee273 to f59e804 Compare June 27, 2026 09:06
@mhsnook
mhsnook force-pushed the claude/optimistic-acknowledge-layer branch from f59e804 to d968805 Compare June 27, 2026 10:09
@mhsnook mhsnook changed the title feat(db): expose an acknowledged state for optimistic mutations (depth 1) feat(db): expose an acknowledged state for optimistic mutations Jun 27, 2026
@mhsnook
mhsnook force-pushed the claude/optimistic-acknowledge-layer branch from d968805 to c1b387d Compare June 27, 2026 14:18
Adds an additive 'acknowledged' milestone between optimistic and synced, so UIs
can respond when the server accepts a write (the acknowledgement) without waiting
for it to sync back.

Core (@tanstack/db):
- Transaction.acknowledge() + Transaction.isAcknowledged. acknowledge() is a
  no-op-safe setter a collection adapter calls when the server confirms a write.
  isAcknowledged resolves at the acknowledgement, or together with isPersisted
  when no adapter calls it, and rejects on failure — always safe to await and
  never later than isPersisted. isPersisted/$synced semantics and the optimistic
  overlay are unchanged.
- New $acknowledged virtual property (true once acknowledged, always true when
  $synced). Wired through row enrichment, the virtual-prop cache, group-by
  aggregation, and emitted as a virtual-prop-only update when it flips mid-flight.

Adapter (@tanstack/electric-db-collection):
- wrapped insert/update/delete handlers call transaction.acknowledge() after the
  server responds, before awaiting the txid. One line each; non-breaking.

Includes docs/proposals/acknowledged-and-settled.md and tests.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_012VvSGi4YvJ5cYtJosqXkMe
@mhsnook
mhsnook force-pushed the claude/optimistic-acknowledge-layer branch from c1b387d to 222eb0e Compare June 27, 2026 14:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants