aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils/fileExists.ts
diff options
context:
space:
mode:
authorstonedDiscord <Tukz@gmx.de>2026-04-06 20:38:41 +0200
committerGitHub <noreply@github.com>2026-04-06 20:38:41 +0200
commit4be4f4665fe03a0267ac88c36f0e3b73d8fc2d48 (patch)
treeb76ef7b627523a8daebe0beb59e404d3da82d04e /webAO/utils/fileExists.ts
parent815f56add06b92a48b964cb1343f70c86ea36435 (diff)
parent20810aa0d3dfac49e1f43fe84634f74f56374fcd (diff)
Merge pull request #301 from AttorneyOnline/rendering-fix
Fix IC rendering race conditions with asset preloading
Diffstat (limited to 'webAO/utils/fileExists.ts')
-rw-r--r--webAO/utils/fileExists.ts18
1 files changed, 11 insertions, 7 deletions
diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts
index 1dceb72..748bc1f 100644
--- a/webAO/utils/fileExists.ts
+++ b/webAO/utils/fileExists.ts
@@ -1,14 +1,15 @@
-export default async function fileExists(url: string): Promise<boolean> {
- return new Promise((resolve) => {
+const cache = new Map<string, Promise<boolean>>();
+
+export default function fileExists(url: string): Promise<boolean> {
+ const cached = cache.get(url);
+ if (cached !== undefined) return cached;
+
+ const promise = new Promise<boolean>((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);
- }
+ resolve(xhr.status === 200);
}
};
xhr.onerror = function checkError() {
@@ -16,4 +17,7 @@ export default async function fileExists(url: string): Promise<boolean> {
};
xhr.send(null);
});
+
+ cache.set(url, promise);
+ return promise;
}