From 9a189073d63f3ea636aaceb20d2d85850475bbe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Tue, 30 Jan 2024 17:03:11 +0200 Subject: [PATCH] Improve performance of Manager.setAllModsEnabled - Skip processing the mods that already are in the desired state - Update VueX store only once at the end, not after each mod, since this will trigger a costly rerendering (which will be addressed separately - Wrap the logic in try-catch to avoid state drift on unexpected errors --- src/pages/Manager.vue | 54 ++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/pages/Manager.vue b/src/pages/Manager.vue index 7d2e45dee..411ff873f 100644 --- a/src/pages/Manager.vue +++ b/src/pages/Manager.vue @@ -499,30 +499,46 @@ import CategoryFilterModal from '../components/modals/CategoryFilterModal.vue'; } async setAllModsEnabled(enabled: boolean) { - for (const mod of this.localModList) { - let profileErr: R2Error | void; - if (enabled) { - profileErr = await ProfileInstallerProvider.instance.enableMod(mod, this.contextProfile!); - } else { - profileErr = await ProfileInstallerProvider.instance.disableMod(mod, this.contextProfile!); - } - if (profileErr instanceof R2Error) { - this.showError(profileErr); - continue; - } - const update: ManifestV2[] | R2Error = await ProfileModList.updateMod(mod, this.contextProfile!, async (updatingMod: ManifestV2) => { + let lastSuccessfulUpdate: ManifestV2[] = []; + + try { + for (const mod of this.localModList) { + if (mod.isEnabled() === enabled) { + continue; + } + + let profileErr: R2Error | void; if (enabled) { - updatingMod.enable(); + profileErr = await ProfileInstallerProvider.instance.enableMod(mod, this.contextProfile!); } else { - updatingMod.disable(); + profileErr = await ProfileInstallerProvider.instance.disableMod(mod, this.contextProfile!); } - }); - if (update instanceof R2Error) { - this.showError(update); - continue; + if (profileErr instanceof R2Error) { + this.showError(profileErr); + continue; + } + const update: ManifestV2[] | R2Error = await ProfileModList.updateMod(mod, this.contextProfile!, async (updatingMod: ManifestV2) => { + if (enabled) { + updatingMod.enable(); + } else { + updatingMod.disable(); + } + }); + if (update instanceof R2Error) { + this.showError(update); + } else { + lastSuccessfulUpdate = update; + } + } + } catch (e) { + const name = `Error ${enabled ? "enabling" : "disabling"} mods`; + this.showError(R2Error.fromThrownValue(e, name)); + } finally { + if (lastSuccessfulUpdate.length) { + await this.$store.dispatch("updateModList", lastSuccessfulUpdate); } - await this.$store.dispatch("updateModList", update); } + await this.$router.push({name: "manager.installed"}); }