Skip to content

Commit

Permalink
Fix default ordering on OnlineModView
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anttimaki committed Apr 19, 2024
1 parent f22547b commit 5c87aa4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/r2mm/manager/PackageDexieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 5c87aa4

Please sign in to comment.