Skip to content
Source
Sign inSign up
TanStack/db

Discarding optimistic state when making cross-collection transactions. #107

Send to my agent
Closedthruflo(GitHub profile, opens in a new tab) opened this issue 4 comments

Just a question (and to surface the consideration because I was staring at it):

Example scenarios:

  1. a transaction updates two query collections
  2. a transaction updates two electric collections
  3. a transaction updates a query collection and an electric collection

The mutation function can:

  • await collection.invalidate() for the query collection(s); and
  • await collection.awaitTxId(txid) for the electric collections

Any of these may take a different amount of time to resolve.

I assume the safe default would be to wait for all collections to be updated. Otherwise you risk flickering if you discard optimistic state before the collection has caught up.

However, I also wonder if there might be a mechanism for different collections to discard their state individually following a single transaction? Such as returning a {collection: promise, ...} value from the mutationFn? I don't know is this is desirable or not but the status quo is either:

  • Promise.all() => wait longer than needed to discard local state if one of your collections is slow
  • Promise.race() => risk of flickering if you discard before all collections have synced
  1. thruflo(GitHub profile, opens in a new tab) added labelsquestion

  2. Yeah a collection won't commit any synced data until a persisting transaction is finished. Which yes means a slow collection will hold it up.

    Allowing people to opt into commiting server state for some collections independently of others makes sense but I'd want to see how common this is a problem first.

  3. Yup, slow and safe seems like the right default and managing different collections separately does seem like it should have more validation before adding the complexity.

  4. Another pattern that occured to me is you could create a separate transaction for the slow collection inside a custom hook so it could resolve on its own time. So you still package optimistic updates but decouple the backed processing.

    This is assuming this didn't tear the UI. But since the optimistic updates for the slow collection are still showing, it might not be hard to pull off.

Sign in to join this conversation.