import type { ValueOf } from 'type-fest'; import type { OnyxKey, OnyxValue } from './types'; declare const TASK: { readonly GET: "get"; readonly GET_ALL_KEYS: "getAllKeys"; readonly CLEAR: "clear"; }; type CacheTask = ValueOf | `${ValueOf}:${string}`; /** * In memory cache providing data by reference * Encapsulates Onyx cache related functionality */ declare class OnyxCache { /** Cache of all the storage keys available in persistent storage */ private storageKeys; /** A list of keys where a nullish value has been fetched from storage before, but the key still exists in cache */ private nullishStorageKeys; /** A map of cached values */ private storageMap; /** * Captured pending tasks for already running storage methods * Using a map yields better performance on operations such a delete */ private pendingPromises; /** List of keys that are safe to remove when we reach max storage */ private evictionAllowList; /** List of keys that have been directly subscribed to or recently modified from least to most recent */ private recentlyAccessedKeys; /** Frozen collection snapshots for structural sharing */ private collectionSnapshots; /** Collections whose snapshots need rebuilding (lazy — rebuilt on next read) */ private dirtyCollections; constructor(); /** Get all the storage keys */ getAllKeys(): Set; /** * Allows to set all the keys at once. * This is useful when we are getting * all the keys from the storage provider * and we want to keep the cache in sync. * * Previously, we had to call `addKey` in a loop * to achieve the same result. * * @param keys - an array of keys */ setAllKeys(keys: OnyxKey[]): void; /** Saves a key in the storage keys list * Serves to keep the result of `getAllKeys` up to date */ addKey(key: OnyxKey): void; /** Used to set keys that are null/undefined in storage without adding null to the storage map */ addNullishStorageKey(key: OnyxKey): void; /** Used to set keys that are null/undefined in storage without adding null to the storage map */ hasNullishStorageKey(key: OnyxKey): boolean; /** Used to clear keys that are null/undefined in cache */ clearNullishStorageKeys(): void; /** Check whether cache has data for the given key */ hasCacheForKey(key: OnyxKey): boolean; /** Get a cached value from storage */ get(key: OnyxKey): OnyxValue; /** * Set's a key value in cache * Adds the key to the storage keys list as well */ set(key: OnyxKey, value: OnyxValue): OnyxValue; /** Forget the cached value for the given key */ drop(key: OnyxKey): void; /** * Deep merge data to cache, any non existing keys will be created * @param data - a map of (cache) key - values */ merge(data: Record>): void; /** * Check whether the given task is already running * @param taskName - unique name given for the task */ hasPendingTask(taskName: CacheTask): boolean; /** * Use this method to prevent concurrent calls for the same thing * Instead of calling the same task again use the existing promise * provided from this function * @param taskName - unique name given for the task */ getTaskPromise(taskName: CacheTask): Promise | OnyxKey[]> | undefined; /** * Capture a promise for a given task so other caller can * hook up to the promise if it's still pending * @param taskName - unique name for the task */ captureTask(taskName: CacheTask, promise: Promise>): Promise>; /** Check if the value has changed. Uses reference equality as a fast path, falls back to deep equality. */ hasValueChanged(key: OnyxKey, value: OnyxValue): boolean; /** * Sets the list of keys that are considered safe for eviction * @param keys - Array of OnyxKeys that are safe to evict */ setEvictionAllowList(keys: OnyxKey[]): void; /** * Checks to see if this key has been flagged as safe for removal. * @param testKey - Key to check */ isEvictableKey(testKey: OnyxKey): boolean; /** * Remove a key from the recently accessed key list. */ removeLastAccessedKey(key: OnyxKey): void; /** * Add a key to the list of recently accessed keys. The least * recently accessed key should be at the head and the most * recently accessed key at the tail. */ addLastAccessedKey(key: OnyxKey, isCollectionKey: boolean): void; /** * Take all the keys that are safe to evict and add them to * the recently accessed list when initializing the app. This * enables keys that have not recently been accessed to be * removed. * @param isCollectionKeyFn - Function to determine if a key is a collection key * @param getAllKeysFn - Function to get all keys, defaults to Storage.getAllKeys */ addEvictableKeysToRecentlyAccessedList(isCollectionKeyFn: (key: OnyxKey) => boolean, getAllKeysFn: () => Promise>): Promise; /** * Finds the least recently accessed key that can be safely evicted from storage. */ getKeyForEviction(): OnyxKey | undefined; /** * Set the collection keys for optimized storage */ setCollectionKeys(collectionKeys: Set): void; /** * Rebuilds the frozen collection snapshot from current storageMap references. * Uses the indexed collection->members map for O(collectionMembers) instead of O(totalKeys). * Returns the previous snapshot reference when all member references are identical, * preventing unnecessary re-renders in useSyncExternalStore. * * @param collectionKey - The collection key to rebuild */ private rebuildCollectionSnapshot; /** * Get all data for a collection key. * Returns a frozen snapshot with structural sharing — safe to return by reference. * Lazily rebuilds the snapshot if the collection was modified since the last read. */ getCollectionData(collectionKey: OnyxKey): Record> | undefined; } declare const instance: OnyxCache; export default instance; export { TASK }; export type { CacheTask };