react-native-onyx
Version:
State management for React Native
153 lines (152 loc) • 6.48 kB
TypeScript
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<typeof TASK> | `${ValueOf<typeof TASK>}:${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;
/** Unique list of keys maintained in access order (most recent at the end) */
private recentKeys;
/** 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;
/** Maximum size of the keys store din cache */
private maxRecentKeysSize;
/** List of keys that are safe to remove when we reach max storage */
private evictionAllowList;
/** Map of keys and connection arrays whose keys will never be automatically evicted */
private evictionBlocklist;
/** List of keys that have been directly subscribed to or recently modified from least to most recent */
private recentlyAccessedKeys;
constructor();
/** Get all the storage keys */
getAllKeys(): Set<OnyxKey>;
/**
* 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
* @param [shouldReindexCache] – This is an LRU cache, and by default accessing a value will make it become last in line to be evicted. This flag can be used to skip that and just access the value directly without side-effects.
*/
get(key: OnyxKey, shouldReindexCache?: boolean): OnyxValue<OnyxKey>;
/**
* Set's a key value in cache
* Adds the key to the storage keys list as well
*/
set(key: OnyxKey, value: OnyxValue<OnyxKey>): OnyxValue<OnyxKey>;
/** 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<OnyxKey, OnyxValue<OnyxKey>>): 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<OnyxValue<OnyxKey> | 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<OnyxValue<OnyxKey>>): Promise<OnyxValue<OnyxKey>>;
/** Adds a key to the top of the recently accessed keys */
addToAccessedKeys(key: OnyxKey): void;
/** Remove keys that don't fall into the range of recently used keys */
removeLeastRecentlyUsedKeys(): void;
/** Set the recent keys list size */
setRecentKeysLimit(limit: number): void;
/** Check if the value has changed */
hasValueChanged(key: OnyxKey, value: OnyxValue<OnyxKey>): 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;
/**
* Get the eviction block list that prevents keys from being evicted
*/
getEvictionBlocklist(): Record<OnyxKey, string[] | undefined>;
/**
* Checks to see if this key has been flagged as safe for removal.
* @param testKey - Key to check
*/
isEvictableKey(testKey: OnyxKey): boolean;
/**
* Check if a given key matches a pattern key
* @param configKey - Pattern that may contain a wildcard
* @param key - Key to test against the pattern
*/
private isKeyMatch;
/**
* 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<Set<OnyxKey>>): Promise<void>;
/**
* Finds a key that can be safely evicted
*/
getKeyForEviction(): OnyxKey | undefined;
}
declare const instance: OnyxCache;
export default instance;
export { TASK };
export type { CacheTask };