1/** `lodash.memoize` */ 2export function memoize<T extends (...args: any[]) => any>(fn: T): T { 3 const cache: { [key: string]: any } = {}; 4 return ((...args: any[]) => { 5 const key = JSON.stringify(args); 6 if (cache[key]) { 7 return cache[key]; 8 } 9 const result = fn(...args); 10 cache[key] = result; 11 return result; 12 }) as any; 13} 14 15/** memoizes an async function to prevent subsequent calls that might be invoked before the function has finished resolving. */ 16export function guardAsync<V, T extends (...args: any[]) => Promise<V>>(fn: T): T { 17 let invoked = false; 18 let returnValue: V; 19 20 const guard: any = async (...args: any[]): Promise<V> => { 21 if (!invoked) { 22 invoked = true; 23 returnValue = await fn(...args); 24 } 25 26 return returnValue; 27 }; 28 29 return guard; 30} 31