/** * @file Optimistic Sync * @description Optimistic updates with automatic rollback, retry logic, * and state reconciliation for seamless user experience. * * Features: * - Optimistic state updates * - Automatic rollback on failure * - Retry with exponential backoff * - State snapshot management * - Pending changes tracking * - React integration hooks * * @example * ```typescript * import { useOptimisticSync } from '@/lib/data/sync'; * * const { data, update, isPending, pendingChanges } = useOptimisticSync({ * key: 'user', * fetcher: fetchUser, * updater: updateUser, * }); * * // Optimistic update * update({ name: 'New Name' }); * ``` */ /** * Optimistic update status */ export type OptimisticStatus = 'idle' | 'pending' | 'syncing' | 'success' | 'error' | 'rolled-back'; /** * Pending change record */ export interface PendingChange { /** Unique ID for this change */ id: string; /** Original data before change */ snapshot: T; /** Optimistic data after change */ optimisticData: T; /** Change payload */ payload: Partial; /** Change timestamp */ timestamp: number; /** Current status */ status: OptimisticStatus; /** Error message if failed */ error?: string; /** Retry count */ retryCount: number; } /** * Optimistic sync configuration */ export interface OptimisticSyncConfig { /** Unique key for this sync */ key: string; /** Initial data */ initialData?: T; /** Fetch current data */ fetcher?: () => Promise; /** Persist changes to server */ persister: (data: T, payload: Partial) => Promise; /** Merge function for optimistic updates */ merger?: (current: T, payload: Partial) => T; /** Rollback function */ rollback?: (snapshot: T, error: Error) => T; /** Max retry attempts */ maxRetries?: number; /** Retry delay base (ms) */ retryDelay?: number; /** Debounce updates (ms) */ debounceMs?: number; /** Batch updates within window (ms) */ batchWindow?: number; /** Enable persistence queue */ enableQueue?: boolean; /** On success callback */ onSuccess?: (data: T, payload: Partial) => void; /** On error callback */ onError?: (error: Error, payload: Partial, snapshot: T) => void; /** On rollback callback */ onRollback?: (snapshot: T, error: Error) => void; } /** * Optimistic sync state */ export interface OptimisticSyncState { /** Current data (may be optimistic) */ data: T | undefined; /** Server-confirmed data */ confirmedData: T | undefined; /** Whether any changes are pending */ isPending: boolean; /** Whether currently syncing */ isSyncing: boolean; /** Last error */ error: Error | null; /** Status */ status: OptimisticStatus; /** Pending changes */ pendingChanges: PendingChange[]; /** Is data stale (has pending changes) */ isStale: boolean; } /** * Optimistic sync actions */ export interface OptimisticSyncActions { /** Apply optimistic update */ update: (payload: Partial) => Promise; /** Refresh from server */ refresh: () => Promise; /** Rollback all pending changes */ rollbackAll: () => void; /** Rollback specific change */ rollbackChange: (changeId: string) => void; /** Retry failed changes */ retryFailed: () => Promise; /** Reset state */ reset: (data?: T) => void; } /** * Optimistic sync return type */ export type OptimisticSyncReturn = OptimisticSyncState & OptimisticSyncActions; /** * React hook for optimistic updates */ export declare function useOptimisticSync(config: OptimisticSyncConfig): OptimisticSyncReturn; /** * Item with required ID */ export interface ListItem { id: string; } /** * Optimistic list configuration */ export interface OptimisticListConfig { /** Unique key for this list */ key: string; /** Initial items */ initialItems?: T[]; /** Fetch items */ fetcher?: () => Promise; /** Create item */ createItem?: (item: Omit) => Promise; /** Update item */ updateItem?: (id: string, updates: Partial) => Promise; /** Delete item */ deleteItem?: (id: string) => Promise; /** Generate temporary ID for optimistic creates */ generateId?: () => string; /** Max retries */ maxRetries?: number; /** Callbacks */ onCreateSuccess?: (item: T) => void; onUpdateSuccess?: (item: T) => void; onDeleteSuccess?: (id: string) => void; onError?: (error: Error, operation: string) => void; } /** * Pending list operation */ export interface PendingListOperation { id: string; type: 'create' | 'update' | 'delete'; itemId: string; data?: T | Partial; timestamp: number; status: OptimisticStatus; retryCount: number; } /** * React hook for optimistic list operations */ export declare function useOptimisticList(config: OptimisticListConfig): { items: T[]; isPending: boolean; error: Error | null; create: (item: Omit) => Promise; update: (id: string, updates: Partial) => Promise; remove: (id: string) => Promise; refresh: () => Promise; reset: (newItems?: T[]) => void; };