diff options
Diffstat (limited to 'webAO/utils/fileExists.ts')
| -rw-r--r-- | webAO/utils/fileExists.ts | 18 |
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; } |
