fix all promise related eslint issue for export

This commit is contained in:
Abhinav 2023-03-02 12:48:12 +05:30
parent b3ec5423a6
commit 5c900165e0
3 changed files with 179 additions and 126 deletions

View file

@ -70,14 +70,20 @@ export default function ExportModal(props: Props) {
if (!isElectron()) { if (!isElectron()) {
return; return;
} }
setExportFolder(getData(LS_KEYS.EXPORT)?.folder); try {
setExportFolder(getData(LS_KEYS.EXPORT)?.folder);
exportService.electronAPIs.registerStopExportListener(stopExport); exportService.electronAPIs.registerStopExportListener(stopExport);
exportService.electronAPIs.registerPauseExportListener(pauseExport); exportService.electronAPIs.registerPauseExportListener(pauseExport);
exportService.electronAPIs.registerResumeExportListener(resumeExport); exportService.electronAPIs.registerResumeExportListener(
exportService.electronAPIs.registerRetryFailedExportListener( resumeExport
retryFailedExport );
); exportService.electronAPIs.registerRetryFailedExportListener(
retryFailedExport
);
} catch (e) {
logError(e, 'error in exportModal');
}
}, []); }, []);
useEffect(() => { useEffect(() => {
@ -85,19 +91,25 @@ export default function ExportModal(props: Props) {
return; return;
} }
const main = async () => { const main = async () => {
const exportInfo = await exportService.getExportRecord(); try {
setExportStage(exportInfo?.stage ?? ExportStage.INIT); const exportInfo = await exportService.getExportRecord();
setLastExportTime(exportInfo?.lastAttemptTimestamp); setExportStage(exportInfo?.stage ?? ExportStage.INIT);
setExportProgress(exportInfo?.progress ?? { current: 0, total: 0 }); setLastExportTime(exportInfo?.lastAttemptTimestamp);
setExportStats({ setExportProgress(
success: exportInfo?.exportedFiles?.length ?? 0, exportInfo?.progress ?? { current: 0, total: 0 }
failed: exportInfo?.failedFiles?.length ?? 0, );
}); setExportStats({
if (exportInfo?.stage === ExportStage.INPROGRESS) { success: exportInfo?.exportedFiles?.length ?? 0,
resumeExport(); failed: exportInfo?.failedFiles?.length ?? 0,
});
if (exportInfo?.stage === ExportStage.INPROGRESS) {
resumeExport();
}
} catch (e) {
logError(e, 'error handling exportFolder change');
} }
}; };
main(); void main();
}, [exportFolder]); }, [exportFolder]);
useEffect(() => { useEffect(() => {
@ -117,7 +129,7 @@ export default function ExportModal(props: Props) {
const failedFilesCnt = exportRecord.failedFiles?.length; const failedFilesCnt = exportRecord.failedFiles?.length;
const syncedFilesCnt = userPersonalFiles.length; const syncedFilesCnt = userPersonalFiles.length;
if (syncedFilesCnt > exportedFileCnt + failedFilesCnt) { if (syncedFilesCnt > exportedFileCnt + failedFilesCnt) {
updateExportProgress({ await updateExportProgress({
current: exportedFileCnt + failedFilesCnt, current: exportedFileCnt + failedFilesCnt,
total: syncedFilesCnt, total: syncedFilesCnt,
}); });
@ -131,11 +143,11 @@ export default function ExportModal(props: Props) {
getExportRecordFileUID(file) getExportRecordFileUID(file)
) )
); );
exportService.addFilesQueuedRecord( await exportService.addFilesQueuedRecord(
exportFolder, exportFolder,
unExportedFiles unExportedFiles
); );
updateExportStage(ExportStage.PAUSED); await updateExportStage(ExportStage.PAUSED);
} }
} catch (e) { } catch (e) {
setExportStage(ExportStage.INIT); setExportStage(ExportStage.INIT);
@ -143,7 +155,7 @@ export default function ExportModal(props: Props) {
} }
} }
}; };
main(); void main();
}, [props.show]); }, [props.show]);
useEffect(() => { useEffect(() => {
@ -158,19 +170,21 @@ export default function ExportModal(props: Props) {
setData(LS_KEYS.EXPORT, { folder: newFolder }); setData(LS_KEYS.EXPORT, { folder: newFolder });
}; };
const updateExportStage = (newStage: ExportStage) => { const updateExportStage = async (newStage: ExportStage) => {
setExportStage(newStage); setExportStage(newStage);
exportService.updateExportRecord({ stage: newStage }); await exportService.updateExportRecord({ stage: newStage });
}; };
const updateExportTime = (newTime: number) => { const updateExportTime = async (newTime: number) => {
setLastExportTime(newTime); setLastExportTime(newTime);
exportService.updateExportRecord({ lastAttemptTimestamp: newTime }); await exportService.updateExportRecord({
lastAttemptTimestamp: newTime,
});
}; };
const updateExportProgress = (newProgress: ExportProgress) => { const updateExportProgress = async (newProgress: ExportProgress) => {
setExportProgress(newProgress); setExportProgress(newProgress);
exportService.updateExportRecord({ progress: newProgress }); await exportService.updateExportRecord({ progress: newProgress });
}; };
// ====================== // ======================
@ -182,15 +196,15 @@ export default function ExportModal(props: Props) {
if (!exportFolder) { if (!exportFolder) {
await selectExportDirectory(); await selectExportDirectory();
} }
updateExportStage(ExportStage.INPROGRESS); await updateExportStage(ExportStage.INPROGRESS);
await sleep(100); await sleep(100);
}; };
const postExportRun = async (exportResult?: { paused?: boolean }) => { const postExportRun = async (exportResult?: { paused?: boolean }) => {
if (!exportResult?.paused) { if (!exportResult?.paused) {
updateExportStage(ExportStage.FINISHED); await updateExportStage(ExportStage.FINISHED);
await sleep(100); await sleep(100);
updateExportTime(Date.now()); await updateExportTime(Date.now());
syncExportStatsWithRecord(); await syncExportStatsWithRecord();
} }
}; };
@ -214,86 +228,106 @@ export default function ExportModal(props: Props) {
// UI functions // UI functions
// ============= // =============
const startExport = async () => { const startExport = () => {
try { const main = async () => {
await preExportRun(); try {
updateExportProgress({ current: 0, total: 0 }); await preExportRun();
const exportResult = await exportService.exportFiles( await updateExportProgress({ current: 0, total: 0 });
updateExportProgress, const exportResult = await exportService.exportFiles(
ExportType.NEW updateExportProgress,
); ExportType.NEW
await postExportRun(exportResult); );
} catch (e) { await postExportRun(exportResult);
if (e.message !== CustomError.REQUEST_CANCELLED) { } catch (e) {
logError(e, 'startExport failed'); if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'startExport failed');
}
} }
} };
void main();
}; };
const stopExport = async () => { const stopExport = () => {
try { const main = async () => {
exportService.stopRunningExport(); try {
postExportRun(); exportService.stopRunningExport();
} catch (e) { await postExportRun();
if (e.message !== CustomError.REQUEST_CANCELLED) { } catch (e) {
logError(e, 'stopExport failed'); if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'stopExport failed');
}
} }
} };
void main();
}; };
const pauseExport = () => { const pauseExport = () => {
try { const main = async () => {
updateExportStage(ExportStage.PAUSED); try {
exportService.pauseRunningExport(); await updateExportStage(ExportStage.PAUSED);
postExportRun({ paused: true }); exportService.pauseRunningExport();
} catch (e) { await postExportRun({ paused: true });
if (e.message !== CustomError.REQUEST_CANCELLED) { } catch (e) {
logError(e, 'pauseExport failed'); if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'pauseExport failed');
}
} }
} };
void main();
}; };
const resumeExport = async () => { const resumeExport = () => {
try { const main = async () => {
const exportRecord = await exportService.getExportRecord(); try {
await preExportRun(); const exportRecord = await exportService.getExportRecord();
await preExportRun();
const pausedStageProgress = exportRecord.progress; const pausedStageProgress = exportRecord.progress;
setExportProgress(pausedStageProgress); setExportProgress(pausedStageProgress);
const updateExportStatsWithOffset = (progress: ExportProgress) => const updateExportStatsWithOffset = (
updateExportProgress({ progress: ExportProgress
current: pausedStageProgress.current + progress.current, ) =>
total: pausedStageProgress.current + progress.total, updateExportProgress({
current: pausedStageProgress.current + progress.current,
total: pausedStageProgress.current + progress.total,
});
const exportResult = await exportService.exportFiles(
updateExportStatsWithOffset,
ExportType.PENDING
);
await postExportRun(exportResult);
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'resumeExport failed');
}
}
};
void main();
};
const retryFailedExport = () => {
const main = async () => {
try {
await preExportRun();
await updateExportProgress({
current: 0,
total: exportStats.failed,
}); });
const exportResult = await exportService.exportFiles(
updateExportStatsWithOffset,
ExportType.PENDING
);
await postExportRun(exportResult); const exportResult = await exportService.exportFiles(
} catch (e) { updateExportProgress,
if (e.message !== CustomError.REQUEST_CANCELLED) { ExportType.RETRY_FAILED
logError(e, 'resumeExport failed'); );
await postExportRun(exportResult);
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'retryFailedExport failed');
}
} }
} };
}; void main();
const retryFailedExport = async () => {
try {
await preExportRun();
updateExportProgress({ current: 0, total: exportStats.failed });
const exportResult = await exportService.exportFiles(
updateExportProgress,
ExportType.RETRY_FAILED
);
await postExportRun(exportResult);
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'retryFailedExport failed');
}
}
}; };
const ExportDynamicContent = () => { const ExportDynamicContent = () => {

View file

@ -49,6 +49,7 @@ import { User } from 'types/user';
import { FILE_TYPE, TYPE_JPEG, TYPE_JPG } from 'constants/file'; import { FILE_TYPE, TYPE_JPEG, TYPE_JPG } from 'constants/file';
import { ExportType, ExportNotification, RecordType } from 'constants/export'; import { ExportType, ExportNotification, RecordType } from 'constants/export';
import { ElectronAPIs } from 'types/electron'; import { ElectronAPIs } from 'types/electron';
import { CustomError } from 'utils/error';
const LATEST_EXPORT_VERSION = 1; const LATEST_EXPORT_VERSION = 1;
const EXPORT_RECORD_FILE_NAME = 'export_status.json'; const EXPORT_RECORD_FILE_NAME = 'export_status.json';
@ -83,10 +84,11 @@ class ExportService {
this.pauseExport = true; this.pauseExport = true;
} }
async exportFiles( async exportFiles(
updateProgress: (progress: ExportProgress) => void, updateProgress: (progress: ExportProgress) => Promise<void>,
exportType: ExportType exportType: ExportType
) { ) {
try { try {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
if (this.exportInProgress) { if (this.exportInProgress) {
this.electronAPIs.sendNotification( this.electronAPIs.sendNotification(
ExportNotification.IN_PROGRESS ExportNotification.IN_PROGRESS
@ -177,7 +179,7 @@ class ExportService {
newCollections: Collection[], newCollections: Collection[],
renamedCollections: Collection[], renamedCollections: Collection[],
collectionIDPathMap: CollectionIDPathMap, collectionIDPathMap: CollectionIDPathMap,
updateProgress: (progress: ExportProgress) => void, updateProgress: (progress: ExportProgress) => Promise<void>,
exportDir: string exportDir: string
): Promise<{ paused: boolean }> { ): Promise<{ paused: boolean }> {
try { try {
@ -212,7 +214,7 @@ class ExportService {
this.electronAPIs.showOnTray({ this.electronAPIs.showOnTray({
export_progress: `0 / ${files.length} files exported`, export_progress: `0 / ${files.length} files exported`,
}); });
updateProgress({ await updateProgress({
current: 0, current: 0,
total: files.length, total: files.length,
}); });
@ -239,6 +241,12 @@ class ExportService {
RecordType.SUCCESS RecordType.SUCCESS
); );
} catch (e) { } catch (e) {
if (
e.message ===
CustomError.ADD_FILE_EXPORTED_RECORD_FAILED
) {
throw e;
}
await this.addFileExportedRecord( await this.addFileExportedRecord(
exportDir, exportDir,
file, file,
@ -255,7 +263,10 @@ class ExportService {
files.length files.length
} files exported`, } files exported`,
}); });
updateProgress({ current: index + 1, total: files.length }); await updateProgress({
current: index + 1,
total: files.length,
});
} }
if (this.stopExport) { if (this.stopExport) {
this.electronAPIs.sendNotification(ExportNotification.ABORT); this.electronAPIs.sendNotification(ExportNotification.ABORT);
@ -266,7 +277,7 @@ class ExportService {
} else if (failedFileCount > 0) { } else if (failedFileCount > 0) {
this.electronAPIs.sendNotification(ExportNotification.FAILED); this.electronAPIs.sendNotification(ExportNotification.FAILED);
this.electronAPIs.showOnTray({ this.electronAPIs.showOnTray({
retry_export: `export failed - retry export`, retry_export: `Retry failed export`,
}); });
} else { } else {
this.electronAPIs.sendNotification(ExportNotification.FINISH); this.electronAPIs.sendNotification(ExportNotification.FINISH);
@ -289,32 +300,37 @@ class ExportService {
file: EnteFile, file: EnteFile,
type: RecordType type: RecordType
) { ) {
const fileUID = getExportRecordFileUID(file); try {
const exportRecord = await this.getExportRecord(folder); const fileUID = getExportRecordFileUID(file);
exportRecord.queuedFiles = exportRecord.queuedFiles.filter( const exportRecord = await this.getExportRecord(folder);
(queuedFilesUID) => queuedFilesUID !== fileUID exportRecord.queuedFiles = exportRecord.queuedFiles.filter(
); (queuedFilesUID) => queuedFilesUID !== fileUID
if (type === RecordType.SUCCESS) { );
if (!exportRecord.exportedFiles) { if (type === RecordType.SUCCESS) {
exportRecord.exportedFiles = []; if (!exportRecord.exportedFiles) {
} exportRecord.exportedFiles = [];
exportRecord.exportedFiles.push(fileUID); }
exportRecord.failedFiles && exportRecord.exportedFiles.push(fileUID);
(exportRecord.failedFiles = exportRecord.failedFiles.filter( exportRecord.failedFiles &&
(FailedFileUID) => FailedFileUID !== fileUID (exportRecord.failedFiles = exportRecord.failedFiles.filter(
)); (FailedFileUID) => FailedFileUID !== fileUID
} else { ));
if (!exportRecord.failedFiles) { } else {
exportRecord.failedFiles = []; if (!exportRecord.failedFiles) {
} exportRecord.failedFiles = [];
if (!exportRecord.failedFiles.find((x) => x === fileUID)) { }
exportRecord.failedFiles.push(fileUID); if (!exportRecord.failedFiles.find((x) => x === fileUID)) {
exportRecord.failedFiles.push(fileUID);
}
} }
exportRecord.exportedFiles = dedupe(exportRecord.exportedFiles);
exportRecord.queuedFiles = dedupe(exportRecord.queuedFiles);
exportRecord.failedFiles = dedupe(exportRecord.failedFiles);
await this.updateExportRecord(exportRecord, folder);
} catch (e) {
logError(e, 'addFileExportedRecord failed');
throw Error(CustomError.ADD_FILE_EXPORTED_RECORD_FAILED);
} }
exportRecord.exportedFiles = dedupe(exportRecord.exportedFiles);
exportRecord.queuedFiles = dedupe(exportRecord.queuedFiles);
exportRecord.failedFiles = dedupe(exportRecord.failedFiles);
await this.updateExportRecord(exportRecord, folder);
} }
async addCollectionExportedRecord( async addCollectionExportedRecord(
@ -354,6 +370,7 @@ class ExportService {
); );
} catch (e) { } catch (e) {
logError(e, 'error updating Export Record'); logError(e, 'error updating Export Record');
throw e;
} }
} }
@ -372,6 +389,7 @@ class ExportService {
} }
} catch (e) { } catch (e) {
logError(e, 'export Record JSON parsing failed '); logError(e, 'export Record JSON parsing failed ');
throw e;
} }
} }

View file

@ -55,6 +55,7 @@ export const CustomError = {
'Windows native image processing is not supported', 'Windows native image processing is not supported',
NETWORK_ERROR: 'Network Error', NETWORK_ERROR: 'Network Error',
NOT_FILE_OWNER: 'not file owner', NOT_FILE_OWNER: 'not file owner',
ADD_FILE_EXPORTED_RECORD_FAILED: 'add file exported record failed',
}; };
export function parseUploadErrorCodes(error) { export function parseUploadErrorCodes(error) {