From 4d2e26939647281e5a814f7089c8b7cd54f63171 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 22:54:58 +0100 Subject: [PATCH 1/8] minor formatting --- webAO/viewport/utils/setSide.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/webAO/viewport/utils/setSide.ts b/webAO/viewport/utils/setSide.ts index 3726e83d..bff3ba56 100644 --- a/webAO/viewport/utils/setSide.ts +++ b/webAO/viewport/utils/setSide.ts @@ -21,7 +21,7 @@ export const set_side = async ({ }) => { const view = document.getElementById("client_fullview")!; let bench: HTMLImageElement; - if (['def','pro','wit'].includes(position)) { + if (['def', 'pro', 'wit'].includes(position)) { bench = ( document.getElementById(`client_${position}_bench`) ); @@ -57,8 +57,7 @@ export const set_side = async ({ } else { court.src = await tryUrls(client.viewport.getBackgroundFolder() + bg); } - - + if (showDesk === true && desk) { const deskFilename = (await fileExists(client.viewport.getBackgroundFolder() + desk.ao2)) ? desk.ao2 From 477b93638c035ae9772376736726dbdc51845f51 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:10:57 +0100 Subject: [PATCH 2/8] Rename fileExists.js to fileExists.ts --- webAO/utils/{fileExists.js => fileExists.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename webAO/utils/{fileExists.js => fileExists.ts} (100%) diff --git a/webAO/utils/fileExists.js b/webAO/utils/fileExists.ts similarity index 100% rename from webAO/utils/fileExists.js rename to webAO/utils/fileExists.ts From 7661b6c7314327b6b8523976ba89755db4d281b9 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:14:35 +0100 Subject: [PATCH 3/8] Simplify and make fileExists proper typescript --- webAO/utils/fileExists.ts | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index 5b609764..06621e44 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -1,18 +1,7 @@ -const fileExists = async (url) => new Promise((resolve) => { - const xhr = new XMLHttpRequest(); - xhr.open('HEAD', url); - xhr.onload = function checkLoad() { - if (xhr.readyState === 4) { - if (xhr.status === 200) { - resolve(true); - } else { - resolve(false); - } - } - }; - xhr.onerror = function checkError() { - resolve(false); - }; - xhr.send(null); -}); -export default fileExists; +export default async function fileExists(url: string): Promise { + return fetch(url, { + method: 'HEAD', + }).then((response) => { + return response.ok; + }); +} From 26feae73cc8b30f4a9b1189bc87d41d8e1ee2a7d Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:37:11 +0100 Subject: [PATCH 4/8] Add filesExist --- webAO/utils/filesExist.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 webAO/utils/filesExist.ts diff --git a/webAO/utils/filesExist.ts b/webAO/utils/filesExist.ts new file mode 100644 index 00000000..2f394275 --- /dev/null +++ b/webAO/utils/filesExist.ts @@ -0,0 +1,28 @@ +import fileExists from "./fileExists"; + +/** + * This function takes a list of urls and returns the first one that exists. + * It checks all the URLs in parallel. + * @param urls the list of URLs to check + * @returns either the first URL that exists or null if none were found + */ +export default async function filesExist(urls: string[]): Promise { + const promises = urls.map(async (url) => { + if (await fileExists(url)) { + return url; + } + return null; + }); + + // Run all in parallel + const results = await Promise.all(promises); + + // Find the first URL that exists (not null) or return null if none exist + for (const result of results) { + if (result !== null) { + return result; + } + } + + return null; // None of the URLs exist +} From 0688c1852eaf8024ea041149303df63050ffe186 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:45:28 +0100 Subject: [PATCH 5/8] Add findImgSrc --- webAO/utils/findImgSrc.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 webAO/utils/findImgSrc.ts diff --git a/webAO/utils/findImgSrc.ts b/webAO/utils/findImgSrc.ts new file mode 100644 index 00000000..b4db849d --- /dev/null +++ b/webAO/utils/findImgSrc.ts @@ -0,0 +1,19 @@ +import filesExist from "./filesExist"; +import transparentPng from '../constants/transparentPng' + +/** + * This function takes a list of urls and returns the first one that exists. + * If none is found, return a transparent png. + * The function will always return a value that is appriopriate for an img src. + * @param urls The list of urls to try + * @returns The image source of the first url that exists, or a transparent png if none exist + */ +export default async function findImgSrc(urls: string[]): Promise { + return filesExist(urls).then((url) => { + if (url !== null) { + return url; + } + // If none of the images exist, return a transparent png + return transparentPng; + }); +} From 5bb9834817ab14aba2517f7ff76bb5eb97ea0bee Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:51:08 +0100 Subject: [PATCH 6/8] Use findImgSrc in setSide --- webAO/viewport/utils/setSide.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/webAO/viewport/utils/setSide.ts b/webAO/viewport/utils/setSide.ts index bff3ba56..77d17445 100644 --- a/webAO/viewport/utils/setSide.ts +++ b/webAO/viewport/utils/setSide.ts @@ -2,7 +2,7 @@ import { positions } from '../constants/positions' import { AO_HOST } from '../../client/aoHost' import { client } from '../../client' import tryUrls from '../../utils/tryUrls'; -import fileExists from '../../utils/fileExists'; +import findImgSrc from '../../utils/findImgSrc'; /** * Changes the viewport background based on a given position. @@ -59,10 +59,12 @@ export const set_side = async ({ } if (showDesk === true && desk) { - const deskFilename = (await fileExists(client.viewport.getBackgroundFolder() + desk.ao2)) - ? desk.ao2 - : desk.ao1; - bench.src = client.viewport.getBackgroundFolder() + deskFilename; + const bg_folder = client.viewport.getBackgroundFolder(); + const urls_to_try = [ + bg_folder + desk.ao2, + bg_folder + desk.ao1, + ]; + bench.src = await findImgSrc(urls_to_try); bench.style.opacity = "1"; } else { bench.style.opacity = "0"; From 4766a801382e6373b982a2731cdc7ea24f0c70b3 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:54:23 +0100 Subject: [PATCH 7/8] Catch errors in fileExists --- webAO/utils/fileExists.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index 06621e44..f1a9924d 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -3,5 +3,7 @@ export default async function fileExists(url: string): Promise { method: 'HEAD', }).then((response) => { return response.ok; + }).catch(() => { + return false; }); } From 90ed4b74cac7e8e410175cc3445d7140671e221f Mon Sep 17 00:00:00 2001 From: David Skoland Date: Thu, 30 Nov 2023 00:35:08 +0100 Subject: [PATCH 8/8] Revert fileExists --- webAO/utils/fileExists.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index f1a9924d..abb29284 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -1,9 +1,19 @@ export default async function fileExists(url: string): Promise { - return fetch(url, { - method: 'HEAD', - }).then((response) => { - return response.ok; - }).catch(() => { - return false; + return new Promise((resolve) => { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', url); + xhr.onload = function checkLoad() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(true); + } else { + resolve(false); + } + } + }; + xhr.onerror = function checkError() { + resolve(false); + }; + xhr.send(null); }); }