/** * @file Optimistic UI Updates * @description Optimistic update utilities for React applications. * Provides patterns for immediate UI feedback with automatic rollback. * * Features: * - Optimistic state management * - Automatic rollback on error * - Pending state tracking * - Conflict resolution * - Retry mechanisms * - Undo support */ /** * Optimistic update status */ export type OptimisticStatus = 'pending' | 'confirmed' | 'failed' | 'rolled-back'; /** * Optimistic update entry */ export interface OptimisticUpdate { id: string; previousValue: T; optimisticValue: T; status: OptimisticStatus; timestamp: number; retryCount: number; error?: Error; } /** * Optimistic update result */ export interface OptimisticResult { value: T; pending: boolean; error: Error | null; retry: () => Promise; rollback: () => void; } /** * Optimistic manager configuration */ export interface OptimisticConfig { /** Maximum retry attempts */ maxRetries: number; /** Retry delay in ms */ retryDelay: number; /** Retry backoff multiplier */ retryBackoff: number; /** Enable automatic retry on failure */ autoRetry: boolean; /** Timeout for optimistic updates in ms */ timeout: number; /** Keep history for undo */ keepHistory: boolean; /** Maximum history size */ maxHistorySize: number; /** Debug mode */ debug: boolean; } /** * Mutation function type */ export type MutationFn = (...args: TArgs) => Promise; /** * Optimistic value updater */ export type OptimisticUpdater = (currentValue: T) => T; /** * Conflict resolver */ export type ConflictResolver = (localValue: T, serverValue: T, baseValue: T) => T; /** * Update listener */ export type UpdateListener = (update: OptimisticUpdate) => void; /** * Manages optimistic updates with rollback support */ export declare class OptimisticUpdateManager { private config; private currentValue; private updates; private history; private listeners; private idCounter; constructor(initialValue: T, config?: Partial); /** * Get the current value (with pending optimistic updates applied) */ getValue(): T; /** * Get the confirmed value (without pending updates) */ getConfirmedValue(): T; /** * Apply an optimistic update */ applyUpdate(updater: OptimisticUpdater, mutation: MutationFn, ...args: TArgs): Promise>; /** * Retry a failed update */ retryUpdate(id: string, mutation: MutationFn, ...args: TArgs): Promise; /** * Rollback an update */ rollbackUpdate(id: string): void; /** * Get pending updates */ getPendingUpdates(): OptimisticUpdate[]; /** * Get failed updates */ getFailedUpdates(): OptimisticUpdate[]; /** * Check if there are pending updates */ hasPendingUpdates(): boolean; /** * Undo the last confirmed update */ undo(): T | null; /** * Subscribe to update changes */ subscribe(listener: UpdateListener): () => void; /** * Clear all updates */ clear(): void; /** * Get update statistics */ getStats(): { pending: number; confirmed: number; failed: number; rolledBack: number; historySize: number; }; private executeWithTimeout; private calculateRetryDelay; private addToHistory; private notifyListeners; private generateId; private sleep; private log; } /** * Specialized manager for list operations */ export declare class OptimisticListManager { private manager; constructor(initialItems: T[], config?: Partial); /** * Get current items */ getItems(): T[]; /** * Optimistically add an item */ addItem(item: T, mutation: MutationFn): Promise>; /** * Optimistically remove an item */ removeItem(id: string, mutation: MutationFn): Promise>; /** * Optimistically update an item */ updateItem(id: string, updates: Partial, mutation: MutationFn): Promise>; /** * Optimistically reorder items */ reorderItems(fromIndex: number, toIndex: number, mutation: MutationFn): Promise>; /** * Get pending items (items being added) */ getPendingItems(): T[]; /** * Check if an item has pending changes */ hasItemPending(id: string): boolean; /** * Subscribe to changes */ subscribe(listener: UpdateListener): () => void; /** * Check if there are pending updates */ hasPendingUpdates(): boolean; /** * Undo last change */ undo(): T[] | null; } /** * Create an optimistic update manager */ export declare function createOptimisticManager(initialValue: T, config?: Partial): OptimisticUpdateManager; /** * Create an optimistic list manager */ export declare function createOptimisticListManager(initialItems: T[], config?: Partial): OptimisticListManager; /** * Apply optimistic update to a value */ export declare function applyOptimistic(currentValue: T, updater: OptimisticUpdater, mutation: MutationFn, ...args: TArgs): Promise<{ value: T; confirmed: boolean; error: Error | null; }>; /** * Merge server and local values with conflict resolution */ export declare function mergeWithConflictResolution(localValue: T, serverValue: T, baseValue: T, resolver: ConflictResolver): T; /** * Default conflict resolver (server wins) */ export declare function serverWinsResolver(_localValue: T, serverValue: T, _baseValue: T): T; /** * Client wins conflict resolver */ export declare function clientWinsResolver(localValue: T, _serverValue: T, _baseValue: T): T; /** * Last write wins resolver (based on timestamp) */ export declare function lastWriteWinsResolver(localValue: T, serverValue: T, _baseValue: T): T;