From 76b41dc546019469987ab02c5d6286216aa245ed Mon Sep 17 00:00:00 2001 From: vcoppe Date: Thu, 4 Jul 2024 16:25:13 +0200 Subject: [PATCH] finish reworking immer updates --- gpx/src/gpx.ts | 78 +++++++++---------- .../src/lib/components/file-list/FileList.ts | 50 ++++++------ .../file-list/FileListNodeContent.svelte | 6 +- .../file-list/FileListNodeLabel.svelte | 7 +- .../toolbar/tools/routing/Routing.svelte | 1 - .../toolbar/tools/routing/RoutingControls.ts | 2 +- website/src/lib/db.ts | 68 +++++++--------- 7 files changed, 96 insertions(+), 116 deletions(-) diff --git a/gpx/src/gpx.ts b/gpx/src/gpx.ts index 04cbde56..3eb15ef7 100644 --- a/gpx/src/gpx.ts +++ b/gpx/src/gpx.ts @@ -1,6 +1,6 @@ import { ramerDouglasPeucker } from "./simplify"; import { Coordinates, GPXFileAttributes, GPXFileType, LineStyleExtension, Link, Metadata, TrackExtensions, TrackPointExtensions, TrackPointType, TrackSegmentType, TrackType, WaypointType } from "./types"; -import { Draft, immerable, isDraft, original, produce, freeze } from "immer"; +import { immerable, isDraft, original, freeze } from "immer"; function cloneJSON(obj: T): T { if (obj === null || typeof obj !== 'object') { @@ -209,23 +209,23 @@ export class GPXFile extends GPXTreeNode{ } // Producers - replaceTracks(start: number, end: number, tracks: Track[]): Track[] { + replaceTracks(start: number, end: number, tracks: Track[]) { if (this._data.style) { tracks.forEach((track) => track.setStyle(this._data.style, false)); } - return this.trk.splice(start, end - start + 1, ...tracks); + this.trk.splice(start, end - start + 1, ...tracks); } - replaceTrackSegments(trackIndex: number, start: number, end: number, segments: TrackSegment[]): TrackSegment[] { - return this.trk[trackIndex].replaceTrackSegments(start, end, segments); + replaceTrackSegments(trackIndex: number, start: number, end: number, segments: TrackSegment[]) { + this.trk[trackIndex].replaceTrackSegments(start, end, segments); } replaceTrackPoints(trackIndex: number, segmentIndex: number, start: number, end: number, points: TrackPoint[], speed?: number, startTime?: Date) { this.trk[trackIndex].replaceTrackPoints(segmentIndex, start, end, points, speed, startTime); } - replaceWaypoints(start: number, end: number, waypoints: Waypoint[]): Waypoint[] { - return this.wpt.splice(start, end - start + 1, ...waypoints); + replaceWaypoints(start: number, end: number, waypoints: Waypoint[]) { + this.wpt.splice(start, end - start + 1, ...waypoints); } reverse() { @@ -347,28 +347,22 @@ export class GPXFile extends GPXTreeNode{ } setHiddenWaypoints(hidden: boolean, waypointIndices?: number[]) { - return produce(this, (draft) => { - let og = getOriginal(draft); // Read as much as possible from the original object because it is faster - - let allHiddenWpt = hidden; - let wpt = og.wpt.map((waypoint, index) => { - if (waypointIndices === undefined || waypointIndices.includes(index)) { - return waypoint.setHidden(hidden); - } else { - allHiddenWpt = allHiddenWpt && (waypoint._data.hidden === true); - return waypoint; - } - }); - draft.wpt = freeze(wpt); // Pre-freeze the array, faster as well - - let allHiddenTrk = true; - og.trk.forEach((track) => { - allHiddenTrk = allHiddenTrk && (track._data.hidden === true); - }); - - draft._data.hiddenWpt = allHiddenWpt; - draft._data.hidden = allHiddenTrk && allHiddenWpt; + let allHiddenWpt = hidden; + this.wpt.forEach((waypoint, index) => { + if (waypointIndices === undefined || waypointIndices.includes(index)) { + waypoint.setHidden(hidden); + } else { + allHiddenWpt = allHiddenWpt && (waypoint._data.hidden === true); + } }); + + let allHiddenTrk = true; + this.trk.forEach((track) => { + allHiddenTrk = allHiddenTrk && (track._data.hidden === true); + }); + + this._data.hiddenWpt = allHiddenWpt; + this._data.hidden = allHiddenTrk && allHiddenWpt; } }; @@ -458,8 +452,8 @@ export class Track extends GPXTreeNode { } // Producers - replaceTrackSegments(start: number, end: number, segments: TrackSegment[]): TrackSegment[] { - return this.trkseg.splice(start, end - start + 1, ...segments); + replaceTrackSegments(start: number, end: number, segments: TrackSegment[]) { + this.trkseg.splice(start, end - start + 1, ...segments); } replaceTrackPoints(segmentIndex: number, start: number, end: number, points: TrackPoint[], speed?: number, startTime?: Date) { @@ -494,23 +488,21 @@ export class Track extends GPXTreeNode { } clean(bounds: [Coordinates, Coordinates], inside: boolean, segmentIndices?: number[]) { - return produce(this, (draft) => { - let i = 0; - let segmentIndex = 0; - while (i < this.trkseg.length) { - if (segmentIndices === undefined || segmentIndices.includes(segmentIndex)) { - this.trkseg[i].clean(bounds, inside); - if (this.trkseg[i].getNumberOfTrackPoints() === 0) { - this.trkseg.splice(i, 1); - } else { - i++; - } + let i = 0; + let segmentIndex = 0; + while (i < this.trkseg.length) { + if (segmentIndices === undefined || segmentIndices.includes(segmentIndex)) { + this.trkseg[i].clean(bounds, inside); + if (this.trkseg[i].getNumberOfTrackPoints() === 0) { + this.trkseg.splice(i, 1); } else { i++; } - segmentIndex++; + } else { + i++; } - }); + segmentIndex++; + } } changeTimestamps(startTime: Date, speed: number, ratio: number, lastPoint?: TrackPoint, segmentIndex?: number) { diff --git a/website/src/lib/components/file-list/FileList.ts b/website/src/lib/components/file-list/FileList.ts index 61929df6..36d53d89 100644 --- a/website/src/lib/components/file-list/FileList.ts +++ b/website/src/lib/components/file-list/FileList.ts @@ -327,27 +327,29 @@ export function moveItems(fromParent: ListItem, toParent: ListItem, fromItems: L return; } - sortItems(fromItems, remove && !(fromParent instanceof ListRootItem)); + sortItems(fromItems, false); sortItems(toItems, false); let context: (GPXFile | Track | TrackSegment | Waypoint[] | Waypoint)[] = []; - if (!remove || fromParent instanceof ListRootItem) { - fromItems.forEach((item) => { - let file = getFile(item.getFileId()); - if (file) { - if (item instanceof ListFileItem) { - context.push(file.clone()); - } else if (item instanceof ListTrackItem && item.getTrackIndex() < file.trk.length) { - context.push(file.trk[item.getTrackIndex()].clone()); - } else if (item instanceof ListTrackSegmentItem && item.getTrackIndex() < file.trk.length && item.getSegmentIndex() < file.trk[item.getTrackIndex()].trkseg.length) { - context.push(file.trk[item.getTrackIndex()].trkseg[item.getSegmentIndex()].clone()); - } else if (item instanceof ListWaypointsItem) { - context.push(file.wpt.map((wpt) => wpt.clone())); - } else if (item instanceof ListWaypointItem && item.getWaypointIndex() < file.wpt.length) { - context.push(file.wpt[item.getWaypointIndex()].clone()); - } + fromItems.forEach((item) => { + let file = getFile(item.getFileId()); + if (file) { + if (item instanceof ListFileItem) { + context.push(file.clone()); + } else if (item instanceof ListTrackItem && item.getTrackIndex() < file.trk.length) { + context.push(file.trk[item.getTrackIndex()].clone()); + } else if (item instanceof ListTrackSegmentItem && item.getTrackIndex() < file.trk.length && item.getSegmentIndex() < file.trk[item.getTrackIndex()].trkseg.length) { + context.push(file.trk[item.getTrackIndex()].trkseg[item.getSegmentIndex()].clone()); + } else if (item instanceof ListWaypointsItem) { + context.push(file.wpt.map((wpt) => wpt.clone())); + } else if (item instanceof ListWaypointItem && item.getWaypointIndex() < file.wpt.length) { + context.push(file.wpt[item.getWaypointIndex()].clone()); } - }); + } + }); + + if (remove && !(fromParent instanceof ListRootItem)) { + sortItems(fromItems, true); } let files = [fromParent.getFileId(), toParent.getFileId()]; @@ -355,16 +357,15 @@ export function moveItems(fromParent: ListItem, toParent: ListItem, fromItems: L (file, context: (GPXFile | Track | TrackSegment | Waypoint[] | Waypoint)[]) => { fromItems.forEach((item) => { if (item instanceof ListTrackItem) { - context.push(...file.replaceTracks(item.getTrackIndex(), item.getTrackIndex(), [])); + file.replaceTracks(item.getTrackIndex(), item.getTrackIndex(), []); } else if (item instanceof ListTrackSegmentItem) { - context.push(...file.replaceTrackSegments(item.getTrackIndex(), item.getSegmentIndex(), item.getSegmentIndex(), [])); + file.replaceTrackSegments(item.getTrackIndex(), item.getSegmentIndex(), item.getSegmentIndex(), []); } else if (item instanceof ListWaypointsItem) { - context.push(file.replaceWaypoints(0, newFile.wpt.length - 1, [])); + file.replaceWaypoints(0, file.wpt.length - 1, []); } else if (item instanceof ListWaypointItem) { - context.push(...file.replaceWaypoints(item.getWaypointIndex(), item.getWaypointIndex(), [])); + file.replaceWaypoints(item.getWaypointIndex(), item.getWaypointIndex(), []); } }); - context.reverse(); }, (file, context: (GPXFile | Track | TrackSegment | Waypoint[] | Waypoint)[]) => { toItems.forEach((item, i) => { @@ -415,15 +416,14 @@ export function moveItems(fromParent: ListItem, toParent: ListItem, fromItems: L if (context[i].name) { newFile.metadata.name = context[i].name; } - console.log(context[i]); - newFile.replaceTracks(0, 0, [context[i]])[0]; + newFile.replaceTracks(0, 0, [context[i]]); files.set(item.getFileId(), freeze(newFile)); } else if (context[i] instanceof TrackSegment) { let newFile = newGPXFile(); newFile._data.id = item.getFileId(); newFile.replaceTracks(0, 0, [new Track({ trkseg: [context[i]] - })])[0]; + })]); files.set(item.getFileId(), freeze(newFile)); } } diff --git a/website/src/lib/components/file-list/FileListNodeContent.svelte b/website/src/lib/components/file-list/FileListNodeContent.svelte index 7515b782..3be75eab 100644 --- a/website/src/lib/components/file-list/FileListNodeContent.svelte +++ b/website/src/lib/components/file-list/FileListNodeContent.svelte @@ -190,8 +190,9 @@ if (Sortable.get(e.from)._waypointRoot) { fromItems = [fromItem.extend('waypoints')]; } else { - let oldIndices = + let oldIndices: number[] = e.oldIndicies.length > 0 ? e.oldIndicies.map((i) => i.index) : [e.oldIndex]; + oldIndices = oldIndices.filter((i) => i >= 0); oldIndices.sort((a, b) => a - b); fromItems = oldIndices.map((i) => fromItem.extend(i)); @@ -204,8 +205,9 @@ toItem = toItem.extend('waypoints'); } - let newIndices = + let newIndices: number[] = e.newIndicies.length > 0 ? e.newIndicies.map((i) => i.index) : [e.newIndex]; + newIndices = newIndices.filter((i) => i >= 0); newIndices.sort((a, b) => a - b); if (toItem instanceof ListRootItem) { diff --git a/website/src/lib/components/file-list/FileListNodeLabel.svelte b/website/src/lib/components/file-list/FileListNodeLabel.svelte index 611227d3..1b4af97b 100644 --- a/website/src/lib/components/file-list/FileListNodeLabel.svelte +++ b/website/src/lib/components/file-list/FileListNodeLabel.svelte @@ -103,6 +103,7 @@ $editStyle && $selection.has(item) && $selection.getSelected().findIndex((i) => i.getFullId() === item.getFullId()) === 0; + $: hidden = item.level === ListLevel.WAYPOINTS ? node._data.hiddenWpt : node._data.hidden; @@ -142,7 +143,9 @@ /> {/if} { if (e.ctrlKey) { // Add to selection instead of opening context menu @@ -181,7 +184,7 @@ {label} - {#if (item.level !== ListLevel.WAYPOINTS && node._data.hidden) || (item.level === ListLevel.WAYPOINTS && node._data.hiddenWpt)} + {#if hidden}