UNPKG

react-query-external-sync

Version:

A tool for syncing React Query state to an external Dev Tools

55 lines (54 loc) 2.09 kB
import { QueryClient } from '@tanstack/react-query'; /** * AsyncStorage static interface (from @react-native-async-storage/async-storage) */ export interface AsyncStorageStatic { getItem: (key: string) => Promise<string | null>; getAllKeys: () => Promise<readonly string[]>; setItem: (key: string, value: string) => Promise<void>; removeItem: (key: string) => Promise<void>; } export interface UseDynamicAsyncStorageQueriesOptions { /** * The React Query client instance */ queryClient: QueryClient; /** * AsyncStorage instance to use for storage operations * Pass your AsyncStorage instance from @react-native-async-storage/async-storage * If not provided, the hook will be disabled */ asyncStorage?: AsyncStorageStatic; /** * Optional interval in milliseconds to poll for key changes * Defaults to 5000ms (5 seconds). Set to 0 to disable polling. */ pollInterval?: number; /** * Whether to enable AsyncStorage monitoring * When false, no queries will be created and no polling will occur * @default true */ enabled?: boolean; } export interface AsyncStorageQueryResult { key: string; data: unknown; isLoading: boolean; error: Error | null; } /** * Hook that creates individual React Query queries for each AsyncStorage key * This gives you granular control and better performance since each key has its own query * Since AsyncStorage doesn't have built-in change listeners, this hook uses polling to detect changes * * @example * // Get individual queries for all AsyncStorage keys * const queries = useDynamicAsyncStorageQueries({ queryClient }); * // Returns: [ * // { key: '@notifications:status', data: 'enabled', isLoading: false, error: null }, * // { key: '@user:preferences', data: { theme: 'dark' }, isLoading: false, error: null }, * // ... * // ] */ export declare function useDynamicAsyncStorageQueries({ queryClient, asyncStorage, pollInterval, enabled, }: UseDynamicAsyncStorageQueriesOptions): AsyncStorageQueryResult[];