feat: migrating repacks to a worker

This commit is contained in:
Chubby Granny Chaser
2024-06-03 14:34:02 +01:00
parent eb3eb88f23
commit 5a85033486
19 changed files with 204 additions and 147 deletions

View File

@@ -1,5 +1,6 @@
import path from "node:path";
import steamGamesWorkerPath from "./steam-games.worker?modulePath";
import repacksWorkerPath from "./repacks.worker?modulePath";
import Piscina from "piscina";
@@ -11,3 +12,7 @@ export const steamGamesWorker = new Piscina({
steamGamesPath: path.join(seedsPath, "steam-games.json"),
},
});
export const repacksWorker = new Piscina({
filename: repacksWorkerPath,
});

View File

@@ -0,0 +1,31 @@
import { formatName } from "@shared";
import { CatalogueEntry, GameRepack } from "@types";
import flexSearch from "flexsearch";
const repacksIndex = new flexSearch.Index();
const state: { repacks: GameRepack[] } = { repacks: [] };
export const setRepacks = (repacks: GameRepack[]) => {
state.repacks = repacks;
for (let i = 0; i < repacks.length; i++) {
const repack = repacks[i];
const formattedTitle = formatName(repack.title);
repacksIndex.add(i, formattedTitle);
}
};
export const search = (options: flexSearch.SearchOptions) =>
repacksIndex.search(options).map((index) => state.repacks[index]);
export const list = () => state.repacks;
export const findRepacksForCatalogueEntries = (entries: CatalogueEntry[]) => {
return entries.map((entry) => {
const repacks = search({ query: formatName(entry.title) });
return { ...entry, repacks };
});
};

View File

@@ -25,7 +25,7 @@ for (let i = 0; i < steamGames.length; i++) {
}
export const search = (options: flexSearch.SearchOptions) => {
const results = steamGamesIndex.search(options.query ?? "", options);
const results = steamGamesIndex.search(options);
const games = results.map((index) => steamGames[index]);
return orderBy(games, ["name"], ["asc"]);