import type { ValueOf } from 'type-fest'; import type Onyx from './Onyx'; import type { CollectionKeyBase, ConnectOptions, DeepRecord, KeyValueMapping, CallbackToStateMapping, MultiMergeReplaceNullPatches, OnyxCollection, OnyxEntry, OnyxInput, OnyxKey, OnyxMergeCollectionInput, OnyxUpdate, OnyxValue, Selector, MergeCollectionWithPatchesParams, SetCollectionParams, SetParams, OnyxMultiSetInput, RetriableOnyxOperation } from './types'; import type { FastMergeResult } from './utils'; import type { DeferredTask } from './createDeferredTask'; import type { StorageKeyValuePair } from './storage/providers/types'; declare const METHOD: { readonly SET: "set"; readonly MERGE: "merge"; readonly MERGE_COLLECTION: "mergecollection"; readonly SET_COLLECTION: "setcollection"; readonly MULTI_SET: "multiset"; readonly CLEAR: "clear"; }; type OnyxMethod = ValueOf; declare function getSnapshotKey(): OnyxKey | null; /** * Getter - returns the merge queue. */ declare function getMergeQueue(): Record>>; /** * Getter - returns the merge queue promise. */ declare function getMergeQueuePromise(): Record>; /** * Getter - returns the default key states. */ declare function getDefaultKeyStates(): Record>; /** * Getter - returns the deffered init task. */ declare function getDeferredInitTask(): DeferredTask; /** * Executes an action after Onyx has been initialized. * If Onyx is already initialized, the action is executed immediately. * Otherwise, it waits for initialization to complete before executing. * * @param action The action to execute after initialization * @returns The result of the action */ declare function afterInit(action: () => Promise): Promise; /** * Getter - returns the skippable collection member IDs. */ declare function getSkippableCollectionMemberIDs(): Set; /** * Getter - returns the snapshot merge keys allowlist. */ declare function getSnapshotMergeKeys(): Set; /** * Setter - sets the skippable collection member IDs. */ declare function setSkippableCollectionMemberIDs(ids: Set): void; /** * Setter - sets the snapshot merge keys allowlist. */ declare function setSnapshotMergeKeys(keys: Set): void; /** * Sets the initial values for the Onyx store * * @param keys - `ONYXKEYS` constants object from Onyx.init() * @param initialKeyStates - initial data to set when `init()` and `clear()` are called * @param evictableKeys - This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal. */ declare function initStoreValues(keys: DeepRecord, initialKeyStates: Partial, evictableKeys: OnyxKey[]): void; /** * Sends an action to DevTools extension * * @param method - Onyx method from METHOD * @param key - Onyx key that was changed * @param value - contains the change that was made by the method * @param mergedValue - (optional) value that was written in the storage after a merge method was executed. */ declare function sendActionToDevTools(method: typeof METHOD.MERGE_COLLECTION | typeof METHOD.MULTI_SET | typeof METHOD.SET_COLLECTION, key: undefined, value: OnyxCollection, mergedValue?: undefined): void; declare function sendActionToDevTools(method: Exclude, key: OnyxKey, value: OnyxEntry, mergedValue?: OnyxEntry): void; /** * Takes a collection of items (eg. {testKey_1:{a:'a'}, testKey_2:{b:'b'}}) * and runs it through a reducer function to return a subset of the data according to a selector. * The resulting collection will only contain items that are returned by the selector. */ declare function reduceCollectionWithSelector(collection: OnyxCollection, selector: Selector): Record; /** Get some data from the store */ declare function get>(key: TKey): Promise; declare function multiGet(keys: CollectionKeyBase[]): Promise>>; /** * This helper exists to map an array of Onyx keys such as `['report_', 'conciergeReportID']` * to the values for those keys (correctly typed) such as `[OnyxCollection, OnyxEntry]` * * Note: just using `.map`, you'd end up with `Array|OnyxEntry>`, which is not what we want. This preserves the order of the keys provided. */ declare function tupleGet(keys: Keys): Promise<{ [Index in keyof Keys]: OnyxValue; }>; /** * Stores a subscription ID associated with a given key. * * @param subscriptionID - A subscription ID of the subscriber. * @param key - A key that the subscriber is subscribed to. */ declare function storeKeyBySubscriptions(key: OnyxKey, subscriptionID: number): void; /** * Deletes a subscription ID associated with its corresponding key. * * @param subscriptionID - The subscription ID to be deleted. */ declare function deleteKeyBySubscriptions(subscriptionID: number): void; /** Returns current key names stored in persisted storage */ declare function getAllKeys(): Promise>; /** * Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined. * If the requested key is a collection, it will return an object with all the collection members. */ declare function tryGetCachedValue(key: TKey): OnyxValue; declare function getCachedCollection(collectionKey: TKey, collectionMemberKeys?: string[]): NonNullable>; /** * When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks */ declare function keysChanged(collectionKey: TKey, partialCollection: OnyxCollection, partialPreviousCollection: OnyxCollection | undefined): void; /** * When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks */ declare function keyChanged(key: TKey, value: OnyxValue, canUpdateSubscriber?: (subscriber?: CallbackToStateMapping) => boolean, isProcessingCollectionUpdate?: boolean): void; /** * Sends the data obtained from the keys to the connection. */ declare function sendDataToConnection(mapping: CallbackToStateMapping, matchedKey: TKey | undefined): void; /** * Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber. */ declare function getCollectionDataAndSendAsObject(matchingKeys: CollectionKeyBase[], mapping: CallbackToStateMapping): void; /** * Remove a key from Onyx and update the subscribers */ declare function remove(key: TKey, isProcessingCollectionUpdate?: boolean): Promise; declare function reportStorageQuota(error?: Error): Promise; /** * Handles storage operation failures based on the error type: * - Storage capacity errors: evicts data and retries the operation * - Invalid data errors: logs an alert and throws an error * - Other errors: retries the operation */ declare function retryOperation(error: Error, onyxMethod: TMethod, defaultParams: Parameters[0], retryAttempt: number | undefined): Promise; /** * Notifies subscribers and writes current value to cache */ declare function broadcastUpdate(key: TKey, value: OnyxValue, hasChanged?: boolean): void; declare function hasPendingMergeForKey(key: OnyxKey): boolean; /** * Storage expects array like: [["@MyApp_user", value_1], ["@MyApp_key", value_2]] * This method transforms an object like {'@MyApp_user': myUserValue, '@MyApp_key': myKeyValue} * to an array of key-value pairs in the above format and removes key-value pairs that are being set to null * * @return an array of key - value pairs <[key, value]> */ declare function prepareKeyValuePairsForStorage(data: Record>, shouldRemoveNestedNulls?: boolean, replaceNullPatches?: MultiMergeReplaceNullPatches, isProcessingCollectionUpdate?: boolean): StorageKeyValuePair[]; /** * Merges an array of changes with an existing value or creates a single change. * * @param changes Array of changes that should be merged * @param existingValue The existing value that should be merged with the changes */ declare function mergeChanges | undefined, TChange extends OnyxInput | undefined>(changes: TChange[], existingValue?: TValue): FastMergeResult; /** * Merges an array of changes with an existing value or creates a single change. * It will also mark deep nested objects that need to be entirely replaced during the merge. * * @param changes Array of changes that should be merged * @param existingValue The existing value that should be merged with the changes */ declare function mergeAndMarkChanges | undefined, TChange extends OnyxInput | undefined>(changes: TChange[], existingValue?: TValue): FastMergeResult; /** * Merge user provided default key value pairs. */ declare function initializeWithDefaultKeyStates(): Promise; /** * Validate the collection is not empty and has a correct type before applying mergeCollection() */ declare function isValidNonEmptyCollectionForMerge(collection: OnyxMergeCollectionInput): boolean; /** * Verify if all the collection keys belong to the same parent */ declare function doAllCollectionItemsBelongToSameParent(collectionKey: TKey, collectionKeys: string[]): boolean; /** * Subscribes to an Onyx key and listens to its changes. * * @param connectOptions The options object that will define the behavior of the connection. * @returns The subscription ID to use when calling `OnyxUtils.unsubscribeFromKey()`. */ declare function subscribeToKey(connectOptions: ConnectOptions): number; /** * Disconnects and removes the listener from the Onyx key. * * @param subscriptionID Subscription ID returned by calling `OnyxUtils.subscribeToKey()`. */ declare function unsubscribeFromKey(subscriptionID: number): void; declare function updateSnapshots(data: Array>, mergeFn: typeof Onyx.merge): Array<() => Promise>; /** * Writes a value to our store with the given key. * Serves as core implementation for `Onyx.set()` public function, the difference being * that this internal function allows passing an additional `retryAttempt` parameter to retry on failure. * * @param params - set parameters * @param params.key ONYXKEY to set * @param params.value value to store * @param params.options optional configuration object * @param retryAttempt retry attempt */ declare function setWithRetry({ key, value, options }: SetParams, retryAttempt?: number): Promise; /** * Sets multiple keys and values. * Serves as core implementation for `Onyx.multiSet()` public function, the difference being * that this internal function allows passing an additional `retryAttempt` parameter to retry on failure. * * @param data object keyed by ONYXKEYS and the values to set * @param retryAttempt retry attempt */ declare function multiSetWithRetry(data: OnyxMultiSetInput, retryAttempt?: number): Promise; /** * Sets a collection by replacing all existing collection members with new values. * Any existing collection members not included in the new data will be removed. * Serves as core implementation for `Onyx.setCollection()` public function, the difference being * that this internal function allows passing an additional `retryAttempt` parameter to retry on failure. * * @param params - collection parameters * @param params.collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` * @param params.collection Object collection keyed by individual collection member keys and values * @param retryAttempt retry attempt */ declare function setCollectionWithRetry({ collectionKey, collection }: SetCollectionParams, retryAttempt?: number): Promise; /** * Merges a collection based on their keys. * Serves as core implementation for `Onyx.mergeCollection()` public function, the difference being * that this internal function allows passing an additional `mergeReplaceNullPatches` parameter and retries on failure. * * @param params - mergeCollection parameters * @param params.collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` * @param params.collection Object collection keyed by individual collection member keys and values * @param params.mergeReplaceNullPatches Record where the key is a collection member key and the value is a list of * tuples that we'll use to replace the nested objects of that collection member record with something else. * @param params.isProcessingCollectionUpdate whether this is part of a collection update operation. * @param retryAttempt retry attempt */ declare function mergeCollectionWithPatches({ collectionKey, collection, mergeReplaceNullPatches, isProcessingCollectionUpdate }: MergeCollectionWithPatchesParams, retryAttempt?: number): Promise; /** * Sets keys in a collection by replacing all targeted collection members with new values. * Any existing collection members not included in the new data will not be removed. * Retries on failure. * * @param params - collection parameters * @param params.collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` * @param params.collection Object collection keyed by individual collection member keys and values * @param retryAttempt retry attempt */ declare function partialSetCollection({ collectionKey, collection }: SetCollectionParams, retryAttempt?: number): Promise; declare function logKeyChanged(onyxMethod: Extract, key: OnyxKey, value: unknown, hasChanged: boolean): void; declare function logKeyRemoved(onyxMethod: Extract, key: OnyxKey): void; /** * Getter - returns the callback to state mapping, useful in test environments. */ declare function getCallbackToStateMapping(): Record>; /** * Clear internal variables used in this file, useful in test environments. */ declare function clearOnyxUtilsInternals(): void; declare const OnyxUtils: { METHOD: { readonly SET: "set"; readonly MERGE: "merge"; readonly MERGE_COLLECTION: "mergecollection"; readonly SET_COLLECTION: "setcollection"; readonly MULTI_SET: "multiset"; readonly CLEAR: "clear"; }; getMergeQueue: typeof getMergeQueue; getMergeQueuePromise: typeof getMergeQueuePromise; getDefaultKeyStates: typeof getDefaultKeyStates; getDeferredInitTask: typeof getDeferredInitTask; afterInit: typeof afterInit; initStoreValues: typeof initStoreValues; sendActionToDevTools: typeof sendActionToDevTools; get: typeof get; getAllKeys: typeof getAllKeys; tryGetCachedValue: typeof tryGetCachedValue; getCachedCollection: typeof getCachedCollection; keysChanged: typeof keysChanged; keyChanged: typeof keyChanged; sendDataToConnection: typeof sendDataToConnection; getCollectionDataAndSendAsObject: typeof getCollectionDataAndSendAsObject; remove: typeof remove; reportStorageQuota: typeof reportStorageQuota; retryOperation: typeof retryOperation; broadcastUpdate: typeof broadcastUpdate; hasPendingMergeForKey: typeof hasPendingMergeForKey; prepareKeyValuePairsForStorage: typeof prepareKeyValuePairsForStorage; mergeChanges: typeof mergeChanges; mergeAndMarkChanges: typeof mergeAndMarkChanges; initializeWithDefaultKeyStates: typeof initializeWithDefaultKeyStates; getSnapshotKey: typeof getSnapshotKey; multiGet: typeof multiGet; tupleGet: typeof tupleGet; isValidNonEmptyCollectionForMerge: typeof isValidNonEmptyCollectionForMerge; doAllCollectionItemsBelongToSameParent: typeof doAllCollectionItemsBelongToSameParent; subscribeToKey: typeof subscribeToKey; unsubscribeFromKey: typeof unsubscribeFromKey; getSkippableCollectionMemberIDs: typeof getSkippableCollectionMemberIDs; setSkippableCollectionMemberIDs: typeof setSkippableCollectionMemberIDs; getSnapshotMergeKeys: typeof getSnapshotMergeKeys; setSnapshotMergeKeys: typeof setSnapshotMergeKeys; storeKeyBySubscriptions: typeof storeKeyBySubscriptions; deleteKeyBySubscriptions: typeof deleteKeyBySubscriptions; reduceCollectionWithSelector: typeof reduceCollectionWithSelector; updateSnapshots: typeof updateSnapshots; mergeCollectionWithPatches: typeof mergeCollectionWithPatches; partialSetCollection: typeof partialSetCollection; logKeyChanged: typeof logKeyChanged; logKeyRemoved: typeof logKeyRemoved; setWithRetry: typeof setWithRetry; multiSetWithRetry: typeof multiSetWithRetry; setCollectionWithRetry: typeof setCollectionWithRetry; getCallbackToStateMapping: typeof getCallbackToStateMapping; }; export type { OnyxMethod }; export default OnyxUtils; export { clearOnyxUtilsInternals };