( func: T, computeCacheKey: (...args: Parameters<T>) => string = defaultComputeCacheKey, )
| 11 | // reuse previous promise when a request call |
| 12 | // and previous request not completed |
| 13 | export const reuseable = <T extends (...args: any[]) => Promise<any>>( |
| 14 | func: T, |
| 15 | computeCacheKey: (...args: Parameters<T>) => string = defaultComputeCacheKey, |
| 16 | ) => { |
| 17 | const cache = new Map<string, ReturnType<T>>(); |
| 18 | |
| 19 | return function (...args: Parameters<T>): ReturnType<T> { |
| 20 | const key = computeCacheKey(...args); |
| 21 | if (cache.has(key)) { |
| 22 | return cache.get(key)!; |
| 23 | } |
| 24 | |
| 25 | const promise = func.call(this, ...args); |
| 26 | cache.set(key, promise); |
| 27 | return pFinally(promise, () => cache.delete(key)); |
| 28 | }; |
| 29 | }; |
| 30 | |
| 31 | export const throttle = <T extends (...args: any[]) => any>(func: T, interval: number) => { |
| 32 | let timer: ReturnType<typeof setTimeout> | null = null; |
no test coverage detected