react-native-onyx
Version:
State management for React Native
83 lines (82 loc) • 4.25 kB
TypeScript
import type { ConnectOptions, OnyxInput, OnyxKey } from './types';
type EmptyObject = Record<string, never>;
type EmptyValue = EmptyObject | null | undefined;
/**
* A tuple where the first value is the path to the nested object that contains the
* internal `ONYX_INTERNALS__REPLACE_OBJECT_MARK` flag, and the second value is the data we want to replace
* in that path.
*
* This tuple will be used in SQLiteProvider to replace the nested object using `JSON_REPLACE`.
* */
type FastMergeReplaceNullPatch = [string[], unknown];
type FastMergeOptions = {
/** If true, null object values will be removed. */
shouldRemoveNestedNulls?: boolean;
/**
* If set to "mark", we will mark objects that are set to null instead of simply removing them,
* so that we can batch changes together, without losing information about the object removal.
* If set to "replace", we will completely replace the marked objects with the new value instead of merging them.
*/
objectRemovalMode?: 'mark' | 'replace' | 'none';
};
type FastMergeMetadata = {
/** The list of tuples that will be used in SQLiteProvider to replace the nested objects using `JSON_REPLACE`. */
replaceNullPatches: FastMergeReplaceNullPatch[];
};
type FastMergeResult<TValue> = {
/** The result of the merge. */
result: TValue;
/** The list of tuples that will be used in SQLiteProvider to replace the nested objects using `JSON_REPLACE`. */
replaceNullPatches: FastMergeReplaceNullPatch[];
};
/**
* Merges two objects and removes null values if "shouldRemoveNestedNulls" is set to true
*
* We generally want to remove null values from objects written to disk and cache, because it decreases the amount of data stored in memory and on disk.
*/
declare function fastMerge<TValue>(target: TValue, source: TValue, options?: FastMergeOptions, metadata?: FastMergeMetadata, basePath?: string[]): FastMergeResult<TValue>;
/** Checks whether the given object is an object and not null/undefined. */
declare function isEmptyObject<T>(obj: T | EmptyValue): obj is EmptyValue;
/** Deep removes the nested null values from the given value. */
declare function removeNestedNullValues<TValue extends OnyxInput<OnyxKey> | null>(value: TValue): TValue;
/** Formats the action name by uppercasing and adding the key if provided. */
declare function formatActionName(method: string, key?: OnyxKey): string;
/** validate that the update and the existing value are compatible */
declare function checkCompatibilityWithExistingValue(value: unknown, existingValue: unknown): {
isCompatible: boolean;
existingValueType?: string;
newValueType?: string;
};
/**
* Picks entries from an object based on a condition.
*
* @param obj - The object to pick entries from.
* @param condition - The condition to determine which entries to pick.
* @returns The object containing only the picked entries.
*/
declare function pick<TValue>(obj: Record<string, TValue>, condition: string | string[] | ((entry: [string, TValue]) => boolean)): Record<string, TValue>;
/**
* Omits entries from an object based on a condition.
*
* @param obj - The object to omit entries from.
* @param condition - The condition to determine which entries to omit.
* @returns The object containing only the remaining entries after omission.
*/
declare function omit<TValue>(obj: Record<string, TValue>, condition: string | string[] | ((entry: [string, TValue]) => boolean)): Record<string, TValue>;
/**
* Whether the connect options has the `withOnyxInstance` property defined, that is, it's used by the `withOnyx()` HOC.
*/
declare function hasWithOnyxInstance<TKey extends OnyxKey>(mapping: ConnectOptions<TKey>): unknown;
declare const _default: {
fastMerge: typeof fastMerge;
isEmptyObject: typeof isEmptyObject;
formatActionName: typeof formatActionName;
removeNestedNullValues: typeof removeNestedNullValues;
checkCompatibilityWithExistingValue: typeof checkCompatibilityWithExistingValue;
pick: typeof pick;
omit: typeof omit;
hasWithOnyxInstance: typeof hasWithOnyxInstance;
ONYX_INTERNALS__REPLACE_OBJECT_MARK: string;
};
export default _default;
export type { FastMergeResult, FastMergeReplaceNullPatch, FastMergeOptions };