|
| 1 | +--- |
| 2 | +"@tanstack/db": patch |
| 3 | +"@tanstack/react-db": patch |
| 4 | +--- |
| 5 | + |
| 6 | +Add paced mutations with pluggable timing strategies |
| 7 | + |
| 8 | +Introduces a new paced mutations system that enables optimistic mutations with pluggable timing strategies. This provides fine-grained control over when and how mutations are persisted to the backend. Powered by [TanStack Pacer](https://github.com/TanStack/pacer). |
| 9 | + |
| 10 | +**Key Design:** |
| 11 | + |
| 12 | +- **Debounce/Throttle**: Only one pending transaction (collecting mutations) and one persisting transaction (writing to backend) at a time. Multiple rapid mutations automatically merge together. |
| 13 | +- **Queue**: Each mutation creates a separate transaction, guaranteed to run in the order they're made (FIFO by default, configurable to LIFO). |
| 14 | + |
| 15 | +**Core Features:** |
| 16 | + |
| 17 | +- **Pluggable Strategy System**: Choose from debounce, queue, or throttle strategies to control mutation timing |
| 18 | +- **Auto-merging Mutations**: Multiple rapid mutations on the same item automatically merge for efficiency (debounce/throttle only) |
| 19 | +- **Transaction Management**: Full transaction lifecycle tracking (pending → persisting → completed/failed) |
| 20 | +- **React Hook**: `usePacedMutations` for easy integration in React applications |
| 21 | + |
| 22 | +**Available Strategies:** |
| 23 | + |
| 24 | +- `debounceStrategy`: Wait for inactivity before persisting. Only final state is saved. (ideal for auto-save, search-as-you-type) |
| 25 | +- `queueStrategy`: Each mutation becomes a separate transaction, processed sequentially in order (defaults to FIFO, configurable to LIFO). All mutations are guaranteed to persist. (ideal for sequential workflows, rate-limited APIs) |
| 26 | +- `throttleStrategy`: Ensure minimum spacing between executions. Mutations between executions are merged. (ideal for analytics, progress updates) |
| 27 | + |
| 28 | +**Example Usage:** |
| 29 | + |
| 30 | +```ts |
| 31 | +import { usePacedMutations, debounceStrategy } from "@tanstack/react-db" |
| 32 | + |
| 33 | +const mutate = usePacedMutations({ |
| 34 | + mutationFn: async ({ transaction }) => { |
| 35 | + await api.save(transaction.mutations) |
| 36 | + }, |
| 37 | + strategy: debounceStrategy({ wait: 500 }), |
| 38 | +}) |
| 39 | + |
| 40 | +// Trigger a mutation |
| 41 | +const tx = mutate(() => { |
| 42 | + collection.update(id, (draft) => { |
| 43 | + draft.value = newValue |
| 44 | + }) |
| 45 | +}) |
| 46 | + |
| 47 | +// Optionally await persistence |
| 48 | +await tx.isPersisted.promise |
| 49 | +``` |
0 commit comments