Skip to content

Commit e209e1d

Browse files
Simplify download logic by omitting callback
1 parent 24dcf1a commit e209e1d

File tree

3 files changed

+67
-63
lines changed

3 files changed

+67
-63
lines changed

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

+23-25
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
6868
[`${tsMod.getName()} (${tsVersion.getVersionNumber().toString()})`]
6969
);
7070
71-
setTimeout(() => {
72-
ThunderstoreDownloaderProvider.instance.download(profile.asImmutableProfile(), tsMod, tsVersion, ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
71+
setTimeout(async () => {
72+
const downloadedMods = await ThunderstoreDownloaderProvider.instance.download(profile.asImmutableProfile(), tsMod, tsVersion, ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
7373
try {
7474
if (status === StatusEnum.FAILURE) {
7575
store.commit('download/updateDownload', {assignId, failed: true});
@@ -83,26 +83,25 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
8383
} catch (e) {
8484
return reject(e);
8585
}
86-
}, async (downloadedMods: ThunderstoreCombo[]) => {
87-
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-
);
95-
}
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+
);
9695
}
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-
}
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);
103102
}
104-
return resolve();
105-
});
103+
}
104+
return resolve();
106105
});
107106
}, 1);
108107
});
@@ -117,8 +116,8 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
117116
);
118117
119118
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) => {
119+
setTimeout(async () => {
120+
const downloadedMods = await ThunderstoreDownloaderProvider.instance.download(this.profile.asImmutableProfile(), tsMod, tsVersion, this.ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
122121
try {
123122
if (status === StatusEnum.FAILURE) {
124123
this.$store.commit('download/setIsModProgressModalOpen', false);
@@ -133,10 +132,9 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
133132
} catch (e) {
134133
this.$store.commit('error/handleError', R2Error.fromThrownValue(e));
135134
}
136-
}, async (downloadedMods) => {
137-
await this.downloadCompletedCallback(downloadedMods);
138-
this.$store.commit('download/setIsModProgressModalOpen', false);
139135
});
136+
this.$store.commit('download/setIsModProgressModalOpen', false);
137+
await this.downloadCompletedCallback(downloadedMods);
140138
}, 1);
141139
}
142140

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ export default abstract class ThunderstoreDownloaderProvider {
5454
* @param modVersion The version of the mod to download.
5555
* @param ignoreCache Download mod even if it already exists in the cache.
5656
* @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.
5857
*/
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;
58+
public abstract download(
59+
profile: ImmutableProfile,
60+
mod: ThunderstoreMod,
61+
modVersion: ThunderstoreVersion,
62+
ignoreCache: boolean,
63+
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void
64+
): Promise<ThunderstoreCombo[]>;
6365

6466
/**
6567
* A top-level method to download exact versions of exported mods.

Diff for: src/r2mm/downloading/BetterThunderstoreDownloader.ts

+37-33
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,17 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
8383
});
8484
}
8585

86-
public async download(profile: ImmutableProfile, mod: ThunderstoreMod, modVersion: ThunderstoreVersion,
87-
ignoreCache: boolean,
88-
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void,
89-
completedCallback: (modList: ThunderstoreCombo[]) => void) {
86+
public async download(
87+
profile: ImmutableProfile, mod: ThunderstoreMod, modVersion: ThunderstoreVersion,
88+
ignoreCache: boolean,
89+
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void
90+
): Promise<ThunderstoreCombo[]> {
91+
9092

9193
const modList = await ProfileModList.getModList(profile);
9294
if (modList instanceof R2Error) {
93-
return callback(0, mod.getName(), StatusEnum.FAILURE, modList);
95+
callback(0, mod.getName(), StatusEnum.FAILURE, modList);
96+
throw modList;
9497
}
9598

9699
let dependencies: ThunderstoreCombo[] = [];
@@ -101,40 +104,41 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
101104
combo.setVersion(modVersion);
102105

103106
dependencies = await this.determineDependencyVersions(dependencies, combo, modVersion, modList);
104-
let downloadableDependencySize = this.calculateInitialDownloadSize(dependencies);
107+
const allModsToDownload = [...dependencies, combo];
105108

106109
let downloadCount = 0;
107-
await this.downloadAndSave(combo, ignoreCache, async (progress: number, status: number, err: R2Error | null) => {
110+
const singleModProgressCallback = (progress: number, status: number, err: R2Error | null) => {
108111
if (status === StatusEnum.FAILURE) {
109-
callback(0, mod.getName(), status, err);
110-
} else if (status === StatusEnum.PENDING) {
111-
callback(this.generateProgressPercentage(progress, downloadCount, downloadableDependencySize + 1), mod.getName(), status, err);
112+
throw err;
113+
}
114+
115+
let totalProgress: number;
116+
if (status === StatusEnum.PENDING) {
117+
totalProgress = this.generateProgressPercentage(progress, downloadCount, allModsToDownload.length);
112118
} else if (status === StatusEnum.SUCCESS) {
119+
totalProgress = this.generateProgressPercentage(100, downloadCount, allModsToDownload.length);
113120
downloadCount += 1;
114-
// If no dependencies, end here.
115-
if (dependencies.length === 0) {
116-
callback(100, mod.getName(), StatusEnum.PENDING, err);
117-
completedCallback([combo]);
118-
return;
119-
}
121+
} else {
122+
console.error(`Ignore unknown status code "${status}"`);
123+
return;
124+
}
125+
callback(totalProgress, mod.getName(), status, err);
126+
}
120127

121-
// If dependencies, queue and download.
122-
await this.queueDownloadDependencies(dependencies.entries(), ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
123-
if (status === StatusEnum.FAILURE) {
124-
callback(0, modName, status, err);
125-
} else if (status === StatusEnum.PENDING) {
126-
callback(this.generateProgressPercentage(progress, downloadCount, downloadableDependencySize + 1), modName, status, err);
127-
} else if (status === StatusEnum.SUCCESS) {
128-
callback(this.generateProgressPercentage(progress, downloadCount, downloadableDependencySize + 1), modName, StatusEnum.PENDING, err);
129-
downloadCount += 1;
130-
if (downloadCount >= dependencies.length + 1) {
131-
callback(100, modName, StatusEnum.PENDING, err);
132-
completedCallback([...dependencies, combo]);
133-
}
134-
}
135-
});
128+
for (const combo of allModsToDownload) {
129+
if (!ignoreCache && await this.isVersionAlreadyDownloaded(combo)) {
130+
callback(100, combo.getMod().getName(), StatusEnum.SUCCESS, null);
131+
continue;
136132
}
137-
})
133+
134+
try {
135+
const response = await this._downloadCombo(combo, singleModProgressCallback);
136+
await this._saveDownloadResponse(response, combo, singleModProgressCallback);
137+
} catch(e) {
138+
throw R2Error.fromThrownValue(e, `Failed to download mod ${combo.getVersion().getFullName()}`);
139+
}
140+
}
141+
return allModsToDownload;
138142
}
139143

140144
// If combo is a modpack, use the modpack's dependency versions. If it isn't, get the latest versions.
@@ -185,7 +189,7 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
185189

186190
try {
187191
const response = await this._downloadCombo(combo, singleModProgressCallback);
188-
await this._saveDownloadResponse(response, combo, singleModProgressCallback);
192+
await this._saveDownloadResponse(response, combo, singleModProgressCallback); //TODO: track installation separately
189193
} catch(e) {
190194
throw R2Error.fromThrownValue(e, `Failed to download mod ${combo.getVersion().getFullName()}`);
191195
}

0 commit comments

Comments
 (0)