Browse Source

fix: clean up pending thumbnail map on rejection to prevent stale entries

Move removePending to .finally() so it runs on both resolve and reject,
preventing stale rejected promises from accumulating in the pending map.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
handoff-20260429-1057
shrimbly 4 months ago
parent
commit
df903c1469
  1. 35
      src/hooks/useAdaptiveImageSrc.ts

35
src/hooks/useAdaptiveImageSrc.ts

@ -66,27 +66,32 @@ export function useAdaptiveImageSrc(
// Check if generation is already in progress // Check if generation is already in progress
const existing = getPending(fullSrc); const existing = getPending(fullSrc);
if (existing) { if (existing) {
existing.then((thumb) => { existing
if (prevSrcRef.current === fullSrc) { .then((thumb) => {
setThumbnailSrc(thumb); if (prevSrcRef.current === fullSrc) {
} setThumbnailSrc(thumb);
}); }
})
.catch(() => {});
return; return;
} }
// Generate thumbnail // Generate thumbnail
const promise = generateThumbnail(fullSrc).then((thumb) => { const promise = generateThumbnail(fullSrc)
setThumbnail(fullSrc, thumb); .then((thumb) => {
removePending(fullSrc); setThumbnail(fullSrc, thumb);
return thumb; if (prevSrcRef.current === fullSrc) {
}); setThumbnailSrc(thumb);
}
return thumb;
})
.finally(() => {
removePending(fullSrc);
});
setPending(fullSrc, promise); setPending(fullSrc, promise);
promise.then((thumb) => { // Suppress unhandled rejection — fullSrc falls back gracefully
if (prevSrcRef.current === fullSrc) { promise.catch(() => {});
setThumbnailSrc(thumb);
}
});
}, [fullSrc]); }, [fullSrc]);
if (!fullSrc) return null; if (!fullSrc) return null;

Loading…
Cancel
Save