got rid of pesky type definitions for function returns and better map merge

This commit is contained in:
O2theC authored and Danielv123 committed 2025-03-15 15:29:59 +01:00
1 parent 24c07c0ed1
commit 64e429c5f6
1 file changed
+10 -9
+10 -9
View File
@@ -169,7 +169,7 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
return new this(modsDirectory, files); return new this(modsDirectory, files);
} }
private static getLatestVersionFromReleases(releases: Array<ModRelease>): string | undefined { private static getLatestVersionFromReleases(releases: Array<ModRelease>) {
if (releases.length === 0) { if (releases.length === 0) {
return undefined; return undefined;
} }
@@ -182,7 +182,7 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
return latestVersion; return latestVersion;
} }
private static async getLatestVersionsChunk(modNames: Array<string>): Promise<Map<string, string>> { private static async getLatestVersionsChunk(modNames: string[]) {
const url = new URL("https://mods.factorio.com/api/mods"); const url = new URL("https://mods.factorio.com/api/mods");
url.searchParams.set("page_size", "max"); url.searchParams.set("page_size", "max");
const response = await fetch(url, { const response = await fetch(url, {
@@ -207,18 +207,19 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
/** /**
* *
* @param modNames an array of mod names to check for their latest version * @param modNames an array of mod names to check for their latest version
* @returns a dict/object with the keys being mod names and values being their latest version, * @returns a Map with the keys being mod names and values being their latest version,
* it is not guarteed that all mods submited will be returned * it is not guarteed that all mods submited will be returned
*/ */
static async getLatestVersions(modNames: Array<string>): Promise<Map<string, string>> { static async getLatestVersions(modNames: string[]) {
const chunkSize = 500; const chunkSize = 500;
const chunks: Array<Map<string, string>> = []; let versions = new Map<string, string>();
for (let i = 0; i < modNames.length; i += chunkSize) { for (let i = 0; i < modNames.length; i += chunkSize) {
chunks.push(await ModStore.getLatestVersionsChunk(modNames.slice(i, i + chunkSize))); const chunk = (await ModStore.getLatestVersionsChunk(modNames.slice(i, i + chunkSize)));
for (const [modName, version] of chunk) {
versions.set(modName, version);
}
} }
const versions = chunks.reduce((acc, curr) => (
new Map<string, string>([...acc, ...curr])
), new Map<string, string>());
return versions; return versions;
} }