UNPKG

@conform-to/react

Version:

Conform view adapter for react

49 lines 1.84 kB
/** * A memoized function with cache clearing capability. */ export type Memoized<T extends (...args: any) => any> = { (this: ThisParameterType<T>, ...args: Parameters<T>): ReturnType<T>; /** Clears the memoization cache */ clearCache: () => void; }; /** * Default equality check that compares arguments using Object.is(). * * @param prevArgs - Previous function arguments * @param nextArgs - Current function arguments * @returns True if all arguments are equal */ export declare function defaultEqualityCheck(prevArgs: any[], nextArgs: any[]): boolean; /** * Memoizes function calls, caching only the most recent result to prevent redundant async validations. * * Built-in implementation based on memoize-one with enhanced async support. * Can be replaced with other memoization libraries if needed. * * @param fn - The function to memoize * @param isEqual - Custom equality function to compare arguments (defaults to shallow comparison) * @returns Memoized function with cache clearing capability * * @example * ```ts * // Async validation with API call * const validateUsername = useMemo( * () => memoize(async function isUnique(username: string) { * const response = await fetch(`/api/users/${username}`); * return response.ok ? null : ['Username is already taken']; * }), * [] * ); * * // Usage in form validation * async onValidate({ payload, error }) { * if (payload.username && !error.fieldErrors.username) { * const messages = await validateUsername(value.username); * if (messages) error.fieldErrors.username = messages; * } * return error; * } * ``` */ export declare function memoize<T extends (...args: any) => any>(fn: T, isEqual?: (prevArgs: Parameters<T>, nextArgs: Parameters<T>) => boolean): Memoized<T>; //# sourceMappingURL=memoize.d.ts.map