/** * @file useBuffer Hook * @description Generic buffering/batching hook for accumulating items and flushing based on size or time * * This hook is useful for scenarios like: * - Batching analytics events before sending * - Accumulating performance metrics * - Buffering real-time updates before rendering * - Debouncing multiple rapid updates into batches * * @example * ```tsx * function AnalyticsTracker() { * const { add, flush } = useBuffer({ * maxSize: 10, * flushInterval: 5000, * onFlush: (events) => sendToAnalytics(events) * }); * * const trackEvent = (event) => { * add(event); // Automatically flushes when buffer reaches 10 or after 5s * }; * * return ; * } * ``` */ /** * Buffer configuration options */ export interface BufferOptions { /** Maximum buffer size before auto-flush */ maxSize?: number; /** Time interval for auto-flush (ms) */ flushInterval?: number; /** Callback when buffer is flushed */ onFlush?: (items: T[]) => void | Promise; /** Whether to flush on component unmount */ flushOnUnmount?: boolean; /** Custom flush condition */ shouldFlush?: (buffer: T[]) => boolean; } /** * Buffer hook return type */ export interface UseBufferResult { /** Add item to buffer */ add: (item: T) => void; /** Add multiple items to buffer */ addMany: (items: T[]) => void; /** Manually flush buffer */ flush: () => void; /** Clear buffer without flushing */ clear: () => void; /** Get current buffer size */ size: () => number; /** Get current buffer contents (read-only) */ peek: () => readonly T[]; } /** * Hook for buffering items with automatic flushing * * @param options - Buffer configuration * @returns Buffer control interface */ export declare function useBuffer(options?: BufferOptions): UseBufferResult; /** * Hook for buffering with time-based windows * * Flushes buffer at regular intervals regardless of size. Useful for * aggregating time-series data or periodic batch updates. * * @example * ```tsx * const { add } = useTimeWindowBuffer({ * windowSize: 1000, // 1 second windows * onFlush: (items) => console.log(`${items.length} items in window`) * }); * ``` */ export declare function useTimeWindowBuffer(options: Omit, 'maxSize' | 'shouldFlush'> & { windowSize: number; }): UseBufferResult; /** * Hook for buffering with size-based batches only * * Flushes only when buffer reaches max size (no time-based flushing). * Useful for operations that must happen in specific batch sizes. * * @example * ```tsx * const { add } = useBatchBuffer({ * batchSize: 100, * onFlush: (batch) => bulkInsert(batch) * }); * ``` */ export declare function useBatchBuffer(options: Omit, 'flushInterval' | 'shouldFlush'> & { batchSize: number; }): UseBufferResult;