Files
Sable/src/app/hooks/useBlobCache.ts
T

69 lines
1.7 KiB
TypeScript

import { useState, useEffect } from 'react';
const imageBlobCache = new Map<string, string>();
const inflightRequests = new Map<string, Promise<string>>();
export function useBlobCache(url?: string): string | undefined {
const [cacheState, setCacheState] = useState<{ sourceUrl?: string; blobUrl?: string }>({
sourceUrl: url,
blobUrl: url ? imageBlobCache.get(url) : undefined,
});
if (url !== cacheState.sourceUrl) {
setCacheState({
sourceUrl: url,
blobUrl: url ? imageBlobCache.get(url) : undefined,
});
}
useEffect(() => {
if (!url || imageBlobCache.has(url)) return;
let isMounted = true;
const fetchBlob = async () => {
if (inflightRequests.has(url)) {
const existingBlobUrl = await inflightRequests.get(url);
if (isMounted) setCacheState({ sourceUrl: url, blobUrl: existingBlobUrl });
return;
}
const requestPromise = (async () => {
try {
const res = await fetch(url, { mode: 'cors' });
if (!res.ok) throw new Error();
const blob = await res.blob();
const objectUrl = URL.createObjectURL(blob);
imageBlobCache.set(url, objectUrl);
return objectUrl;
} catch (e) {
inflightRequests.delete(url);
throw e;
}
})();
inflightRequests.set(url, requestPromise);
try {
const finalBlobUrl = await requestPromise;
if (isMounted) {
setCacheState({ sourceUrl: url, blobUrl: finalBlobUrl });
}
} catch (err) {
// silency fail... mrow
} finally {
inflightRequests.delete(url);
}
};
fetchBlob();
return () => {
isMounted = false;
};
}, [url]);
return cacheState.blobUrl || url;
}