Consolidate and deduplicate

This commit is contained in:
Manav Rathi 2024-04-05 20:33:44 +05:30
parent 7807d3a413
commit 42a59f2fb5
No known key found for this signature in database
17 changed files with 57 additions and 127 deletions

View file

@ -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";

View file

@ -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";

View file

@ -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";

View file

@ -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,

View file

@ -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,

View file

@ -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";

View file

@ -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,
);
});

View file

@ -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";

View file

@ -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";

View file

@ -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";
}

View file

@ -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,
]);
};

View file

@ -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,
);
});

View file

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

View file

@ -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",

View file

@ -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,
]);
};

View file

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

View file

@ -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,
]);
};