mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 14:22:27 +00:00
19 lines
301 B
TypeScript
19 lines
301 B
TypeScript
export function lazy<T>(fn: () => T) {
|
|
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 = () => {
|
|
loaded = false
|
|
value = undefined
|
|
}
|
|
|
|
return result
|
|
}
|