Auto-formatting

This commit is contained in:
Pieter Vander Vennet 2022-12-16 13:45:07 +01:00
parent 9e000d521f
commit fed4cff878
26 changed files with 360 additions and 304 deletions

View file

@ -1,6 +1,5 @@
import * as colors from "./assets/colors.json"
export class Utils {
/**
* In the 'deploy'-step, some code needs to be run by ts-node.
@ -139,7 +138,13 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
"false",
]
private static injectedDownloads = {}
private static _download_cache = new Map<string, { promise: Promise<any | {error: string, url: string, statuscode?: number}>; timestamp: number }>()
private static _download_cache = new Map<
string,
{
promise: Promise<any | { error: string; url: string; statuscode?: number }>
timestamp: number
}
>()
/**
* Parses the arguments for special visualisations
@ -570,7 +575,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
object[head] = leaf.map((o) => replaceLeaf(o, travelledPath))
} else {
object[head] = replaceLeaf(leaf, travelledPath)
if(object[head] === undefined){
if (object[head] === undefined) {
delete object[head]
}
}
@ -803,7 +808,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
public static async download(url: string, headers?: any): Promise<string | undefined> {
const result = await Utils.downloadAdvanced(url, headers)
if(result["error"] !== undefined){
if (result["error"] !== undefined) {
throw result["error"]
}
return result["content"]
@ -816,8 +821,12 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
*/
public static downloadAdvanced(
url: string,
headers?: any,
): Promise<{ content: string } | { redirect: string } | { error: string,url: string, statuscode?: number}> {
headers?: any
): Promise<
| { content: string }
| { redirect: string }
| { error: string; url: string; statuscode?: number }
> {
if (this.externalDownloadFunction !== undefined) {
return this.externalDownloadFunction(url, headers)
}
@ -831,9 +840,13 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
} else if (xhr.status === 302) {
resolve({ redirect: xhr.getResponseHeader("location") })
} else if (xhr.status === 509 || xhr.status === 429) {
resolve ({error: "rate limited", url, statuscode: xhr.status})
resolve({ error: "rate limited", url, statuscode: xhr.status })
} else {
resolve ({error: "other error: "+xhr.statusText, url, statuscode: xhr.status})
resolve({
error: "other error: " + xhr.statusText,
url,
statuscode: xhr.status,
})
}
}
xhr.open("GET", url)
@ -878,7 +891,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
headers?: any
): Promise<any> {
const result = await Utils.downloadJsonAdvanced(url, headers)
if(result["content"]){
if (result["content"]) {
return result["content"]
}
throw result["error"]
@ -888,56 +901,58 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
url: string,
maxCacheTimeMs: number,
headers?: any
): Promise<any | {error: string, url: string, statuscode?: number}> {
): Promise<any | { error: string; url: string; statuscode?: number }> {
const cached = Utils._download_cache.get(url)
if (cached !== undefined) {
if (new Date().getTime() - cached.timestamp <= maxCacheTimeMs) {
return cached.promise
}
}
const promise = /*NO AWAIT as we work with the promise directly */ Utils.downloadJsonAdvanced(
url,
headers
)
const promise =
/*NO AWAIT as we work with the promise directly */ Utils.downloadJsonAdvanced(
url,
headers
)
Utils._download_cache.set(url, { promise, timestamp: new Date().getTime() })
return await promise
}
public static async downloadJson(url: string, headers?: any): Promise<any> {
const result = await Utils.downloadJsonAdvanced(url, headers)
if(result["content"]){
if (result["content"]) {
return result["content"]
}
throw result["error"]
}
public static async downloadJsonAdvanced(url: string, headers?: any): Promise<{content: any} | {error: string, url: string, statuscode?: number}> {
public static async downloadJsonAdvanced(
url: string,
headers?: any
): Promise<{ content: any } | { error: string; url: string; statuscode?: number }> {
const injected = Utils.injectedDownloads[url]
if (injected !== undefined) {
console.log("Using injected resource for test for URL", url)
return new Promise((resolve, _) => resolve({content: injected}))
return new Promise((resolve, _) => resolve({ content: injected }))
}
const result = await Utils.downloadAdvanced(
url,
Utils.Merge({ accept: "application/json" }, headers ?? {})
)
if(result["error"] !== undefined){
return <{error: string, url: string, statuscode?: number}> result
if (result["error"] !== undefined) {
return <{ error: string; url: string; statuscode?: number }>result
}
const data = result["content"]
try {
if (typeof data === "string") {
return {content: JSON.parse(data)}
return { content: JSON.parse(data) }
}
return {"content": data}
return { content: data }
} catch (e) {
console.error("Could not parse ", data, "due to", e, "\n", e.stack)
return {error: "malformed", url}
return { error: "malformed", url }
}
}
/**
* Triggers a 'download file' popup which will download the contents
*/
@ -1261,16 +1276,15 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
public static findParentWithScrolling(element: HTMLElement): HTMLElement {
// Check if the element itself has scrolling
if (element.scrollHeight > element.clientHeight) {
return element;
return element
}
// If the element does not have scrolling, check if it has a parent element
if (!element.parentElement) {
return null;
return null
}
// If the element has a parent, repeat the process for the parent element
return Utils.findParentWithScrolling(element.parentElement);
return Utils.findParentWithScrolling(element.parentElement)
}
}