Skip to content

Commit d9914ec

Browse files
Remove Promises and other unnecessary waiting
1 parent 09523e5 commit d9914ec

File tree

2 files changed

+57
-63
lines changed

2 files changed

+57
-63
lines changed

src/components/views/DownloadModModal.vue

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,63 +78,55 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
7878
combo: ThunderstoreCombo,
7979
store: Store<any>
8080
): Promise<void> {
81-
return new Promise(async (resolve, reject) => {
81+
const assignId = await store.dispatch(
82+
'download/addDownload',
83+
[`${combo.getMod().getName()} (${combo.getVersion().getVersionNumber().toString()})`]
84+
);
8285
83-
const assignId = await store.dispatch(
84-
'download/addDownload',
85-
[`${combo.getMod().getName()} (${combo.getVersion().getVersionNumber().toString()})`]
86+
let downloadedMods: ThunderstoreCombo[] = [];
87+
try {
88+
downloadedMods = await ThunderstoreDownloaderProvider.instance.download(
89+
profile,
90+
combo,
91+
store.state.download.ignoreCache,
92+
(downloadProgress: number, modName: string, status: number, err: R2Error | null) => {
93+
try {
94+
DownloadMixin.downloadProgressCallback(store, assignId, downloadProgress, modName, status, err);
95+
} catch (e) {
96+
throw e;
97+
}
98+
}
8699
);
87-
88-
setTimeout(async () => {
89-
let downloadedMods: ThunderstoreCombo[] = [];
100+
} catch (e) {
101+
store.commit('download/updateDownload', { assignId, failed: true });
102+
throw e;
103+
}
104+
await ProfileModList.requestLock(async () => {
105+
let currentDownloadIndex = 0;
106+
for (const combo of downloadedMods) {
90107
try {
91-
downloadedMods = await ThunderstoreDownloaderProvider.instance.download(
92-
profile,
93-
combo,
94-
store.state.download.ignoreCache,
95-
(downloadProgress: number, modName: string, status: number, err: R2Error | null) => {
96-
try {
97-
DownloadMixin.downloadProgressCallback(store, assignId, downloadProgress, modName, status, err);
98-
} catch (e) {
99-
reject(e);
100-
}
101-
}
102-
);
108+
await store.dispatch('download/installModAfterDownload', {profile, combo});
103109
} catch (e) {
104110
store.commit('download/updateDownload', { assignId, failed: true });
105-
return reject(e);
111+
throw R2Error.fromThrownValue(e, `Failed to install mod [${combo.getMod().getFullName()}]`);
106112
}
107-
await ProfileModList.requestLock(async () => {
108-
let currentDownloadIndex = 0;
109-
for (const combo of downloadedMods) {
110-
try {
111-
await store.dispatch('download/installModAfterDownload', {profile, combo});
112-
} catch (e) {
113-
store.commit('download/updateDownload', { assignId, failed: true });
114-
return reject(
115-
R2Error.fromThrownValue(e, `Failed to install mod [${combo.getMod().getFullName()}]`)
116-
);
117-
}
118-
store.commit('download/updateDownload', {
119-
assignId,
120-
modName: combo.getMod().getName(),
121-
installProgress: ThunderstoreDownloaderProvider.instance.generateProgressPercentage(100, currentDownloadIndex, downloadedMods.length)
122-
});
123-
currentDownloadIndex++;
124-
}
125-
const modList = await ProfileModList.getModList(profile);
126-
if (modList instanceof R2Error) {
127-
store.commit('download/updateDownload', { assignId, failed: true });
128-
return reject(modList);
129-
}
130-
const err = await ConflictManagementProvider.instance.resolveConflicts(modList, profile);
131-
if (err instanceof R2Error) {
132-
store.commit('download/updateDownload', { assignId, failed: true });
133-
return reject(err);
134-
}
135-
return resolve();
113+
store.commit('download/updateDownload', {
114+
assignId,
115+
modName: combo.getMod().getName(),
116+
installProgress: ThunderstoreDownloaderProvider.instance.generateProgressPercentage(100, currentDownloadIndex, downloadedMods.length)
136117
});
137-
}, 1);
118+
currentDownloadIndex++;
119+
}
120+
const modList = await ProfileModList.getModList(profile);
121+
if (modList instanceof R2Error) {
122+
store.commit('download/updateDownload', { assignId, failed: true });
123+
throw modList;
124+
}
125+
const err = await ConflictManagementProvider.instance.resolveConflicts(modList, profile);
126+
if (err instanceof R2Error) {
127+
store.commit('download/updateDownload', { assignId, failed: true });
128+
throw err;
129+
}
138130
});
139131
}
140132

src/pages/Manager.vue

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ import { LogSeverity } from '../providers/ror2/logging/LoggerProvider';
160160
161161
import Profile from '../model/Profile';
162162
import VersionNumber from '../model/VersionNumber';
163-
import R2Error from '../model/errors/R2Error';
163+
import R2Error, { throwForR2Error } from '../model/errors/R2Error';
164164
import ManifestV2 from '../model/ManifestV2';
165165
import ManagerSettings from '../r2mm/manager/ManagerSettings';
166166
import ThemeManager from '../r2mm/manager/ThemeManager';
@@ -637,18 +637,20 @@ import ModalCard from '../components/ModalCard.vue';
637637
});
638638
return;
639639
}
640-
DownloadModModal.downloadSpecific(this.profile.asImmutableProfile(), combo, this.$store)
641-
.then(async value => {
642-
const modList = await ProfileModList.getModList(this.profile.asImmutableProfile());
643-
if (!(modList instanceof R2Error)) {
644-
await this.$store.dispatch('profile/updateModList', modList);
645-
} else {
646-
this.$store.commit('error/handleError', modList);
647-
}
648-
})
649-
.catch(
650-
(err: R2Error) => this.$store.commit('error/handleError', err)
651-
);
640+
641+
try {
642+
await DownloadModModal.downloadSpecific(this.profile.asImmutableProfile(), combo, this.$store);
643+
} catch (err) {
644+
this.$store.commit('error/handleError', err as R2Error);
645+
return;
646+
}
647+
648+
const modList = await ProfileModList.getModList(this.profile.asImmutableProfile());
649+
if (modList instanceof R2Error) {
650+
this.$store.commit('error/handleError', modList);
651+
return;
652+
}
653+
await this.$store.dispatch('profile/updateModList', modList);
652654
});
653655
654656
this.isManagerUpdateAvailable();

0 commit comments

Comments
 (0)