Add timeout to loading the cache server status page, avoids long (>1minute) loading time if the server hangs
This commit is contained in:
parent
2b9ccd4b34
commit
1c03e7ab07
1 changed files with 22 additions and 11 deletions
33
src/index.ts
33
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<Set<string>> {
|
||||
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<string>(status.layers)
|
||||
} catch (e) {
|
||||
console.error("Could not get MVT available layers due to", e)
|
||||
return new Set<string>()
|
||||
}
|
||||
}
|
||||
|
||||
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((_) => {
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue