Skip to content

Commit

Permalink
Clean up the downloadn state management Vuex store and it's usages
Browse files Browse the repository at this point in the history
- Remove assignId field
- Remove downloadObject field
- Rename some fields and functions to be more descriptive
- assignId is no longer passed unnecessarily when calling vuex store
  functions
  • Loading branch information
VilppeRiskidev committed Dec 18, 2024
1 parent cc472eb commit c71b670
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 93 deletions.
95 changes: 27 additions & 68 deletions src/components/views/DownloadModModal.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<template>
<div>
<div id='downloadProgressModal' :class="['modal', {'is-active':downloadingMod}]" v-if="$store.state.download.downloadObject !== null">
<div id='downloadProgressModal' :class="['modal', {'is-active':downloadingMod}]" v-if="$store.getters['download/currentDownload'] !== null">
<div class="modal-background" @click="downloadingMod = false;"></div>
<div class='modal-content'>
<div class='notification is-info'>
<h3 class='title'>Downloading {{$store.state.download.downloadObject.modName}}</h3>
<p>{{Math.floor($store.state.download.downloadObject.progress)}}% complete</p>
<h3 class='title'>Downloading {{$store.getters['download/currentDownload'].modName}}</h3>
<p>{{Math.floor($store.getters['download/currentDownload'].progress)}}% complete</p>
<Progress
:max='100'
:value='$store.state.download.downloadObject.progress'
:value="$store.getters['download/currentDownload'].progress"
:className="['is-dark']"
/>
</div>
Expand Down Expand Up @@ -60,14 +60,7 @@ import ConflictManagementProvider from '../../providers/generic/installing/Confl
import ProfileInstallerProvider from '../../providers/ror2/installing/ProfileInstallerProvider';
import ThunderstoreDownloaderProvider from '../../providers/ror2/downloading/ThunderstoreDownloaderProvider';
import ProfileModList from '../../r2mm/mods/ProfileModList';
interface DownloadProgress {
assignId: number;
initialMods: string[];
modName: string;
progress: number;
failed: boolean;
}
import { DownloadProgress } from "../../store/modules/DownloadModule";
@Component({
components: {
Expand Down Expand Up @@ -95,8 +88,7 @@ interface DownloadProgress {
const tsMod = combo.getMod();
const tsVersion = combo.getVersion();
store.commit('download/increaseAssignId');
const currentAssignId = store.state.download.assignId;
const currentAssignId = store.getters["download/currentDownload"]?.assignId + 1 || 0;
const progressObject = {
progress: 0,
Expand All @@ -106,22 +98,17 @@ interface DownloadProgress {
failed: false,
};
store.commit('download/pushDownloadObjectToAllVersions', {
assignId: currentAssignId,
downloadObject: progressObject
});
store.commit('download/addDownload', progressObject);
setTimeout(() => {
ThunderstoreDownloaderProvider.instance.download(profile.asImmutableProfile(), tsMod, tsVersion, ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
const assignIndex = store.state.download.allVersions.findIndex(([number, val]: [number, DownloadProgress]) => number === currentAssignId);
if (status === StatusEnum.FAILURE) {
if (err !== null) {
const existing = store.state.download.allVersions[assignIndex]
existing[1].failed = true;
store.commit('download/updateDownloadObject', {
assignId: assignIndex,
downloadVersion: [currentAssignId, existing[1]]
const existing = store.state.download.allDownloads.find((dlObj: DownloadProgress) => {
return dlObj.assignId === currentAssignId;
});
existing.failed = true;
store.commit('download/updateDownload', existing);
DownloadModModal.addSolutionsToError(err);
return reject(err);
}
Expand All @@ -133,10 +120,7 @@ interface DownloadProgress {
assignId: currentAssignId,
failed: false,
}
store.commit('download/updateDownloadObject', {
assignId: assignIndex,
downloadVersion: [currentAssignId, obj]
});
store.commit('download/updateDownload', obj);
}
}, async (downloadedMods: ThunderstoreCombo[]) => {
ProfileModList.requestLock(async () => {
Expand Down Expand Up @@ -167,8 +151,7 @@ interface DownloadProgress {
this.closeModal();
const modsWithUpdates: ThunderstoreCombo[] = await this.$store.dispatch('profile/getCombosWithUpdates');
this.$store.commit('download/increaseAssignId');
const currentAssignId = this.$store.state.download.assignId;
const currentAssignId = this.$store.getters["download/currentDownload"]?.assignId + 1 || 0;
const progressObject = {
progress: 0,
Expand All @@ -177,23 +160,17 @@ interface DownloadProgress {
assignId: currentAssignId,
failed: false,
};
this.$store.commit('download/setDownloadObject', progressObject);
this.$store.commit('download/pushDownloadObjectToAllVersions', {
assignId: currentAssignId,
downloadObject: progressObject
});
this.$store.commit('download/addDownload', progressObject);
this.downloadingMod = true;
ThunderstoreDownloaderProvider.instance.downloadLatestOfAll(modsWithUpdates, this.ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
const assignIndex = this.$store.state.download.allVersions.findIndex(([number, val]: [number, DownloadProgress]) => number === currentAssignId);
if (status === StatusEnum.FAILURE) {
if (err !== null) {
this.downloadingMod = false;
const existing = this.$store.state.download.allVersions[assignIndex];
existing[1].failed = true;
this.$store.commit('download/updateDownloadObject', {
assignId: assignIndex,
downloadVersion: [currentAssignId, existing[1]]
const existing = this.$store.state.download.allDownloads.find((dlObj: DownloadProgress) => {
return dlObj.assignId === currentAssignId;
});
existing.failed = true;
this.$store.commit('download/updateDownload', existing);
DownloadModModal.addSolutionsToError(err);
this.$store.commit('error/handleError', err);
return;
Expand All @@ -206,13 +183,7 @@ interface DownloadProgress {
assignId: currentAssignId,
failed: false,
}
if (this.$store.state.download.downloadObject!.assignId === currentAssignId) {
this.$store.commit('download/setDownloadObject', obj);
}
this.$store.commit('download/updateDownloadObject', {
assignId: assignIndex,
downloadVersion: [currentAssignId, obj]
});
this.$store.commit('download/updateDownload', obj);
}
}, (downloadedMods) => {
this.downloadCompletedCallback(downloadedMods);
Expand All @@ -223,8 +194,7 @@ interface DownloadProgress {
downloadHandler(tsMod: ThunderstoreMod, tsVersion: ThunderstoreVersion) {
this.closeModal();
this.$store.commit('download/increaseAssignId');
const currentAssignId = this.$store.state.download.assignId;
const currentAssignId = this.$store.getters["download/currentDownload"]?.assignId + 1 || 0;
const progressObject = {
progress: 0,
Expand All @@ -233,24 +203,19 @@ interface DownloadProgress {
assignId: currentAssignId,
failed: false,
};
this.$store.commit('download/setDownloadObject', progressObject);
this.$store.commit('download/pushDownloadObjectToAllVersions', {
assignId: currentAssignId,
downloadObject: this.$store.state.download.downloadObject
});
this.$store.commit('download/addDownload', progressObject);
this.downloadingMod = true;
setTimeout(() => {
ThunderstoreDownloaderProvider.instance.download(this.profile.asImmutableProfile(), tsMod, tsVersion, this.ignoreCache, (progress: number, modName: string, status: number, err: R2Error | null) => {
const assignIndex = this.$store.state.download.allVersions.findIndex(([number, val]: [number, DownloadProgress]) => number === currentAssignId);
if (status === StatusEnum.FAILURE) {
if (err !== null) {
this.downloadingMod = false;
const existing = this.$store.state.download.allVersions[assignIndex];
existing[1].failed = true;
this.$store.commit('download/updateDownloadObject', {
assignId: assignIndex,
downloadVersion: [currentAssignId, existing[1]]
const existing = this.$store.state.download.allDownloads.find((dlObj: DownloadProgress) => {
return dlObj.assignId === currentAssignId;
});
existing.failed = true;
this.$store.commit('download/updateDownload', existing);
DownloadModModal.addSolutionsToError(err);
this.$store.commit('error/handleError', err);
return;
Expand All @@ -263,13 +228,7 @@ interface DownloadProgress {
assignId: currentAssignId,
failed: false,
}
if (this.$store.state.download.downloadObject!.assignId === currentAssignId) {
this.$store.commit('download/setDownloadObject', obj);
}
this.$store.commit('download/updateDownloadObject', {
assignId: assignIndex,
downloadVersion: [currentAssignId, obj]
});
this.$store.commit('download/updateDownload', obj);
}
}, (downloadedMods) => {
this.downloadCompletedCallback(downloadedMods);
Expand Down
12 changes: 7 additions & 5 deletions src/pages/DownloadMonitor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>
</template>
<template v-else>
<div v-for="([assignId, downloadObject], index) of activeDownloads" :key="`download-progress-${index}`">
<div v-for="(downloadObject, index) of activeDownloads" :key="`download-progress-${index}`">
<div>
<div class="container margin-right">
<div class="border-at-bottom pad pad--sides">
Expand Down Expand Up @@ -42,10 +42,12 @@

<script lang="ts">
import Timeout = NodeJS.Timeout;
import { Component, Vue } from 'vue-property-decorator';
import { Hero } from '../components/all';
import Progress from '../components/Progress.vue';
import Timeout = NodeJS.Timeout;
import { DownloadProgress } from "../store/modules/DownloadModule";
@Component({
components: {
Expand All @@ -55,12 +57,12 @@ import Timeout = NodeJS.Timeout;
})
export default class DownloadMonitor extends Vue {
private refreshInterval!: Timeout;
private activeDownloads: [number, any][] = [];
private activeDownloads: DownloadProgress[] = [];
created() {
this.activeDownloads = [...this.$store.state.download.allVersions].reverse();
this.activeDownloads = [...this.$store.state.download.allDownloads].reverse();
this.refreshInterval = setInterval(() => {
this.activeDownloads = [...this.$store.state.download.allVersions].reverse();
this.activeDownloads = [...this.$store.state.download.allDownloads].reverse();
}, 100);
}
Expand Down
45 changes: 25 additions & 20 deletions src/store/modules/DownloadModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GetterTree } from "vuex";

import { State as RootState } from "../../store";
import R2Error from "../../model/errors/R2Error";

export interface DownloadProgress {
assignId: number;
Expand All @@ -11,9 +13,7 @@ export interface DownloadProgress {


interface State {
allVersions: [number, DownloadProgress][],
assignId: number,
downloadObject: DownloadProgress | null,
allDownloads: DownloadProgress[],
}

/**
Expand All @@ -23,34 +23,39 @@ export const DownloadModule = {
namespaced: true,

state: (): State => ({
allVersions: [],
assignId: 0,
downloadObject: null, // TODO: Check if the last element of allVersions can be used instead of this
allDownloads: [],
}),

getters: <GetterTree<State, RootState>>{
activeDownloadCount(state) {
const active = state.allVersions.filter(
dl => !dl[1].failed && dl[1].progress < 100
const active = state.allDownloads.filter(
dl => !dl.failed && dl.progress < 100
);
return active.length;
},
currentDownload(state) {
return state.allDownloads[state.allDownloads.length-1] || null;
},
},

mutations: {
setDownloadObject(state: State, downloadObject: DownloadProgress) {
state.downloadObject = {...downloadObject};
},
pushDownloadObjectToAllVersions(state: State, params: { assignId: number, downloadObject: DownloadProgress }) {
state.allVersions = [...state.allVersions, [params.assignId, params.downloadObject]];
addDownload(state: State, downloadObject: DownloadProgress) {
state.allDownloads = [...state.allDownloads, downloadObject];
},
updateDownloadObject(state: State, params: { assignId: number, downloadVersion: [number, DownloadProgress]}) {
const newVersions = [...state.allVersions];
newVersions[params.assignId] = params.downloadVersion;
state.allVersions = newVersions;
updateDownload(state: State, downloadObject: DownloadProgress) {
const newVersions = [...state.allDownloads];
const index = newVersions.findIndex((oldDownloadObject: DownloadProgress) => {
return oldDownloadObject.assignId === downloadObject.assignId;
});
if (index === -1) {
throw new R2Error(
'Failed to update download status.',
'Download status object with a specified index was not found, which means that there\'s a mismatch between the download statuses in memory and what\'s being iterated over.',
'Try initiating the download again or restarting the app.'
);
}
newVersions[index] = downloadObject;
state.allDownloads = newVersions;
},
increaseAssignId(state: State) {
state.assignId++;
}
},
}

0 comments on commit c71b670

Please sign in to comment.