Don't use exceptions for control flow

This commit is contained in:
Manav Rathi
2024-11-14 09:48:20 +05:30
parent f749f9de65
commit 86f859aaca
2 changed files with 25 additions and 25 deletions

View File

@@ -192,6 +192,8 @@ const PhotoFrame = ({
return <div />;
}
// Return a function which will return true if the URL was updated (for the
// given params), and false otherwise.
const updateURL =
(index: number) => (id: number, url: string, forceUpdate?: boolean) => {
const file = displayFiles[index];
@@ -203,9 +205,10 @@ const PhotoFrame = ({
throw Error("update url file id mismatch");
}
if (file.msrc && !forceUpdate) {
throw Error(CustomError.URL_ALREADY_SET);
return false;
}
updateFileMsrcProps(file, url);
return true;
};
const updateSrcURL = async (
@@ -344,21 +347,20 @@ const PhotoFrame = ({
thumbFetching[item.id] = true;
const url = await DownloadManager.getThumbnailForPreview(item);
try {
updateURL(index)(item.id, url);
log.info(
`[${item.id}] calling invalidateCurrItems for thumbnail msrc: ${!!item.msrc}`,
);
instance.invalidateCurrItems();
if ((instance as any).isOpen()) {
instance.updateSize(true);
if (updateURL(index)(item.id, url)) {
log.info(
`[${item.id}] calling invalidateCurrItems for thumbnail msrc: ${!!item.msrc}`,
);
instance.invalidateCurrItems();
if ((instance as any).isOpen()) {
instance.updateSize(true);
}
}
} catch (e) {
if (e.message !== CustomError.URL_ALREADY_SET) {
log.error(
"updating photoswipe after msrc url update failed",
e,
);
}
log.error(
"updating photoswipe after msrc url update failed",
e,
);
// ignore
}
} catch (e) {
@@ -491,13 +493,14 @@ const PhotoFrame = ({
return;
}
try {
updateURL(index)(item.id, item.msrc, true);
log.info(
`[${item.id}] calling invalidateCurrItems for thumbnail msrc: ${!!item.msrc}`,
);
instance.invalidateCurrItems();
if ((instance as any).isOpen()) {
instance.updateSize(true);
if (updateURL(index)(item.id, item.msrc, true)) {
log.info(
`[${item.id}] calling invalidateCurrItems for thumbnail msrc: ${!!item.msrc}`,
);
instance.invalidateCurrItems();
if ((instance as any).isOpen()) {
instance.updateSize(true);
}
}
} catch (e) {
if (e.message !== CustomError.URL_ALREADY_SET) {

View File

@@ -12,7 +12,6 @@ import {
} from "@/new/photos/components/PlaceholderThumbnails";
import { TRASH_SECTION } from "@/new/photos/services/collection";
import DownloadManager from "@/new/photos/services/download";
import { CustomError } from "@ente/shared/error";
import useLongPress from "@ente/shared/hooks/useLongPress";
import AlbumOutlined from "@mui/icons-material/AlbumOutlined";
import Favorite from "@mui/icons-material/FavoriteRounded";
@@ -286,9 +285,7 @@ export default function PreviewCard(props: IProps) {
setImgSrc(url);
updateURL(file.id, url);
} catch (e) {
if (e.message !== CustomError.URL_ALREADY_SET) {
log.error("preview card useEffect failed", e);
}
log.error("preview card useEffect failed", e);
// no-op
}
};