mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2025-09-03 09:12:30 +00:00
fix stats update logic
This commit is contained in:
@@ -28,32 +28,47 @@ fileObservers.subscribe((files) => { // Update selectedFiles automatically when
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const fileStatistics: Map<string, GPXStatistics> = new Map();
|
const fileStatistics: Map<string, Writable<GPXStatistics>> = new Map();
|
||||||
|
const fileUnsubscribe: Map<string, Function> = new Map();
|
||||||
export const gpxStatistics: Writable<GPXStatistics> = writable(new GPXStatistics());
|
export const gpxStatistics: Writable<GPXStatistics> = writable(new GPXStatistics());
|
||||||
|
|
||||||
function updateGPXData() {
|
function updateGPXData() {
|
||||||
let fileIds: string[] = get(fileOrder).filter((f) => fileStatistics.has(f) && get(selectedFiles).has(f));
|
let fileIds: string[] = get(fileOrder).filter((f) => fileStatistics.has(f) && get(selectedFiles).has(f));
|
||||||
gpxStatistics.set(fileIds.reduce((stats: GPXStatistics, fileId: string) => {
|
gpxStatistics.set(fileIds.reduce((stats: GPXStatistics, fileId: string) => {
|
||||||
stats.mergeWith(fileStatistics.get(fileId) ?? new GPXStatistics());
|
let statisticsStore = fileStatistics.get(fileId);
|
||||||
|
if (statisticsStore) {
|
||||||
|
stats.mergeWith(get(statisticsStore));
|
||||||
|
}
|
||||||
return stats;
|
return stats;
|
||||||
}, new GPXStatistics()));
|
}, new GPXStatistics()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let selectedFilesUnsubscribe: Function[] = [];
|
fileObservers.subscribe((files) => { // Maintain up-to-date statistics
|
||||||
selectedFiles.subscribe((selectedFiles) => {
|
fileStatistics.forEach((stats, fileId) => {
|
||||||
selectedFilesUnsubscribe.forEach((unsubscribe) => unsubscribe());
|
if (!files.has(fileId)) {
|
||||||
selectedFiles.forEach((fileId) => {
|
fileStatistics.delete(fileId);
|
||||||
let fileObserver = get(fileObservers).get(fileId);
|
let unsubscribe = fileUnsubscribe.get(fileId);
|
||||||
if (fileObserver) {
|
if (unsubscribe) unsubscribe();
|
||||||
let unsubscribe = fileObserver.subscribe((file) => {
|
fileUnsubscribe.delete(fileId);
|
||||||
if (file) {
|
|
||||||
fileStatistics.set(fileId, file.getStatistics());
|
|
||||||
updateGPXData();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
selectedFilesUnsubscribe.push(unsubscribe);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
files.forEach((fileObserver, fileId) => {
|
||||||
|
if (!fileStatistics.has(fileId)) {
|
||||||
|
fileStatistics.set(fileId, writable(new GPXStatistics()));
|
||||||
|
let unsubscribe = fileObserver.subscribe((file) => {
|
||||||
|
if (file) {
|
||||||
|
fileStatistics.get(fileId)?.set(file.getStatistics());
|
||||||
|
if (get(selectedFiles).has(fileId)) {
|
||||||
|
updateGPXData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
fileUnsubscribe.set(fileId, unsubscribe);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
selectedFiles.subscribe((selectedFiles) => { // Maintain up-to-date statistics for the current selection
|
||||||
updateGPXData();
|
updateGPXData();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user