ximg: minor refactor

Signed-off-by: Varun Patil <radialapps@gmail.com>
monorepo
Varun Patil 2023-10-31 21:46:21 -07:00
parent e2d4a53c66
commit 44c3ed1b86
1 changed files with 3 additions and 8 deletions

View File

@ -66,9 +66,7 @@ async function flushPreviewQueue() {
// it came from a multipreview, so that we can try fetching // it came from a multipreview, so that we can try fetching
// the single image instead // the single image instead
blob ??= await res.blob(); blob ??= await res.blob();
pendingUrls.get(url)?.forEach((cb) => { pendingUrls.get(url)?.forEach((cb) => cb?.resolve?.(blob!));
cb?.resolve?.(blob!);
});
pendingUrls.delete(url); pendingUrls.delete(url);
// Cache response // Cache response
@ -77,9 +75,7 @@ async function flushPreviewQueue() {
// Throw error on URL // Throw error on URL
const reject = (url: string, e: any): void => { const reject = (url: string, e: any): void => {
pendingUrls.get(url)?.forEach((cb) => { pendingUrls.get(url)?.forEach((cb) => cb?.reject?.(e));
cb?.reject?.(e);
});
pendingUrls.delete(url); pendingUrls.delete(url);
}; };
@ -218,7 +214,6 @@ async function fetchImage(url: string): Promise<Blob> {
// Just fetch if not a preview // Just fetch if not a preview
const regex = /\/memories\/api\/image\/preview\/\d+(\?.*)?$/; const regex = /\/memories\/api\/image\/preview\/\d+(\?.*)?$/;
if (!regex.test(url)) { if (!regex.test(url)) {
const res = await fetchOneImage(url); const res = await fetchOneImage(url);
cacheResponse(url, res); cacheResponse(url, res);
@ -250,7 +245,7 @@ async function fetchImage(url: string): Promise<Blob> {
fetchPreviewTimer = self.setTimeout(flushPreviewQueue, 20); fetchPreviewTimer = self.setTimeout(flushPreviewQueue, 20);
} }
// If queue has >10 items, flush immediately // If queue has >20 items, flush immediately
// This will internally clear the timer // This will internally clear the timer
if (fetchPreviewQueue.length >= 20) { if (fetchPreviewQueue.length >= 20) {
flushPreviewQueue(); flushPreviewQueue();