Skip to content

Commit

Permalink
Add retrying functionality to profile importing via code and reflect the
Browse files Browse the repository at this point in the history
importing being in progress in the UI
  • Loading branch information
VilppeRiskidev committed Oct 24, 2024
1 parent f5ed710 commit 81d1d0b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/components/profiles-modals/ImportProfileModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import OnlineModList from "../views/OnlineModList.vue";
export default class ImportProfileModal extends mixins(ProfilesMixin) {
private importUpdateSelection: "IMPORT" | "UPDATE" | null = null;
private importPhaseDescription: string = 'Downloading mods: 0%';
private importViaCodeInProgress: boolean = false;
private profileImportCode: string = '';
private listenerId: number = 0;
private newProfileName: string = '';
Expand Down Expand Up @@ -65,6 +66,7 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
this.listenerId = 0;
this.newProfileName = '';
this.importUpdateSelection = null;
this.importViaCodeInProgress = false;
this.profileImportCode = '';
this.profileImportFilePath = null;
this.profileImportContent = null;
Expand All @@ -74,11 +76,14 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
// User selects importing via code from the UI.
async importProfileUsingCode() {
try {
this.importViaCodeInProgress = true;
const filepath = await ProfileImportExport.downloadProfileCode(this.profileImportCode.trim());
await this.importProfileHandler([filepath]);
} catch (e: any) {
const err = R2Error.fromThrownValue(e, 'Failed to import profile');
this.$store.commit('error/handleError', err);
} finally {
this.importViaCodeInProgress = false;
}
}
Expand Down Expand Up @@ -252,7 +257,7 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
</template>
</ModalCard>

<ModalCard v-else-if="activeStep === 'IMPORT_CODE'" key="IMPORT_CODE" :is-active="isOpen" @close-modal="closeModal">
<ModalCard :can-close="!importViaCodeInProgress" v-else-if="activeStep === 'IMPORT_CODE'" key="IMPORT_CODE" :is-active="isOpen" @close-modal="closeModal">
<template v-slot:header>
<h2 class="modal-title">Enter the profile code</h2>
</template>
Expand All @@ -277,6 +282,13 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
v-if="!isProfileCodeValid(profileImportCode)">
Fix issues before importing
</button>
<button
disabled
id="modal-import-profile-from-code-loading"
class="button is-disabled"
v-else-if="importViaCodeInProgress">
Loading...
</button>
<button
id="modal-import-profile-from-code"
class="button is-info"
Expand Down
11 changes: 10 additions & 1 deletion src/r2mm/mods/ProfileImportExport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { retry } from "../../utils/Common";
import FileUtils from '../../utils/FileUtils';
import path from 'path';
import R2Error from "../../model/errors/R2Error";
import PathResolver from '../../r2mm/manager/PathResolver';
import FsProvider from '../../providers/generic/file/FsProvider';
import { ProfileApiClient } from '../../r2mm/profiles/ProfilesClient';
import {AxiosResponse} from "axios";

const IMPORT_CACHE_DIRNAME = "_import_cache";
const PROFILE_DATA_PREFIX = "#r2modman";
Expand Down Expand Up @@ -51,7 +53,14 @@ async function saveDownloadedProfile(profileData: string): Promise<string> {
*/
async function downloadProfileCode(profileCode: string): Promise<string> {
try {
const response = await ProfileApiClient.getProfile(profileCode);
const response: AxiosResponse<string> = await retry(
() => ProfileApiClient.getProfile(profileCode),
5,
1000,
undefined,
undefined,
true
);
return saveDownloadedProfile(response.data);
} catch (e: any) {
if (e.message === 'Network Error') {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export async function retry<T>(
attempts: number = 3,
interval: number = 5000,
canRetry: () => boolean = () => true,
onError: (e: Error | unknown) => void = console.error
onError: (e: Error | unknown) => void = console.error,
throwLastErrorAsIs: boolean = false
): Promise<T> {
for (let currentAttempt = 1; currentAttempt <= attempts; currentAttempt++) {
if (!canRetry()) {
Expand All @@ -46,6 +47,9 @@ export async function retry<T>(
if (currentAttempt < attempts) {
await sleep(interval);
}
if (throwLastErrorAsIs && currentAttempt === attempts) {
throw e;
}
}
}
throw new Error(`Retry failed after ${attempts} attempts!`);
Expand Down

0 comments on commit 81d1d0b

Please sign in to comment.