(fn)
| 17 | * @returns {FunctionReturning<T>} new function |
| 18 | */ |
| 19 | const memoize = (fn) => { |
| 20 | let cache = false; |
| 21 | /** @type {T | undefined} */ |
| 22 | let result; |
| 23 | return () => { |
| 24 | if (cache) { |
| 25 | return /** @type {T} */ (result); |
| 26 | } |
| 27 | |
| 28 | result = fn(); |
| 29 | cache = true; |
| 30 | // Allow to clean up memory for fn |
| 31 | // and all dependent resources |
| 32 | /** @type {FunctionReturning<T> | undefined} */ |
| 33 | (fn) = undefined; |
| 34 | return /** @type {T} */ (result); |
| 35 | }; |
| 36 | }; |
| 37 | |
| 38 | module.exports = memoize; |
no test coverage detected