From 1c03e7ab07e8dd68eb92a380179f95a407b52ed5 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 28 Mar 2024 15:53:41 +0100 Subject: [PATCH] Add timeout to loading the cache server status page, avoids long (>1minute) loading time if the server hangs --- src/index.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9728d5e15..a31fff4f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,16 +20,26 @@ function webgl_support() { return false } } + +async function timeout(timeMS: number): Promise<{ layers: string[] }> { + await Utils.waitFor(timeMS) + return { layers: [] } +} + async function getAvailableLayers(): Promise> { try { const host = new URL(Constants.VectorTileServer).host - const status = await Utils.downloadJson("https://" + host + "/summary/status.json") + const status: { layers: string[] } = await Promise.any([ + Utils.downloadJson("https://" + host + "/summary/status.json"), + timeout(5000) + ]) return new Set(status.layers) } catch (e) { console.error("Could not get MVT available layers due to", e) return new Set() } } + async function main() { // @ts-ignore try { @@ -38,7 +48,7 @@ async function main() { } const [layout, availableLayers] = await Promise.all([ DetermineLayout.GetLayout(), - await getAvailableLayers(), + await getAvailableLayers() ]) console.log("The available layers on server are", Array.from(availableLayers)) const state = new ThemeViewState(layout, availableLayers) @@ -55,16 +65,17 @@ async function main() { customDefinition?.length > 0 ? new SubtleButton(new SvelteUIElement(Download), "Download the raw file").onClick( - () => - Utils.offerContentsAsDownloadableFile( - DetermineLayout.getCustomDefinition(), - "mapcomplete-theme.json", - { mimetype: "application/json" } - ) - ) - : undefined, + () => + Utils.offerContentsAsDownloadableFile( + DetermineLayout.getCustomDefinition(), + "mapcomplete-theme.json", + { mimetype: "application/json" } + ) + ) + : undefined ]).AttachTo("maindiv") } } -main().then((_) => {}) +main().then((_) => { +})