From d93e43e2686813a7aeca40b367bd0351f8b1166d Mon Sep 17 00:00:00 2001 From: vcoppe Date: Wed, 15 May 2024 11:47:42 +0200 Subject: [PATCH] group filestate updates --- website/src/lib/db.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/website/src/lib/db.ts b/website/src/lib/db.ts index 7a4bdb34..314256ee 100644 --- a/website/src/lib/db.ts +++ b/website/src/lib/db.ts @@ -110,16 +110,16 @@ function dexieStore(querier: () => T | Promise, initial?: T): Readable export type GPXFileWithStatistics = { file: GPXFile, statistics: GPXStatistics }; // Wrap Dexie live queries in a Svelte store to avoid triggering the query for every subscriber, also takes care of the conversion to a GPXFile object -function dexieGPXFileStore(querier: () => GPXFile | undefined | Promise): Readable & { destroy: () => void } { +function dexieGPXFileStore(id: string): Readable & { destroy: () => void } { let store = writable(undefined); - let query = liveQuery(querier).subscribe(value => { + let query = liveQuery(() => db.files.get(id)).subscribe(value => { if (value !== undefined) { let gpx = new GPXFile(value); let statistics = gpx.getStatistics(); - if (!fileState.has(gpx._data.id)) { // Update the map bounds for new files + if (!fileState.has(id)) { // Update the map bounds for new files updateTargetMapBounds(statistics.global.bounds); } - fileState.set(gpx._data.id, gpx); + fileState.set(id, gpx); store.set({ file: gpx, statistics @@ -128,7 +128,10 @@ function dexieGPXFileStore(querier: () => GPXFile | undefined | Promise { + fileState.delete(id); + query.unsubscribe(); + } }; } @@ -183,12 +186,11 @@ liveQuery(() => db.fileids.toArray()).subscribe(dbFileIds => { if (newFiles.length > 0 || deletedFiles.length > 0) { fileObservers.update($files => { newFiles.forEach(id => { - $files.set(id, dexieGPXFileStore(() => db.files.get(id))); + $files.set(id, dexieGPXFileStore(id)); }); deletedFiles.forEach(id => { $files.get(id)?.destroy(); $files.delete(id); - fileState.delete(id); }); return $files; }); @@ -260,7 +262,7 @@ function applyPatch(patch: Patch[]) { // Get the file ids of the files that have changed in the patch function getChangedFileIds(patch: Patch[]): string[] { - let changedFileIds = new Set(); + let changedFileIds = new Set(); for (let p of patch) { changedFileIds.add(p.path[0]); }