Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunked IndexedDB puts #1317

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/r2mm/manager/PackageDexieStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Dexie, { Table } from 'dexie';

import ThunderstoreMod from '../../model/ThunderstoreMod';
import * as ArrayUtils from '../../utils/ArrayUtils';

interface DexieVersion {
full_name: string;
Expand Down Expand Up @@ -57,11 +58,14 @@ 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, i) => ({
...pkg,
...extra,
default_order: i
}));
const newPackageChunks: DexiePackage[][] = ArrayUtils.chunk(
packages.map((pkg, i) => ({
...pkg,
...extra,
default_order: i
})),
5000
);

await db.transaction(
'rw',
Expand All @@ -76,7 +80,9 @@ export async function updateFromApiResponse(community: string, packages: any[])
// for big lists since the small ones are fast enough anyway, and
// this approach also works better with the plans to use pagination
// when fetching the package list in the future.
await db.packages.bulkPut(newPackages);
await Promise.all(
newPackageChunks.map((chunk) => db.packages.bulkPut(chunk))
);

// Find packages that were no longer returned by the API and delete them.
// .bulkDelete is faster than calling .delete() on the Collection
Expand Down
Loading