/** * React hooks for Pub/Sub system * * @module pubsub/react */ import { type FetchResponse } from 'fetchff'; import type { SubscriptionCallback, DefaultResponse } from './index'; import type { ApiError } from '@plyaz/types/api'; /** * Hook to subscribe to cache updates * Handles cleanup automatically * * @param key - Cache key to subscribe to * @param callback - Function called on cache updates * @param enabled - Enable/disable subscription * * @example * ```typescript * function LiveData() { * const [data, setData] = useState(null); * * useSubscription('/api/live-data', (response) => { * if (response.data) { * setData(response.data); * } * }); * * return ; * } * ``` */ export declare function useSubscription(key: string | null, callback: SubscriptionCallback, enabled?: boolean): void; /** * Subscribe to multiple cache keys at once * Useful for dashboards or components that need multiple data sources * * @param keys - Array of cache keys to subscribe to * @param callback - Function called on any cache update * @param enabled - Enable/disable all subscriptions * * @example * ```typescript * function Dashboard() { * const [metrics, setMetrics] = useState({}); * * useMultipleSubscriptions( * ['/api/metrics', '/api/alerts', '/api/status'], * (key, response) => { * const dataKey = key.split('/').pop(); * setMetrics(prev => ({ * ...prev, * [dataKey]: response.data * })); * } * ); * * return ; * } * ``` */ export declare function useMultipleSubscriptions(keys: string[], callback: (key: string, response: FetchResponse) => void, enabled?: boolean): void; /** * Hook for optimistic updates with subscription * Provides mutate function and subscribes to updates * * @param key - Cache key * @param initialData - Initial data value * * @example * ```typescript * function EditableUser({ userId }) { * const { data, mutate, isValidating } = useOptimisticUpdate( * `/api/user/${userId}`, * null * ); * * const updateName = async (newName) => { * // Optimistic update * mutate({ ...data, name: newName }); * * // Send to server * await api.updateUser(userId, { name: newName }); * }; * * return ( * updateName(e.target.value)} * disabled={isValidating} * /> * ); * } * ``` */ export declare function useOptimisticUpdate(key: string, initialData?: T): { data: T | undefined; mutate: (data: T | ((current: T | undefined) => T)) => void; isValidating: boolean; error: ApiError | undefined; }; /** * Hook for real-time data with automatic refresh * * @param key - Cache key * @param refreshInterval - Refresh interval in milliseconds * @param enabled - Enable/disable real-time updates * * @example * ```typescript * function LiveMetrics() { * const { data, isLoading, error } = useRealTimeData( * '/api/metrics/live', * 5000 // Refresh every 5 seconds * ); * * if (isLoading) return ; * if (error) return ; * * return ; * } * ``` */ export declare function useRealTimeData, Constraints = Record, TError = ApiError>(key: string, refreshInterval?: number, enabled?: boolean): { data: T | undefined; isLoading: boolean; error: TError | undefined; refresh: () => void; }; /** * Hook for subscription with debounced callback * Useful for reducing update frequency * * @param key - Cache key * @param callback - Debounced callback * @param delay - Debounce delay in milliseconds * * @example * ```typescript * function SearchResults() { * const [results, setResults] = useState([]); * * // Only update UI after 300ms of no changes * useDebouncedSubscription( * '/api/search/results', * (response) => { * setResults(response.data || []); * }, * 300 * ); * * return ; * } * ``` */ export declare function useDebouncedSubscription(key: string | null, callback: SubscriptionCallback, delay?: number): void; /** * Hook for conditional subscription based on a condition * * @param key - Cache key * @param callback - Subscription callback * @param condition - Condition function or boolean * * @example * ```typescript * function ConditionalData({ shouldFetch }) { * const [data, setData] = useState(null); * * useConditionalSubscription( * '/api/conditional-data', * (response) => setData(response.data), * shouldFetch // Only subscribe when true * ); * * return data ? : ; * } * ``` */ export declare function useConditionalSubscription(key: string | null, callback: SubscriptionCallback, condition?: boolean | (() => boolean)): void; /** * Hook to track subscription state * * @param key - Cache key * @returns Subscription state * * @example * ```typescript * function DataComponent() { * const { data, error, isValidating, isSubscribed } = useSubscriptionState( * '/api/data' * ); * * return ( *
* {isSubscribed && Live} * {isValidating && } * {error && } * {data && } *
* ); * } * ``` */ export declare function useSubscriptionState, Constraints = Record, TError = ApiError>(key: string | null): { data: T | undefined; error: TError | undefined; isValidating: boolean; isLoading: boolean; isSubscribed: boolean; }; //# sourceMappingURL=react.d.ts.map