Files
healer-man/src/ui/talentGameplayResources.ts
T
2026-09-09 11:21:33 -04:00

72 lines
2.3 KiB
TypeScript

import type { ClassId } from "../app/characterCatalog";
import { loadTalentUiCatalog, talentUiArtUrls } from "../game/talentUiCatalog";
let talentGameplayPanelModule: Promise<typeof import("./TalentGameplayPanel")> | null = null;
let loadedTalentGameplayPanel: typeof import("./TalentGameplayPanel")["default"] | null = null;
const talentArtLoads = new Map<string, Promise<void>>();
const retainedTalentArt = new Map<string, HTMLImageElement>();
const talentGameplayResourceLoads = new Map<ClassId, Promise<void>>();
export function loadTalentGameplayPanel() {
if (!talentGameplayPanelModule) {
talentGameplayPanelModule = import("./TalentGameplayPanel")
.then((module) => {
loadedTalentGameplayPanel = module.default;
return module;
})
.catch((error: unknown) => {
talentGameplayPanelModule = null;
throw error;
});
}
return talentGameplayPanelModule;
}
export function getLoadedTalentGameplayPanel() {
return loadedTalentGameplayPanel;
}
function preloadTalentArt(url: string): Promise<void> {
const cached = talentArtLoads.get(url);
if (cached) return cached;
if (typeof Image === "undefined") return Promise.resolve();
const image = new Image();
retainedTalentArt.set(url, image);
const pending = new Promise<void>((resolve) => {
image.onload = () => {
image.onload = null;
image.onerror = null;
if (typeof image.decode === "function") {
void image.decode().catch(() => undefined).then(() => resolve());
} else {
resolve();
}
};
image.onerror = () => {
image.onload = null;
image.onerror = null;
resolve();
};
image.src = url;
});
talentArtLoads.set(url, pending);
return pending;
}
export function preloadTalentGameplayResources(classId: ClassId): Promise<void> {
const cached = talentGameplayResourceLoads.get(classId);
if (cached) return cached;
const pending = Promise.all([
loadTalentUiCatalog(classId),
loadTalentGameplayPanel(),
]).then(async ([catalog]) => {
await Promise.all(talentUiArtUrls(catalog).map(preloadTalentArt));
}).catch((error: unknown) => {
talentGameplayResourceLoads.delete(classId);
throw error;
});
talentGameplayResourceLoads.set(classId, pending);
return pending;
}