mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-26 04:04:22 +01:00
wip: cloud
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
export function memo<T>(fn: () => T) {
|
||||
export function memo<T>(fn: () => T, cleanup?: (input: T) => Promise<void>) {
|
||||
let value: T | undefined
|
||||
let loaded = false
|
||||
|
||||
return (): T => {
|
||||
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