chore: init clean tree

This commit is contained in:
2025-12-17 23:19:04 +02:00
commit 01d96d3200
45 changed files with 4152 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
export type LazyResource<T> = {
load: () => Promise<T>;
unload: () => void;
isLoaded: () => boolean;
};
export function createLazyResource<T>(
loader: () => Promise<T>,
dispose?: (value: T) => void
): LazyResource<T> {
let cached: T | null = null;
let inflight: Promise<T> | null = null;
const load = async (): Promise<T> => {
if (cached) return cached;
if (inflight) return inflight;
inflight = (async () => {
const value = await loader();
cached = value;
inflight = null;
return value;
})();
return inflight;
};
const unload = () => {
if (cached && dispose) {
try {
dispose(cached);
} catch (error) {
console.error("[lazy-resource] failed to dispose resource", error);
}
}
cached = null;
inflight = null;
};
return {
load,
unload,
isLoaded: () => cached !== null
};
}