react-native-onyx
Version:
State management for React Native
94 lines (93 loc) • 4 kB
TypeScript
import type { CollectionKeyBase, CollectionKey, OnyxKey } from './types';
/**
* Initializes the collection key set. Called once during Onyx.init().
*/
declare function setCollectionKeys(keys: Set<OnyxKey>): void;
/**
* Returns the set of all registered collection keys.
*/
declare function getCollectionKeys(): Set<OnyxKey>;
/**
* Checks if the given key is a registered collection key (e.g. "report_").
*/
declare function isCollectionKey(key: OnyxKey): key is CollectionKeyBase;
/**
* Checks if the given key is a member of the specified collection key.
* e.g. isCollectionMemberKey("report_", "report_123") → true
*/
declare function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}`;
/**
* Checks if a given key is a collection member key (not just a collection key).
*/
declare function isCollectionMember(key: OnyxKey): boolean;
/**
* Checks if the provided key matches the config key — either an exact match
* or a collection prefix match.
*/
declare function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean;
/**
* Extracts the collection key from a collection member key.
*
* Uses a pre-computed Map for O(1) lookup. Falls back to string parsing
* for keys not yet in the map (e.g. before they're cached).
*
* Examples:
* - getCollectionKey("report_123") → "report_"
* - getCollectionKey("report_") → "report_"
* - getCollectionKey("sharedNVP_user_-1_something") → "sharedNVP_user_"
*/
declare function getCollectionKey(key: CollectionKey | OnyxKey): string | undefined;
/**
* Pre-computes and caches the member → collection key mapping for a given key.
* Called from OnyxCache.addKey() to ensure the Map stays populated.
*/
declare function registerMemberKey(key: OnyxKey): void;
/**
* Removes a member key from the reverse lookup map.
* Called when a key is dropped from cache.
*/
declare function deregisterMemberKey(key: OnyxKey): void;
/**
* Returns the set of member keys for a given collection key.
* O(1) lookup using the forward index.
*/
declare function getMembersOfCollection(collectionKey: OnyxKey): Set<OnyxKey> | undefined;
/**
* Splits a collection member key into the collection key part and the ID part.
*
* @param key - The collection member key to split
* @param collectionKey - Optional pre-resolved collection key for optimization
* @returns A tuple of [collectionKey, memberId]
* @throws If the key is not a valid collection member key
*/
declare function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType = TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never>(key: TKey, collectionKey?: string): [CollectionKeyType, string];
/**
* Initializes the RAM-only key set. Called once during Onyx.init().
*/
declare function setRamOnlyKeys(keys: Set<OnyxKey>): void;
/**
* Checks if a given key is a RAM-only key, RAM-only collection key, or a RAM-only collection member.
*
* For example, given ramOnlyKeys: ["ramOnlyKey", "ramOnlyCollection_"]:
* - isRamOnlyKey("ramOnlyKey") → true
* - isRamOnlyKey("ramOnlyCollection_") → true
* - isRamOnlyKey("ramOnlyCollection_1") → true
* - isRamOnlyKey("someOtherKey") → false
*/
declare function isRamOnlyKey(key: OnyxKey): boolean;
declare const _default: {
setCollectionKeys: typeof setCollectionKeys;
getCollectionKeys: typeof getCollectionKeys;
isCollectionKey: typeof isCollectionKey;
isCollectionMemberKey: typeof isCollectionMemberKey;
isCollectionMember: typeof isCollectionMember;
isKeyMatch: typeof isKeyMatch;
getCollectionKey: typeof getCollectionKey;
registerMemberKey: typeof registerMemberKey;
deregisterMemberKey: typeof deregisterMemberKey;
getMembersOfCollection: typeof getMembersOfCollection;
splitCollectionMemberKey: typeof splitCollectionMemberKey;
setRamOnlyKeys: typeof setRamOnlyKeys;
isRamOnlyKey: typeof isRamOnlyKey;
};
export default _default;