Bläddra i källkod

Consolidate and deduplicate

Manav Rathi 1 år sedan
förälder
incheckning
42a59f2fb5

+ 1 - 1
web/apps/cast/src/services/wasm/ffmpeg.ts

@@ -1,5 +1,5 @@
 import { addLogLine } from "@ente/shared/logging";
-import { promiseWithTimeout } from "@ente/shared/promise";
+import { promiseWithTimeout } from "@ente/shared/utils";
 import { logError } from "@ente/shared/sentry";
 import QueueProcessor from "@ente/shared/utils/queueProcessor";
 import { generateTempName } from "@ente/shared/utils/temp";

+ 1 - 1
web/apps/photos/src/services/download/clients/photos.ts

@@ -1,7 +1,7 @@
 import { CustomError } from "@ente/shared/error";
 import HTTPService from "@ente/shared/network/HTTPService";
 import { getFileURL, getThumbnailURL } from "@ente/shared/network/api";
-import { retryAsyncFunction } from "@ente/shared/promise";
+import { retryAsyncFunction } from "@ente/shared/utils";
 import { DownloadClient } from "services/download";
 import { EnteFile } from "types/file";
 

+ 1 - 1
web/apps/photos/src/services/download/clients/publicAlbums.ts

@@ -4,7 +4,7 @@ import {
     getPublicCollectionFileURL,
     getPublicCollectionThumbnailURL,
 } from "@ente/shared/network/api";
-import { retryAsyncFunction } from "@ente/shared/promise";
+import { retryAsyncFunction } from "@ente/shared/utils";
 import { DownloadClient } from "services/download";
 import { EnteFile } from "types/file";
 

+ 1 - 1
web/apps/photos/src/services/export/index.ts

@@ -1,7 +1,7 @@
 import { logError } from "@ente/shared/sentry";
 import { LS_KEYS, getData, setData } from "@ente/shared/storage/localStorage";
 import { EnteFile } from "types/file";
-import { sleep } from "utils/common";
+import { sleep } from "@ente/shared/utils";
 import {
     convertCollectionIDExportNameObjectToMap,
     convertFileIDExportNameObjectToMap,

+ 1 - 1
web/apps/photos/src/services/export/migration.ts

@@ -19,7 +19,7 @@ import {
 } from "types/export";
 import { EnteFile } from "types/file";
 import { getNonEmptyPersonalCollections } from "utils/collection";
-import { sleep } from "utils/common";
+import { sleep } from "@ente/shared/utils";
 import {
     getCollectionExportPath,
     getCollectionIDFromFileUID,

+ 1 - 1
web/apps/photos/src/services/heic-convert/service.ts

@@ -1,6 +1,6 @@
 import { CustomError } from "@ente/shared/error";
 import { addLogLine } from "@ente/shared/logging";
-import { retryAsyncFunction } from "@ente/shared/promise";
+import { retryAsyncFunction } from "@ente/shared/utils";
 import { logError } from "@ente/shared/sentry";
 import QueueProcessor from "@ente/shared/utils/queueProcessor";
 import { convertBytesToHumanReadable } from "@ente/shared/utils/size";

+ 2 - 2
web/apps/photos/src/services/upload/thumbnailService.ts

@@ -152,7 +152,7 @@ export async function generateImageThumbnailUsingCanvas(
             }
         };
         timeout = setTimeout(
-            () => reject(Error(CustomError.WAIT_TIME_EXCEEDED)),
+            () => reject(new Error("Operation timed out")),
             WAIT_TIME_THUMBNAIL_GENERATION,
         );
     });
@@ -238,7 +238,7 @@ export async function generateVideoThumbnailUsingCanvas(
             }
         });
         timeout = setTimeout(
-            () => reject(Error(CustomError.WAIT_TIME_EXCEEDED)),
+            () => reject(new Error("Operation timed out")),
             WAIT_TIME_THUMBNAIL_GENERATION,
         );
     });

+ 1 - 1
web/apps/photos/src/services/upload/uploader.ts

@@ -15,7 +15,7 @@ import {
     Logger,
     UploadFile,
 } from "types/upload";
-import { sleep } from "utils/common";
+import { sleep } from "@ente/shared/utils";
 import { findMatchingExistingFiles } from "utils/upload";
 import UIService from "./uiService";
 import uploadCancelService from "./uploadCancelService";

+ 1 - 1
web/apps/photos/src/services/wasm/ffmpeg.ts

@@ -4,7 +4,7 @@ import QueueProcessor from "@ente/shared/utils/queueProcessor";
 import { generateTempName } from "@ente/shared/utils/temp";
 import { createFFmpeg, FFmpeg } from "ffmpeg-wasm";
 import { getUint8ArrayView } from "services/readerService";
-import { promiseWithTimeout } from "utils/common";
+import { promiseWithTimeout } from "@ente/shared/utils";
 
 const INPUT_PATH_PLACEHOLDER = "INPUT";
 const FFMPEG_PLACEHOLDER = "FFMPEG";

+ 1 - 35
web/apps/photos/src/utils/common/index.ts

@@ -1,6 +1,7 @@
 import { APP_DOWNLOAD_URL } from "@ente/shared/constants/urls";
 import { CustomError } from "@ente/shared/error";
 import isElectron from "is-electron";
+import { isPromise } from "@ente/shared/utils";
 
 export function checkConnectivity() {
     if (navigator.onLine) {
@@ -49,12 +50,6 @@ export function webglSupported() {
     }
 }
 
-export async function sleep(time: number) {
-    await new Promise((resolve) => {
-        setTimeout(() => resolve(null), time);
-    });
-}
-
 export function downloadApp() {
     openLink(APP_DOWNLOAD_URL, true);
 }
@@ -71,27 +66,6 @@ export function initiateEmail(email: string) {
     a.rel = "noreferrer noopener";
     a.click();
 }
-export const promiseWithTimeout = async <T>(
-    request: Promise<T>,
-    timeout: number,
-): Promise<T> => {
-    const timeoutRef = { current: null };
-    const rejectOnTimeout = new Promise<null>((_, reject) => {
-        timeoutRef.current = setTimeout(
-            () => reject(Error(CustomError.WAIT_TIME_EXCEEDED)),
-            timeout,
-        );
-    });
-    const requestWithTimeOutCancellation = async () => {
-        const resp = await request;
-        clearTimeout(timeoutRef.current);
-        return resp;
-    };
-    return await Promise.race([
-        requestWithTimeOutCancellation(),
-        rejectOnTimeout,
-    ]);
-};
 
 export const preloadImage = (imgBasePath: string) => {
     const srcSet = [];
@@ -120,14 +94,6 @@ export async function waitAndRun(
     await task();
 }
 
-function isPromise(p: any) {
-    if (typeof p === "object" && typeof p.then === "function") {
-        return true;
-    }
-
-    return false;
-}
-
 export function isClipboardItemPresent() {
     return typeof ClipboardItem !== "undefined";
 }

+ 0 - 22
web/apps/photos/src/utils/common/promiseTimeout.ts

@@ -1,22 +0,0 @@
-import { CustomError } from "@ente/shared/error";
-
-export const promiseWithTimeout = async (
-    request: Promise<any>,
-    timeout: number,
-) => {
-    const timeoutRef = { current: null };
-    const rejectOnTimeout = new Promise((_, reject) => {
-        timeoutRef.current = setTimeout(
-            () => reject(Error(CustomError.WAIT_TIME_EXCEEDED)),
-            timeout,
-        );
-    });
-    return await Promise.race([
-        (async () => {
-            const resp = await request;
-            clearTimeout(timeoutRef.current);
-            return resp;
-        })(),
-        rejectOnTimeout,
-    ]);
-};

+ 1 - 2
web/apps/photos/src/utils/file/index.ts

@@ -52,7 +52,6 @@ import {
 import { FileTypeInfo } from "types/upload";
 
 import { default as ElectronAPIs } from "@ente/shared/electron";
-import { downloadUsingAnchor } from "@ente/shared/utils";
 import { t } from "i18next";
 import imageProcessor from "services/imageProcessor";
 import { getFileExportPath, getUniqueFileExportName } from "utils/export";
@@ -866,7 +865,7 @@ export const copyFileToClipboard = async (fileUrl: string) => {
             clearTimeout(timeout);
         }
         timeout = setTimeout(
-            () => reject(Error(CustomError.WAIT_TIME_EXCEEDED)),
+            () => reject(new Error("Operation timed out")),
             WAIT_TIME_IMAGE_CONVERSION,
         );
     });

+ 1 - 1
web/apps/photos/src/utils/upload/uploadRetrier.ts

@@ -1,4 +1,4 @@
-import { sleep } from "utils/common";
+import { sleep } from "@ente/shared/utils";
 
 const retrySleepTimeInMilliSeconds = [2000, 5000, 10000];
 

+ 0 - 1
web/packages/shared/error/index.ts

@@ -40,7 +40,6 @@ export const CustomError = {
     INVALID_COLLECTION_OPERATION: "invalid collection operation",
     TO_MOVE_FILES_FROM_MULTIPLE_COLLECTIONS:
         "to move files from multiple collections",
-    WAIT_TIME_EXCEEDED: "operation wait time exceeded",
     REQUEST_CANCELLED: "request canceled",
     REQUEST_FAILED: "request failed",
     TOKEN_EXPIRED: "token expired",

+ 0 - 51
web/packages/shared/promise/index.ts

@@ -1,51 +0,0 @@
-import { sleep } from "@ente/shared/sleep";
-import { CustomError } from "../error";
-
-const waitTimeBeforeNextAttemptInMilliSeconds = [2000, 5000, 10000];
-
-export async function retryAsyncFunction<T>(
-    request: (abort?: () => void) => Promise<T>,
-    waitTimeBeforeNextTry?: number[],
-): Promise<T> {
-    if (!waitTimeBeforeNextTry) {
-        waitTimeBeforeNextTry = waitTimeBeforeNextAttemptInMilliSeconds;
-    }
-
-    for (
-        let attemptNumber = 0;
-        attemptNumber <= waitTimeBeforeNextTry.length;
-        attemptNumber++
-    ) {
-        try {
-            const resp = await request();
-            return resp;
-        } catch (e) {
-            if (attemptNumber === waitTimeBeforeNextTry.length) {
-                throw e;
-            }
-            await sleep(waitTimeBeforeNextTry[attemptNumber]);
-        }
-    }
-}
-
-export const promiseWithTimeout = async <T>(
-    request: Promise<T>,
-    timeout: number,
-): Promise<T> => {
-    const timeoutRef = { current: null };
-    const rejectOnTimeout = new Promise<null>((_, reject) => {
-        timeoutRef.current = setTimeout(
-            () => reject(Error(CustomError.WAIT_TIME_EXCEEDED)),
-            timeout,
-        );
-    });
-    const requestWithTimeOutCancellation = async () => {
-        const resp = await request;
-        clearTimeout(timeoutRef.current);
-        return resp;
-    };
-    return await Promise.race([
-        requestWithTimeOutCancellation(),
-        rejectOnTimeout,
-    ]);
-};

+ 0 - 5
web/packages/shared/sleep/index.ts

@@ -1,5 +0,0 @@
-export async function sleep(time: number) {
-    await new Promise((resolve) => {
-        setTimeout(() => resolve(null), time);
-    });
-}

+ 44 - 0
web/packages/shared/utils/index.ts

@@ -26,3 +26,47 @@ export function downloadUsingAnchor(link: string, name: string) {
 export function isPromise<T>(obj: T | Promise<T>): obj is Promise<T> {
     return obj && typeof (obj as any).then === "function";
 }
+
+export async function retryAsyncFunction<T>(
+    request: (abort?: () => void) => Promise<T>,
+): Promise<T> {
+    const waitTimeBeforeNextTry = [2000, 5000, 10000];
+
+    for (
+        let attemptNumber = 0;
+        attemptNumber <= waitTimeBeforeNextTry.length;
+        attemptNumber++
+    ) {
+        try {
+            const resp = await request();
+            return resp;
+        } catch (e) {
+            if (attemptNumber === waitTimeBeforeNextTry.length) {
+                throw e;
+            }
+            await sleep(waitTimeBeforeNextTry[attemptNumber]);
+        }
+    }
+}
+
+export const promiseWithTimeout = async <T>(
+    request: Promise<T>,
+    timeout: number,
+): Promise<T> => {
+    const timeoutRef = { current: null };
+    const rejectOnTimeout = new Promise<null>((_, reject) => {
+        timeoutRef.current = setTimeout(
+            () => reject(new Error("Operation timed out")),
+            timeout,
+        );
+    });
+    const requestWithTimeOutCancellation = async () => {
+        const resp = await request;
+        clearTimeout(timeoutRef.current);
+        return resp;
+    };
+    return await Promise.race([
+        requestWithTimeOutCancellation(),
+        rejectOnTimeout,
+    ]);
+};