import type { ClassId } from "../app/characterCatalog"; import { loadTalentUiCatalog, talentUiArtUrls } from "../game/talentUiCatalog"; let talentGameplayPanelModule: Promise | null = null; let loadedTalentGameplayPanel: typeof import("./TalentGameplayPanel")["default"] | null = null; const talentArtLoads = new Map>(); const retainedTalentArt = new Map(); const talentGameplayResourceLoads = new Map>(); 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 { 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((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 { 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; }