0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 05:28:35 +00:00
renovatebot_renovate/lib/util/memoize.ts
Sergei Zharinov 608d0f6a40
feat: Create utility for function memoization (#15373)
* feat: Create utility for function memoization

* Simplify
2022-05-01 06:00:55 +02:00

18 lines
287 B
TypeScript

interface Result<T = unknown> {
res: T;
}
export function memoize<T = unknown>(callback: () => T): () => T {
let memo: null | Result<T> = null;
return (): T => {
if (memo) {
return memo.res;
}
const res = callback();
memo = { res };
return res;
};
}