diff --git a/web/apps/cast/src/services/wasm/ffmpeg.ts b/web/apps/cast/src/services/wasm/ffmpeg.ts index 855aad0f6..adbfcb5b1 100644 --- a/web/apps/cast/src/services/wasm/ffmpeg.ts +++ b/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"; diff --git a/web/apps/photos/src/services/download/clients/photos.ts b/web/apps/photos/src/services/download/clients/photos.ts index 7796903fd..a605aa64d 100644 --- a/web/apps/photos/src/services/download/clients/photos.ts +++ b/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"; diff --git a/web/apps/photos/src/services/download/clients/publicAlbums.ts b/web/apps/photos/src/services/download/clients/publicAlbums.ts index 7245fd68d..be4fe34c4 100644 --- a/web/apps/photos/src/services/download/clients/publicAlbums.ts +++ b/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"; diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts index 1f0b8a54d..05c4cb021 100644 --- a/web/apps/photos/src/services/export/index.ts +++ b/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, diff --git a/web/apps/photos/src/services/export/migration.ts b/web/apps/photos/src/services/export/migration.ts index 82a86d9eb..27980fe5a 100644 --- a/web/apps/photos/src/services/export/migration.ts +++ b/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, diff --git a/web/apps/photos/src/services/heic-convert/service.ts b/web/apps/photos/src/services/heic-convert/service.ts index 321b8ba45..63f70fa10 100644 --- a/web/apps/photos/src/services/heic-convert/service.ts +++ b/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"; diff --git a/web/apps/photos/src/services/upload/thumbnailService.ts b/web/apps/photos/src/services/upload/thumbnailService.ts index 74ce23f4c..64df2f016 100644 --- a/web/apps/photos/src/services/upload/thumbnailService.ts +++ b/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, ); }); diff --git a/web/apps/photos/src/services/upload/uploader.ts b/web/apps/photos/src/services/upload/uploader.ts index 2e073e242..a4e60cb93 100644 --- a/web/apps/photos/src/services/upload/uploader.ts +++ b/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"; diff --git a/web/apps/photos/src/services/wasm/ffmpeg.ts b/web/apps/photos/src/services/wasm/ffmpeg.ts index 32762550f..df70d861b 100644 --- a/web/apps/photos/src/services/wasm/ffmpeg.ts +++ b/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"; diff --git a/web/apps/photos/src/utils/common/index.ts b/web/apps/photos/src/utils/common/index.ts index efc9a3553..2c7fd0545 100644 --- a/web/apps/photos/src/utils/common/index.ts +++ b/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 ( - request: Promise, - timeout: number, -): Promise => { - const timeoutRef = { current: null }; - const rejectOnTimeout = new Promise((_, 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"; } diff --git a/web/apps/photos/src/utils/common/promiseTimeout.ts b/web/apps/photos/src/utils/common/promiseTimeout.ts deleted file mode 100644 index bf5a79e2a..000000000 --- a/web/apps/photos/src/utils/common/promiseTimeout.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { CustomError } from "@ente/shared/error"; - -export const promiseWithTimeout = async ( - request: Promise, - 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, - ]); -}; diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts index e02537482..fd6a08da3 100644 --- a/web/apps/photos/src/utils/file/index.ts +++ b/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, ); }); diff --git a/web/apps/photos/src/utils/upload/uploadRetrier.ts b/web/apps/photos/src/utils/upload/uploadRetrier.ts index 2f42ed58f..3d314fd14 100644 --- a/web/apps/photos/src/utils/upload/uploadRetrier.ts +++ b/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]; diff --git a/web/packages/shared/error/index.ts b/web/packages/shared/error/index.ts index be8f10cea..3d3309379 100644 --- a/web/packages/shared/error/index.ts +++ b/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", diff --git a/web/packages/shared/promise/index.ts b/web/packages/shared/promise/index.ts deleted file mode 100644 index 39ce09362..000000000 --- a/web/packages/shared/promise/index.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { sleep } from "@ente/shared/sleep"; -import { CustomError } from "../error"; - -const waitTimeBeforeNextAttemptInMilliSeconds = [2000, 5000, 10000]; - -export async function retryAsyncFunction( - request: (abort?: () => void) => Promise, - waitTimeBeforeNextTry?: number[], -): Promise { - 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 ( - request: Promise, - timeout: number, -): Promise => { - const timeoutRef = { current: null }; - const rejectOnTimeout = new Promise((_, 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, - ]); -}; diff --git a/web/packages/shared/sleep/index.ts b/web/packages/shared/sleep/index.ts deleted file mode 100644 index 39e64cb5f..000000000 --- a/web/packages/shared/sleep/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export async function sleep(time: number) { - await new Promise((resolve) => { - setTimeout(() => resolve(null), time); - }); -} diff --git a/web/packages/shared/utils/index.ts b/web/packages/shared/utils/index.ts index 1e14b35ad..6a9df7e1f 100644 --- a/web/packages/shared/utils/index.ts +++ b/web/packages/shared/utils/index.ts @@ -26,3 +26,47 @@ export function downloadUsingAnchor(link: string, name: string) { export function isPromise(obj: T | Promise): obj is Promise { return obj && typeof (obj as any).then === "function"; } + +export async function retryAsyncFunction( + request: (abort?: () => void) => Promise, +): Promise { + 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 ( + request: Promise, + timeout: number, +): Promise => { + const timeoutRef = { current: null }; + const rejectOnTimeout = new Promise((_, 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, + ]); +};