@muban/muban
Version:
Writing components for server-rendered HTML
20 lines (19 loc) • 540 B
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
const memory = new WeakMap();
/**
* Memo function which does one thing
* - only uses first parameter as the cache-key
* - only accepts objects as cache-key (because of the `WeakMap`)
* @param fn
*/
export function simpleMemo(fn) {
return function (...rest) {
const key = rest[0];
if (memory.has(key)) {
return memory.get(key);
}
const result = fn.apply(this, rest);
memory.set(key, result);
return result;
};
}