import { useCallback, useMemo, useRef } from 'react' import { createPacedMutations } from '@tanstack/db' import type { PacedMutationsConfig, Transaction } from '@tanstack/db' /** * React hook for managing paced mutations with timing strategies. * * Provides optimistic mutations with pluggable strategies like debouncing, * queuing, or throttling. The optimistic updates are applied immediately via * `onMutate`, and the actual persistence is controlled by the strategy. * * @param config - Configuration including onMutate, mutationFn and strategy * @returns A mutate function that accepts variables and returns Transaction objects * * @example * ```tsx * // Debounced auto-save * function AutoSaveForm({ formId }: { formId: string }) { * const mutate = usePacedMutations({ * onMutate: (value) => { * // Apply optimistic update immediately * formCollection.update(formId, draft => { * draft.content = value * }) * }, * mutationFn: async ({ transaction }) => { * await api.save(transaction.mutations) * }, * strategy: debounceStrategy({ wait: 500 }) * }) * * const handleChange = async (value: string) => { * const tx = mutate(value) * * // Optional: await persistence or handle errors * try { * await tx.isPersisted.promise * console.log('Saved!') * } catch (error) { * console.error('Save failed:', error) * } * } * * return