mirror of https://github.com/openclaw/openclaw.git
25 lines
658 B
TypeScript
25 lines
658 B
TypeScript
type LazyValue<T> = T | (() => T);
|
|
|
|
export function createCachedLazyValueGetter<T>(value: LazyValue<T>): () => T;
|
|
export function createCachedLazyValueGetter<T>(
|
|
value: LazyValue<T | null | undefined>,
|
|
fallback: T,
|
|
): () => T;
|
|
export function createCachedLazyValueGetter<T>(
|
|
value: LazyValue<T | null | undefined>,
|
|
fallback?: T,
|
|
): () => T | undefined {
|
|
let resolved = false;
|
|
let cached: T | undefined;
|
|
|
|
return () => {
|
|
if (!resolved) {
|
|
const nextValue =
|
|
typeof value === "function" ? (value as () => T | null | undefined)() : value;
|
|
cached = nextValue ?? fallback;
|
|
resolved = true;
|
|
}
|
|
return cached;
|
|
};
|
|
}
|