mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 22:32:28 +00:00
Co-authored-by: Github Action <action@github.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Brendan Allan <git@brendonovich.dev>
24 lines
410 B
TypeScript
24 lines
410 B
TypeScript
export function lazy<T>(fn: () => T) {
|
|
let value: T | undefined
|
|
let loaded = false
|
|
|
|
const result = (): T => {
|
|
if (loaded) return value as T
|
|
try {
|
|
value = fn()
|
|
loaded = true
|
|
return value as T
|
|
} catch (e) {
|
|
// Don't mark as loaded if initialization failed
|
|
throw e
|
|
}
|
|
}
|
|
|
|
result.reset = () => {
|
|
loaded = false
|
|
value = undefined
|
|
}
|
|
|
|
return result
|
|
}
|