From 0a32cd1d21d440d253e846e68ca13530f58d7705 Mon Sep 17 00:00:00 2001 From: Tom Boullay Date: Thu, 14 May 2026 00:17:44 +0200 Subject: [PATCH] feat: add map node caching for vegetation system --- src/utils/map/loadMapSceneData.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/utils/map/loadMapSceneData.ts b/src/utils/map/loadMapSceneData.ts index 8f334eb..b1a0cb4 100644 --- a/src/utils/map/loadMapSceneData.ts +++ b/src/utils/map/loadMapSceneData.ts @@ -7,7 +7,36 @@ const HTML_CONTENT_TYPE = "text/html"; const MAP_STRUCTURE_NODE_NAMES = new Set(["Scene", "blocking"]); type ModelEntry = [modelName: string, modelUrl: string]; +let cachedSceneData: SceneData | null = null; +let loadingPromise: Promise | null = null; + export async function loadMapSceneData(): Promise { + if (cachedSceneData) { + return cachedSceneData; + } + + if (loadingPromise) { + return loadingPromise; + } + + loadingPromise = loadMapSceneDataInternal(); + cachedSceneData = await loadingPromise; + loadingPromise = null; + + return cachedSceneData; +} + +export function getMapNodes(): MapNode[] | null { + return cachedSceneData?.mapNodes ?? null; +} + +export function getMapNodesByName(name: string): MapNode[] { + const nodes = cachedSceneData?.mapNodes; + if (!nodes) return []; + return nodes.filter((node) => node.name === name); +} + +async function loadMapSceneDataInternal(): Promise { const response = await fetch(MAP_JSON_PATH); if (!response.ok) {