UNPKG

react-native-onyx

Version:

State management for React Native

79 lines (78 loc) 3.97 kB
import type { OnyxKey, OnyxValue } from './types'; import type { UseOnyxOptions, UseOnyxResult, UseOnyxSelector } from './useOnyx'; /** * Manages snapshot caching for useOnyx hook performance optimization. * Handles selector function tracking and memoized getSnapshot results. */ declare class OnyxSnapshotCache { /** * Snapshot cache is a two-level map. The top-level keys are Onyx keys. The top-level values maps. * The second-level keys are a custom composite string defined by this.registerConsumer. These represent a unique useOnyx config, which is not fully represented by the Onyx key alone. * The reason we have two levels is for performance: not to make cache access faster, but to make cache invalidation faster. * We can invalidate the snapshot cache for a given Onyx key with one map.delete operation on the top-level map, rather than having to loop through a large single-level map and delete any matching keys. */ private snapshotCache; /** * Maps selector functions to unique IDs for cache key generation */ private selectorIDMap; /** * Counter for generating incremental selector IDs */ private selectorIDCounter; /** * Reference counting for cache keys to enable automatic cleanup. * Maps cache key (string) to number of consumers using it. */ private cacheKeyRefCounts; constructor(); /** * Generate unique ID for selector functions using incrementing numbers */ getSelectorID<TKey extends OnyxKey, TReturnValue>(selector: UseOnyxSelector<TKey, TReturnValue>): number; /** * Register a consumer for a cache key and return the cache key. * Generates cache key and increments reference counter. * * The properties used to generate the cache key are handpicked for performance reasons and * according to their purpose and effect they produce in the useOnyx hook behavior: * * - `selector`: Different selectors produce different results, so each selector needs its own cache entry * - `initWithStoredValues`: This flag changes the initial loading behavior and affects the returned fetch status * - `allowStaleData`: Controls whether stale data can be returned during pending merges, affecting result timing * - `canBeMissing`: Determines logging behavior for missing data, but doesn't affect the actual data returned * * Other options like `canEvict`, `reuseConnection`, and `allowDynamicKey` don't affect the data transformation * or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates. */ registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector' | 'initWithStoredValues' | 'allowStaleData' | 'canBeMissing'>): string; /** * Deregister a consumer for a cache key. * Decrements reference counter and removes cache entry if no consumers remain. */ deregisterConsumer(key: OnyxKey, cacheKey: string): void; /** * Get cached snapshot result for a key and cache key combination */ getCachedResult<TResult extends UseOnyxResult<OnyxValue<OnyxKey>>>(key: OnyxKey, cacheKey: string): TResult | undefined; /** * Set cached snapshot result for a key and cache key combination */ setCachedResult<TResult extends UseOnyxResult<OnyxValue<OnyxKey>>>(key: OnyxKey, cacheKey: string, result: TResult): void; /** * Selective cache invalidation to prevent data unavailability * Collection members invalidate upward, collections don't cascade downward */ invalidateForKey(keyToInvalidate: OnyxKey): void; /** * Clear all snapshot cache */ clear(): void; /** * Clear selector ID mappings (useful for testing) */ clearSelectorIds(): void; } declare const onyxSnapshotCache: OnyxSnapshotCache; export default onyxSnapshotCache; export { OnyxSnapshotCache };