UNPKG

2.67 kBTypeScriptView Raw
1/**
2 * Test utility for providing a custom weakmap.
3 *
4 * @internal
5 * */
6export declare function setMemoizeWeakMap(weakMap: any): void;
7/**
8 * Reset memoizations.
9 */
10export declare function resetMemoizations(): void;
11/**
12 * Memoize decorator to be used on class methods. WARNING: the `this` reference
13 * will be inaccessible within a memoized method, given that a cached method's `this`
14 * would not be instance-specific.
15 *
16 * @public
17 */
18export declare function memoize<T extends Function>(target: any, key: string, descriptor: TypedPropertyDescriptor<T>): {
19 configurable: boolean;
20 get(): T;
21};
22/**
23 * Memoizes a function; when you pass in the same parameters multiple times, it returns a cached result.
24 * Be careful when passing in objects, you need to pass in the same INSTANCE for caching to work. Otherwise
25 * it will grow the cache unnecessarily. Also avoid using default values that evaluate functions; passing in
26 * undefined for a value and relying on a default function will execute it the first time, but will not
27 * re-evaluate subsequent times which may have been unexpected.
28 *
29 * By default, the cache will reset after 100 permutations, to avoid abuse cases where the function is
30 * unintendedly called with unique objects. Without a reset, the cache could grow infinitely, so we safeguard
31 * by resetting. To override this behavior, pass a value of 0 to the maxCacheSize parameter.
32 *
33 * @public
34 * @param cb - The function to memoize.
35 * @param maxCacheSize - Max results to cache. If the cache exceeds this value, it will reset on the next call.
36 * @param ignoreNullOrUndefinedResult - Flag to decide whether to cache callback result if it is undefined/null.
37 * If the flag is set to true, the callback result is recomputed every time till the callback result is
38 * not undefined/null for the first time, and then the non-undefined/null version gets cached.
39 * @returns A memoized version of the function.
40 */
41export declare function memoizeFunction<T extends (...args: any[]) => RetType, RetType>(cb: T, maxCacheSize?: number, ignoreNullOrUndefinedResult?: boolean): T;
42/**
43 * Creates a memoizer for a single-value function, backed by a WeakMap.
44 * With a WeakMap, the memoized values are only kept as long as the source objects,
45 * ensuring that there is no memory leak.
46 *
47 * This function assumes that the input values passed to the wrapped function will be
48 * `function` or `object` types. To memoize functions which accept other inputs, use
49 * `memoizeFunction`, which memoizes against arbitrary inputs using a lookup cache.
50 *
51 * @public
52 */
53export declare function createMemoizer<F extends (input: any) => any>(getValue: F): F;