@barchart/common-js
Version:
Library of common JavaScript utilities
28 lines (27 loc) • 899 B
TypeScript
/**
* Utilities for caching results of function invocations (a.k.a. memoization).
*
* @public
* @module lang/memoize
*/
/**
* Memoizes a function that accepts a single argument only. Furthermore,
* the parameter's toString function must return a unique value.
*
* @static
* @public
* @param {Function} fn - The function to memoize. This function should accept one parameter whose "toString" function outputs a unique value.
*/
export function simple(fn: Function): (x: any) => any;
/**
* Wraps a function. The resulting function will call the wrapped function
* once and cache the result. If a specific duration is supplied, the
* cache will be dropped after the duration expires and the wrapped
* function will be invoked again.
*
* @public
* @param {Function} fn
* @param {number} duration
* @returns {Function}
*/
export function cache(fn: Function, duration: number): Function;