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

feat(db): isAcknowledged + isSettled (deprecating isPersisted) - #3

Draft
mhsnook wants to merge 3 commits into
mainfrom
claude/optimistic-settle-layer
Draft

mhsnook wants to merge 3 commits into
mainfrom
claude/optimistic-settle-layer

Conversation

@mhsnook

@mhsnook mhsnook commented Jun 27, 2026 •

Copy link
Copy Markdown
Owner

Discussion PR — the fuller option. A smaller companion PR (#2) adds only the acknowledge half; this one builds on it with settleWith and a single name fix. The two are alternatives — only one is meant to land. Design write-up: docs/proposals/acknowledged-and-settled.md.

The idea

Same starting point as #2: an optimistic mutation has two confirmations — acknowledged (server accepted the write) and synced (it echoed back through sync, overlay dropped). Today every signal flips only when synced; the acknowledgement is usually what the UI wants.

Where #2 stops at exposing the acknowledgement, this PR also lets a handler return at the ack and hand the responsibility of waiting for the final sync-and-settle to the framework.

What this adds (additive; soft-deprecates one name)

Core — @tanstack/db:

  • (includes everything from feat(db): expose an acknowledged state for optimistic mutations #2: acknowledge(), isAcknowledged, $acknowledged)
  • Transaction.settleWith(fn): hand the transaction off to a new registry that holds the optimistic overlay open until sync, allowing us to resolve the handler earlier, and give the acknowledgement to the UI and offloading the responsibility for the optimistic overlay to a framework construct.
  • Transaction.isSettled: new alias for isPersisted (soft deprecated). Matches the language of "settleWith" and avoids confusion for situations where the original acknowledgement indicates persistedness. The framework can't know the exact persistence milestone, only when the request resolves and when the sync comes back. You might also call it isSynced to match the naming of $synced virtual prop state.

For side-effects at either milestone (toast, navigation), await tx.isAcknowledged / tx.isSettled. isPersisted still exists but is now unclear on which event it refers to.

Adapter — @tanstack/electric-db-collection:

const wrappedOnInsert = async ({ transaction, ...p }) => {
  const result = await config.onInsert!({ transaction, ...p })   // acknowledged
  transaction.acknowledge()                                      // flips $acknowledged
  transaction.settleWith(() => processMatchingStrategy(result))  // framework owns the sync wait
  return result   // returns at the ack; isSettled/isPersisted still fire when synced
}

(Whether Electric adopts this is up to the Electric/TanStack folks — it's here as because it was a clear demonstration of the easy improvement this enables for maintainers and sync-builders.)

Why it's still non-breaking

  • settleWith keeps the tx persisting until synced, so isSettled/isPersisted fire at exactly the same moment as today — real collections settle via commit() unchanged.
  • isPersisted stays a working, exact alias of isSettled — the deprecation is a JSDoc @deprecated, not a removal.

On naming

This PR keeps $synced exactly as-is. The only existing name it changes is isPersisted → isSettled (the operationally-defined one). The proposal suggests — but deliberately does not do — a future $synced → $settled rename for full parallelism (the acknowledgement can itself carry synced data, so "synced" is also a little overloaded, in s similar way to isPersisted). That's flagged for maintainers to decide separately; "sync" and "settle" are close enough that this PR ships fine without it.

Trade-off vs #2

This PR's approach to decoupling the overlay from the mutation handler lifecycle feels more like the correct shape. The mutation's job is done once it gets back a 200. But it adds more surface and touches isPersisted. #2 is the conservative alternative: only acknowledge() + $acknowledged, nothing renamed.

(This one also seems to provide a better surface to solve future edge cases like sync timeout messiness. But that may be speculative.)

Tests

  • packages/db/tests/transaction-settle.test.ts (new): settleWith returns the handler at the ack and holds the overlay until the framework step resolves; sync failure rejects isSettled/isPersisted and rolls back (ack not retracted); isPersisted is an exact alias of isSettled.
  • Plus the feat(db): expose an acknowledged state for optimistic mutations #2 acknowledge tests.

Verification: @tanstack/db typechecks clean, builds, full suite 2409 passed / 5 skipped. @tanstack/electric-db-collection 463 passed, 13 files (the only tsc errors are the pre-existing Cannot find module from the unbuilt e2e suite, present on main). Lint: 0 errors.

🤖 Generated with Claude Code

@mhsnook
mhsnook force-pushed the claude/optimistic-settle-layer branch from 384b34d to 46e7cc3 Compare June 27, 2026 10:10
@mhsnook mhsnook changed the title feat(db): acknowledged + settled — settleWith, isSettled, onAcknowledged/onSettled (depth 2) feat(db): acknowledged + settled — settleWith, isSettled, onAcknowledged/onSettled Jun 27, 2026
@mhsnook
mhsnook force-pushed the claude/optimistic-settle-layer branch from 46e7cc3 to 2ee020f Compare June 27, 2026 14:20
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-settle-layer branch from 2ee020f to 42d13dd Compare June 27, 2026 14:41
@mhsnook mhsnook changed the title feat(db): acknowledged + settled — settleWith, isSettled, onAcknowledged/onSettled feat(db): acknowledged + settled — settleWith, isSettled (deprecating isPersisted) Jun 27, 2026
@mhsnook mhsnook changed the title feat(db): acknowledged + settled — settleWith, isSettled (deprecating isPersisted) feat(db): isAcknowledged + isSettled (deprecating isPersisted) Jun 27, 2026
@mhsnook
mhsnook force-pushed the claude/optimistic-settle-layer branch from 42d13dd to a06eec1 Compare June 27, 2026 16:59
Builds on the acknowledge layer with the fuller shape, doing surgery on only one
existing name.

Core (@tanstack/db):
- Transaction.settleWith(fn) + Transaction.isSettled. settleWith lets an adapter
  return the handler at the acknowledgement and hand the sync wait (e.g. awaiting
  a txid) to the framework, which keeps the transaction 'persisting' so the
  optimistic overlay is held — no stale-drop, no flicker — until the thunk
  resolves, then settles.
- isPersisted is soft-deprecated in favour of isSettled (kept as an exact alias).
  This is the only existing name that changes; $synced and all sync/settle words
  are untouched. No new lifecycle-callback surface is added — await
  tx.isAcknowledged / tx.isSettled for side-effects at either milestone.

Adapter (@tanstack/electric-db-collection):
- wrapped handlers acknowledge() then settleWith(processMatchingStrategy),
  returning at the acknowledgement. Real collections settle via commit() exactly
  as before; isSettled/isPersisted timing is unchanged.

Updates the proposal to the full version (keeps $synced, suggests a future
$synced->$settled rename without acting on it) and adds transaction-settle.test.ts.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_012VvSGi4YvJ5cYtJosqXkMe
@mhsnook
mhsnook force-pushed the claude/optimistic-settle-layer branch from a06eec1 to cc10233 Compare June 27, 2026 18:17
When a handler returns without throwing and has registered a settleWith
thunk, that return is the acknowledgement. commit() now flips
isAcknowledged / $acknowledged before awaiting the settle thunk, instead
of leaving both milestones to resolve together at settle time.

This makes the no-settleWith and settleWith paths consistent: resolving
the handler flips acknowledged (and settled too, unless a settleWith
defers the settle milestone to the thunk). Adapters using settleWith no
longer need a separate acknowledge() call — the Electric handlers drop
theirs accordingly (explicit acknowledge() remains a safe idempotent
no-op).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_012JGbK3MVBn1dMn4xnvaWYu
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