Skip to content

Commit e433150

Browse files
Simplify download logic by omitting callback
1 parent 6c26748 commit e433150

File tree

6 files changed

+150
-121
lines changed

6 files changed

+150
-121
lines changed

Diff for: src/components/mixins/DownloadMixin.vue

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export default class DownloadMixin extends Vue {
2121
this.$store.commit("closeDownloadModModal");
2222
}
2323
24+
setIsModProgressModalOpen(open: boolean): void {
25+
this.$store.commit('download/setIsModProgressModalOpen', open);
26+
}
27+
2428
get isOpen(): boolean {
2529
return this.$store.state.modals.isDownloadModModalOpen;
2630
}

Diff for: src/components/views/DownloadModModal.vue

+86-56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div>
33
<div id='downloadProgressModal' :class="['modal', {'is-active':$store.state.download.isModProgressModalOpen}]" v-if="$store.getters['download/currentDownload'] !== null">
4-
<div class="modal-background" @click="$store.commit('download/setIsModProgressModalOpen', false);"></div>
4+
<div class="modal-background" @click="setIsModProgressModalOpen(false);"></div>
55
<div class='modal-content'>
66
<div class='notification is-info'>
77
<h3 class='title'>Downloading {{$store.getters['download/currentDownload'].modName}}</h3>
@@ -13,7 +13,7 @@
1313
/>
1414
</div>
1515
</div>
16-
<button class="modal-close is-large" aria-label="close" @click="$store.commit('download/setIsModProgressModalOpen', false);"></button>
16+
<button class="modal-close is-large" aria-label="close" @click="setIsModProgressModalOpen(false);"></button>
1717
</div>
1818
<DownloadModVersionSelectModal @download-mod="downloadHandler" />
1919
<UpdateAllInstalledModsModal />
@@ -60,54 +60,65 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
6060
store: Store<any>
6161
): Promise<void> {
6262
return new Promise(async (resolve, reject) => {
63-
const tsMod = combo.getMod();
64-
const tsVersion = combo.getVersion();
6563
6664
const assignId = await store.dispatch(
6765
'download/addDownload',
68-
[`${tsMod.getName()} (${tsVersion.getVersionNumber().toString()})`]
66+
[`${combo.getMod().getName()} (${combo.getVersion().getVersionNumber().toString()})`]
6967
);
7068
71-
setTimeout(() => {
72-
ThunderstoreDownloaderProvider.instance.download(profile.asImmutableProfile(), tsMod, tsVersion, ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
73-
try {
74-
if (status === StatusEnum.FAILURE) {
75-
store.commit('download/updateDownload', {assignId, failed: true});
76-
if (err !== null) {
77-
DownloadMixin.addSolutionsToError(err);
78-
return reject(err);
79-
}
80-
} else if (status === StatusEnum.PENDING) {
81-
store.commit('download/updateDownload', {assignId, progress, modName});
82-
}
83-
} catch (e) {
84-
return reject(e);
85-
}
86-
}, async (downloadedMods: ThunderstoreCombo[]) => {
87-
ProfileModList.requestLock(async () => {
88-
for (const combo of downloadedMods) {
69+
setTimeout(async () => {
70+
let downloadedMods: ThunderstoreCombo[] = [];
71+
try {
72+
downloadedMods = await ThunderstoreDownloaderProvider.instance.download(
73+
profile.asImmutableProfile(),
74+
combo,
75+
ignoreCache,
76+
(progress: number, modName: string, status: number, err: R2Error | null) => {
8977
try {
90-
await DownloadModModal.installModAfterDownload(profile, combo.getMod(), combo.getVersion());
78+
DownloadModModal.downloadProgressCallback(store, assignId, progress, modName, status, err);
9179
} catch (e) {
92-
return reject(
93-
R2Error.fromThrownValue(e, `Failed to install mod [${combo.getMod().getFullName()}]`)
94-
);
80+
reject(e);
9581
}
9682
}
97-
const modList = await ProfileModList.getModList(profile.asImmutableProfile());
98-
if (!(modList instanceof R2Error)) {
99-
const err = await ConflictManagementProvider.instance.resolveConflicts(modList, profile.asImmutableProfile());
100-
if (err instanceof R2Error) {
101-
return reject(err);
102-
}
83+
);
84+
} catch (e) {
85+
return reject(e);
86+
}
87+
await ProfileModList.requestLock(async () => {
88+
for (const combo of downloadedMods) {
89+
try {
90+
await DownloadModModal.installModAfterDownload(profile, combo.getMod(), combo.getVersion());
91+
} catch (e) {
92+
return reject(
93+
R2Error.fromThrownValue(e, `Failed to install mod [${combo.getMod().getFullName()}]`)
94+
);
10395
}
104-
return resolve();
105-
});
96+
}
97+
const modList = await ProfileModList.getModList(profile.asImmutableProfile());
98+
if (!(modList instanceof R2Error)) {
99+
const err = await ConflictManagementProvider.instance.resolveConflicts(modList, profile.asImmutableProfile());
100+
if (err instanceof R2Error) {
101+
return reject(err);
102+
}
103+
}
104+
return resolve();
106105
});
107106
}, 1);
108107
});
109108
}
110109
110+
static downloadProgressCallback(store: Store<any>, assignId: number, progress: number, modName: string, status: number, err: R2Error | null) {
111+
if (status === StatusEnum.FAILURE) {
112+
store.commit('download/updateDownload', {assignId, failed: true});
113+
if (err !== null) {
114+
DownloadMixin.addSolutionsToError(err);
115+
throw err;
116+
}
117+
} else if (status === StatusEnum.PENDING) {
118+
store.commit('download/updateDownload', {assignId, progress, modName});
119+
}
120+
}
121+
111122
async downloadHandler(tsMod: ThunderstoreMod, tsVersion: ThunderstoreVersion) {
112123
this.closeModal();
113124
@@ -116,30 +127,49 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
116127
[`${tsMod.getName()} (${tsVersion.getVersionNumber().toString()})`]
117128
);
118129
119-
this.$store.commit('download/setIsModProgressModalOpen', true);
120-
setTimeout(() => {
121-
ThunderstoreDownloaderProvider.instance.download(this.profile.asImmutableProfile(), tsMod, tsVersion, this.ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
122-
try {
123-
if (status === StatusEnum.FAILURE) {
124-
this.$store.commit('download/setIsModProgressModalOpen', false);
125-
this.$store.commit('download/updateDownload', {assignId, failed: true});
126-
if (err !== null) {
127-
DownloadMixin.addSolutionsToError(err);
128-
throw err;
129-
}
130-
} else if (status === StatusEnum.PENDING) {
131-
this.$store.commit('download/updateDownload', {assignId, progress, modName});
132-
}
133-
} catch (e) {
134-
this.$store.commit('error/handleError', R2Error.fromThrownValue(e));
135-
}
136-
}, async (downloadedMods) => {
137-
await this.downloadCompletedCallback(downloadedMods);
138-
this.$store.commit('download/setIsModProgressModalOpen', false);
139-
});
130+
this.setIsModProgressModalOpen(true);
131+
132+
const tsCombo = new ThunderstoreCombo();
133+
tsCombo.setMod(tsMod);
134+
tsCombo.setVersion(tsVersion);
135+
136+
setTimeout(async () => {
137+
let downloadedMods: ThunderstoreCombo[] = [];
138+
try {
139+
downloadedMods = await ThunderstoreDownloaderProvider.instance.download(
140+
this.profile.asImmutableProfile(),
141+
tsCombo,
142+
this.ignoreCache,
143+
(progress, modName, status, err) => { this.downloadProgressCallback(assignId, progress, modName, status, err); }
144+
);
145+
} catch (e) {
146+
this.setIsModProgressModalOpen(false);
147+
this.$store.commit('error/handleError', R2Error.fromThrownValue(e));
148+
return;
149+
}
150+
await this.downloadCompletedCallback(downloadedMods);
151+
this.setIsModProgressModalOpen(false);
140152
}, 1);
153+
141154
}
142155
156+
downloadProgressCallback(assignId: number, progress: number, modName: string, status: number, err: R2Error | null) {
157+
try {
158+
if (status === StatusEnum.FAILURE) {
159+
this.setIsModProgressModalOpen(false);
160+
this.$store.commit('download/updateDownload', {assignId, failed: true});
161+
if (err !== null) {
162+
DownloadMixin.addSolutionsToError(err);
163+
throw err;
164+
}
165+
} else if (status === StatusEnum.PENDING) {
166+
this.$store.commit('download/updateDownload', {assignId, progress, modName});
167+
}
168+
} catch (e) {
169+
this.$store.commit('error/handleError', R2Error.fromThrownValue(e));
170+
}
171+
};
172+
143173
static async installModAfterDownload(profile: Profile, mod: ThunderstoreMod, version: ThunderstoreVersion): Promise<R2Error | void> {
144174
return new Promise(async (resolve, reject) => {
145175
const manifestMod: ManifestV2 = new ManifestV2().fromThunderstoreMod(mod, version);

Diff for: src/components/views/DownloadModVersionSelectModal.vue

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import ModalCard from "../ModalCard.vue";
6060
import DownloadMixin from "../../components/mixins/DownloadMixin.vue";
6161
import R2Error from "../../model/errors/R2Error";
6262
import ManifestV2 from "../../model/ManifestV2";
63-
import ThunderstoreMod from "../../model/ThunderstoreMod";
6463
import ThunderstoreVersion from "../../model/ThunderstoreVersion";
6564
import { MOD_LOADER_VARIANTS } from "../../r2mm/installing/profile_installers/ModLoaderVariantRecord";
6665
import * as PackageDb from "../../r2mm/manager/PackageDexieStore";

Diff for: src/components/views/UpdateAllInstalledModsModal.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export default class UpdateAllInstalledModsModal extends mixins(DownloadMixin)
4848
modsWithUpdates.map(value => `${value.getMod().getName()} (${value.getVersion().getVersionNumber().toString()})`)
4949
);
5050
51-
this.$store.commit('download/setIsModProgressModalOpen', true);
51+
this.setIsModProgressModalOpen(true);
5252
ThunderstoreDownloaderProvider.instance.downloadLatestOfAll(modsWithUpdates, this.ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
5353
try {
5454
if (status === StatusEnum.FAILURE) {
55-
this.$store.commit('download/setIsModProgressModalOpen', false);
55+
this.setIsModProgressModalOpen(false);
5656
this.$store.commit('download/updateDownload', {assignId, failed: true});
5757
if (err !== null) {
5858
DownloadMixin.addSolutionsToError(err);
@@ -66,7 +66,7 @@ export default class UpdateAllInstalledModsModal extends mixins(DownloadMixin)
6666
}
6767
}, async (downloadedMods) => {
6868
await this.downloadCompletedCallback(downloadedMods);
69-
this.$store.commit('download/setIsModProgressModalOpen', false);
69+
this.setIsModProgressModalOpen(false);
7070
});
7171
}
7272
}

Diff for: src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import ProviderUtils from '../../generic/ProviderUtils';
22
import ThunderstoreVersion from '../../../model/ThunderstoreVersion';
3-
import ThunderstoreMod from '../../../model/ThunderstoreMod';
43
import ThunderstoreCombo from '../../../model/ThunderstoreCombo';
54
import R2Error from '../../../model/errors/R2Error';
65
import { ImmutableProfile } from '../../../model/Profile';
@@ -49,17 +48,17 @@ export default abstract class ThunderstoreDownloaderProvider {
4948
/**
5049
* A top-level method to download the latest version of a mod including its dependencies.
5150
*
52-
* @param profile The profile the mod is downloaded for (needed to prevent dependencies from updating existing mods).
53-
* @param mod The mod to be downloaded.
54-
* @param modVersion The version of the mod to download.
55-
* @param ignoreCache Download mod even if it already exists in the cache.
56-
* @param callback Callback to show the current state of the downloads.
57-
* @param completedCallback Callback to perform final actions against. Only called if {@param callback} has not returned a failed status.
51+
* @param profile The profile the mod is downloaded for (needed to prevent dependencies from updating existing mods).
52+
* @param combo The combo to be downloaded.
53+
* @param ignoreCache Download mod even if it already exists in the cache.
54+
* @param totalProgressCallback Callback to show the combined state of all the downloads.
5855
*/
59-
public abstract download(profile: ImmutableProfile, mod: ThunderstoreMod, modVersion: ThunderstoreVersion,
60-
ignoreCache: boolean,
61-
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void,
62-
completedCallback: (modList: ThunderstoreCombo[]) => void): void;
56+
public abstract download(
57+
profile: ImmutableProfile,
58+
combo: ThunderstoreCombo,
59+
ignoreCache: boolean,
60+
totalProgressCallback: (progress: number, modName: string, status: number, err: R2Error | null) => void
61+
): Promise<ThunderstoreCombo[]>;
6362

6463
/**
6564
* A top-level method to download exact versions of exported mods.

0 commit comments

Comments
 (0)