-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PoC of mod download/install through vuex store
TODO: Installation is not yet done, but it should be pretty similar to download.
- Loading branch information
1 parent
5e73a77
commit bc696f8
Showing
3 changed files
with
125 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { ActionTree, GetterTree } from 'vuex'; | ||
|
||
import StatusEnum from '../../model/enums/StatusEnum'; | ||
import { ImmutableProfile } from '../../model/Profile'; | ||
import ThunderstoreCombo from '../../model/ThunderstoreCombo'; | ||
import ThunderstoreMod from '../../model/ThunderstoreMod'; | ||
import ThunderstoreVersion from '../../model/ThunderstoreVersion'; | ||
import ThunderstoreDownloaderProvider from '../../providers/ror2/downloading/ThunderstoreDownloaderProvider'; | ||
|
||
import { State as RootState } from '../index'; | ||
|
||
interface Progress { | ||
progress: number; | ||
status: number; // StatusEnum | ||
} | ||
|
||
interface ModProgressItem { | ||
mod: ThunderstoreMod; | ||
version: ThunderstoreVersion; | ||
installProgress: Progress; | ||
downloadProgress: Progress; | ||
} | ||
|
||
interface State { | ||
allDownloads: ModProgressItem[]; | ||
} | ||
|
||
/** | ||
* State for Mod Downloadning/Installation. | ||
*/ | ||
export default { | ||
namespaced: true, | ||
|
||
state: (): State => ({ | ||
allDownloads: [], | ||
}), | ||
|
||
getters: <GetterTree<State, RootState>>{ | ||
allDownloads(state): ModProgressItem[] { | ||
return state.allDownloads; | ||
}, | ||
activeDownload(state): ModProgressItem { | ||
return state.allDownloads.slice(-1)[0]; // Last element of the array | ||
}, | ||
activeDownloadProgress(state): number | undefined { | ||
if(state.allDownloads.length > 0) { | ||
return state.allDownloads.slice(-1)[0].downloadProgress.progress; // Last element of the array | ||
} | ||
}, | ||
}, | ||
mutations: {}, | ||
actions: <ActionTree<State, RootState>>{ | ||
|
||
//TODO: Do the installation | ||
async downloadAndInstallMod( | ||
{dispatch, getters, state}, | ||
params: { | ||
profile: ImmutableProfile, | ||
mod: ThunderstoreMod, | ||
version: ThunderstoreVersion, | ||
} | ||
): Promise<boolean> { | ||
state.allDownloads.push({ | ||
mod: params.mod, | ||
version: params.version, | ||
installProgress: { progress: 0, status: StatusEnum.PENDING }, | ||
downloadProgress: { progress: 0, status: StatusEnum.PENDING } | ||
}); | ||
|
||
return new Promise((resolve, reject) => { | ||
ThunderstoreDownloaderProvider.instance.download( | ||
params.profile, | ||
params.mod, | ||
params.version, | ||
true, | ||
(progress, modName, status, err) => { | ||
const index = state.allDownloads.findIndex((modProgressItem) => modProgressItem.mod.getName() === modName); | ||
|
||
if (status === StatusEnum.FAILURE) { | ||
if(err) { | ||
reject(err); | ||
} | ||
} | ||
if (status === StatusEnum.PENDING) { | ||
if (index !== -1) { | ||
state.allDownloads[index].downloadProgress = { progress: progress, status: status }; | ||
} | ||
} | ||
}, | ||
() => { | ||
resolve(true); | ||
} | ||
); | ||
}); | ||
}, | ||
}, | ||
} |