Ignore stale downloads

If src or sizePx changes while we're downloading,
discard the now-stale fetch result so we don't
override the fresh one.
This commit is contained in:
JephDiel
2026-03-12 22:20:19 -05:00
parent 8ecb1b3dbf
commit b198721969

View File

@@ -110,16 +110,24 @@ export const Avatar: FC<Props> = ({
} }
let objectUrl: string | undefined; let objectUrl: string | undefined;
let stale = false;
blob blob
.then((blob) => { .then((blob) => {
if (stale) {
return;
}
objectUrl = URL.createObjectURL(blob); objectUrl = URL.createObjectURL(blob);
setAvatarUrl(objectUrl); setAvatarUrl(objectUrl);
}) })
.catch((ex) => { .catch((ex) => {
if (stale) {
return;
}
setAvatarUrl(undefined); setAvatarUrl(undefined);
}); });
return (): void => { return (): void => {
stale = true;
if (objectUrl) { if (objectUrl) {
URL.revokeObjectURL(objectUrl); URL.revokeObjectURL(objectUrl);
} }