From b605e41f9e51de2dda4f9f0fcb39db137a8d7873 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 21 Sep 2024 08:50:49 +0530 Subject: [PATCH] Wrap --- .../Collections/CollectionOptions/index.tsx | 96 ++++++++++--------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/web/apps/photos/src/components/Collections/CollectionOptions/index.tsx b/web/apps/photos/src/components/Collections/CollectionOptions/index.tsx index d9b3fd90ed..bed1d3c5c4 100644 --- a/web/apps/photos/src/components/Collections/CollectionOptions/index.tsx +++ b/web/apps/photos/src/components/Collections/CollectionOptions/index.tsx @@ -107,68 +107,70 @@ const CollectionOptions = (props: CollectionOptionsProps) => { }; /** - * Wrap a async function in an error handler, returning a new function that - * shows an generic error dialog if the original function throws. + * Return a new function that shows an generic error dialog if the original + * function throws. */ const wrapError = async (f: () => Promise) => { - try { - await f(); - } catch (e) { - log.error("Collection action failed", e); - setDialogMessage({ - title: t("ERROR"), - content: t("UNKNOWN_ERROR"), - close: { variant: "critical" }, - }); - } + return () => { + try { + await f(); + } catch (e) { + log.error("Collection action failed", e); + setDialogMessage({ + title: t("ERROR"), + content: t("UNKNOWN_ERROR"), + close: { variant: "critical" }, + }); + } + }; }; /** - * Wrap a async function in an error handler, and sync on completion. - * - * This function returns a new function that shows an generic error dialog - * if the original function throws. + * Return a new function by wrapping an async function in an error handler, + * and syncing on completion. */ - const wrapErrorAndSyncWithRemote = async (f: () => Promise) => { - try { - await f(); - } catch (e) { - log.error("Collection action failed", e); - setDialogMessage({ - title: t("ERROR"), - content: t("UNKNOWN_ERROR"), - close: { variant: "critical" }, - }); - } finally { - syncWithRemote(false, true); - } + const wrapErrorAndSyncWithRemote = async ( + f: (...args: any) => Promise, + ) => { + return (...args: any) => { + try { + await f(); + } catch (e) { + log.error("Collection action failed", e); + setDialogMessage({ + title: t("ERROR"), + content: t("UNKNOWN_ERROR"), + close: { variant: "critical" }, + }); + } finally { + syncWithRemote(false, true); + } + }; }; /** * Variant of {@link wrapErrorAndSyncWithRemote} that also shows the global * loading bar. - * - * This function returns a new function that shows an generic error dialog - * if the original function throws. In addition, it also shows a global - * loading indicator while everything happens. */ const wrapErrorAndSyncWithRemoteLoading = async ( f: () => Promise, ) => { - startLoading(); - try { - await f(); - } catch (e) { - log.error("Collection action failed", e); - setDialogMessage({ - title: t("ERROR"), - content: t("UNKNOWN_ERROR"), - close: { variant: "critical" }, - }); - } finally { - syncWithRemote(false, true); - finishLoading(); - } + return () => { + startLoading(); + try { + await f(); + } catch (e) { + log.error("Collection action failed", e); + setDialogMessage({ + title: t("ERROR"), + content: t("UNKNOWN_ERROR"), + close: { variant: "critical" }, + }); + } finally { + syncWithRemote(false, true); + finishLoading(); + } + }; }; const handleCollectionAction = (