mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-20 17:55:20 +03:00
feat: Sync to OSS repo
This commit is contained in:
18
packages/core/src/utils/memo.ts
Normal file
18
packages/core/src/utils/memo.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export function memo<T>(fn: () => T, cleanup?: (input: T) => Promise<void>) {
|
||||
let value: T | undefined;
|
||||
let loaded = false;
|
||||
|
||||
const result = (): T => {
|
||||
if (loaded) return value as T;
|
||||
loaded = true;
|
||||
value = fn();
|
||||
return value as T;
|
||||
};
|
||||
result.reset = async () => {
|
||||
if (cleanup && value) await cleanup(value);
|
||||
loaded = false;
|
||||
value = undefined;
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user