From 5c87aa460fb95a7d340ff1f5adda01d68bf42d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4ki?= Date: Fri, 19 Apr 2024 14:58:52 +0300 Subject: [PATCH] Fix default ordering on OnlineModView The default order on which the packages are shown on OnlineModView is "whatever was received from the API". Old implementation cached the results in a file, where they could be written and read in the same order. When the cache was moved to IndexedDB, the order was inadvertently changed to "whatever the database returns". To stay true to the old implementation, order of each package is stored in each entry in the database, so the result set can be ordered by this field. Dexie/IndexedDB doesn't support both filtering and ordering the results on database level, so the ordering is now on JS instead. The alternative would be to fetch all packages in order and filter packages from other communities on JS. Determining which way is the most efficient is difficult since there seems to be a lot of arbitrary variance in the executions times, at least on the dev build. The performance of the implementation in this commit didn't seem to differ substantially from the unordered implementation, so it was deemed good enough. --- src/r2mm/manager/PackageDexieStore.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/r2mm/manager/PackageDexieStore.ts b/src/r2mm/manager/PackageDexieStore.ts index fa335f1fb..fbbf5ff43 100644 --- a/src/r2mm/manager/PackageDexieStore.ts +++ b/src/r2mm/manager/PackageDexieStore.ts @@ -37,6 +37,7 @@ interface DexiePackage { // Extra fields not included in the API response community: string; date_fetched: Date; // When the entry was fetched from the API + default_order: number; // Entry's index when received from the API } class PackageDexieStore extends Dexie { @@ -56,7 +57,11 @@ const db = new PackageDexieStore(); // TODO: user type guards to validate (part of) the data before operations? export async function updateFromApiResponse(community: string, packages: any[]) { const extra = {community, date_fetched: new Date()}; - const newPackages: DexiePackage[] = packages.map((pkg) => ({...pkg, ...extra})); + const newPackages: DexiePackage[] = packages.map((pkg, i) => ({ + ...pkg, + ...extra, + default_order: i + })); await db.transaction( 'rw', @@ -87,7 +92,7 @@ export async function updateFromApiResponse(community: string, packages: any[]) } export async function getPackagesAsThunderstoreMods(community: string) { - const packages = await db.packages.where({community}).toArray(); + const packages = await db.packages.where({community}).sortBy('default_order'); return packages.map(ThunderstoreMod.parseFromThunderstoreData); }