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:
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user