TanStack / TanStack/db

🧩 Feature Proposal: Add Serialized Transaction Queuing Utility with Strategy Support

Open
#35 1 comment 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.9k
Forks
266
Avg merge
1d 4h
Merged PRs (30d)
55

Description

Summary

Introduce a useSerializedTransaction hook that abstracts common queuing and batching patterns for optimistic transactions in @tanstack/optimistic. This hook simplifies management of concurrent or sequential mutations and provides a clean interface for handling real-world UI interaction patterns like sliders, text inputs, and autosaving forms.

Problem

In components that generate frequent optimistic updates (e.g. sliders, form inputs), developers often need to:

  • Avoid flooding the backend with redundant mutations
  • Preserve the illusion of immediate UI updates
  • Ensure only one transaction is persisted at a time
  • Support merging or batching of changes intelligently

Handling these concerns manually is error-prone and verbose. The new hook provides a structured way to manage these needs.


Proposed API
const commit = useSerializedTransaction({
  mutationFn: async ({ transaction }) => {
    await api.save(transaction.mutations)
  },
  metadata: { context: 'slider' },
  strategy: 'latest', // or 'fifo' | 'debounce' | 'merge'
  debounceMs: 1000 // optional, for 'debounce' strategy
})

For 'merge' strategy, the hook returns a tuple:

const [commit, commitMerge] = useSerializedTransaction({ strategy: 'merge', ... });

Supported Strategies
1. latest (throttle-style)
  • ⚡ Persist most recent mutation, discard prior ones still pending
  • ✅ Best for: sliders, rapid toggles
  • 🧠 Principle: only final state matters
2. fifo (queue-style)
  • 🧱 Queue all mutations in order, persist one at a time
  • ✅ Best for: chat logs, append-only lists
  • 🧠 Principle: every action matters
3. debounce
  • ⏱️ Wait for idle pause, then persist a single coalesced mutation
  • ✅ Best for: text inputs, autosave forms
  • 🧠 Principle: only save after user stops typing
4. merge
  • 🧩 Accumulate all mutations into a single long-lived transaction, committed manually
  • ✅ Best for: complex forms, batch operations
  • 🧠 Principle: user triggers save when ready

Benefits
  • Simplifies transaction lifecycle logic in components
  • Promotes reuse of queuing patterns across the app
  • Reduces accidental backend write contention
  • Keeps UI fully optimistic and reactive

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The proposed entry point is the useSerializedTransaction hook in @tanstack/optimistic; first inspect the package's existing transaction and mutation APIs. Compare the four listed strategies and the proposed return shapes, then treat support for their stated queuing behavior and examples as the definition of done.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.