Skip to content

Commit

Permalink
Optimize updating markers
Browse files Browse the repository at this point in the history
  • Loading branch information
flopp committed Jan 4, 2023
1 parent dce0929 commit 346e5fb
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 138 deletions.
4 changes: 4 additions & 0 deletions src/components/coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class Coordinates {
this._raw_lng = lng;
}

public equals(other: Coordinates): boolean {
return this._raw_lat == other._raw_lat && this._raw_lng == other._raw_lng;
}

public raw_lat(): number {
return this._raw_lat;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Language extends MapStateObserver {
});
}

public update_state(changes: number): void {
public update_state(changes: number, _marker_id: number = -1): void {
if ((changes & MapStateChange.LANGUAGE) === MapStateChange.NOTHING) {
return;
}
Expand Down
71 changes: 46 additions & 25 deletions src/components/leaflet_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class LeafletWrapper extends MapStateObserver {
this.map.invalidateSize();
}

public update_state(changes: number): void {
public update_state(changes: number, marker_id: number = -1): void {
/* update view */
if ((changes & MapStateChange.MAPTYPE) !== 0) {
this.set_map_type(this.app.map_state.map_type!);
Expand All @@ -261,36 +261,57 @@ export class LeafletWrapper extends MapStateObserver {
}

if ((changes & MapStateChange.MARKERS) !== 0) {
// Update and add markers
this.app.map_state.markers.forEach((marker: Marker): void => {
if (this.markers.has(marker.get_id())) {
this.update_marker_object(this.markers.get(marker.get_id())!, marker);
if (marker_id !== -1) {
const marker = this.app.map_state.get_marker(marker_id);
const marker_obj = this.markers.get(marker_id);
if (marker === null) {
// Deleted
if (marker_obj !== undefined) {
this.delete_marker_object(marker_obj);
}
this.markers.delete(marker_id);
} else {
this.create_marker_object(marker);
if (marker_obj !== undefined) {
// Added
this.update_marker_object(marker_obj, marker);
} else {
// Changed
this.create_marker_object(marker);
}
}
});

/* remove spurious markers */
if (this.markers.size > this.app.map_state.markers.length) {
const ids = new Set();
} else {
// Update and add markers
this.app.map_state.markers.forEach((marker: Marker): void => {
ids.add(marker.get_id());
});

const deleted_ids: number[] = [];
this.markers.forEach((_marker: any, id: number, _map: any): void => {
if (!ids.has(id)) {
deleted_ids.push(id);
const marker_obj = this.markers.get(marker.get_id());
if (marker_obj !== undefined) {
this.update_marker_object(marker_obj, marker);
} else {
this.create_marker_object(marker);
}
});

deleted_ids.forEach((id: number): void => {
const marker = this.markers.get(id);
if (marker !== undefined) {
this.delete_marker_object(marker);
}
this.markers.delete(id);
});
/* remove spurious markers */
if (this.markers.size > this.app.map_state.markers.length) {
const ids = new Set();
this.app.map_state.markers.forEach((marker: Marker): void => {
ids.add(marker.get_id());
});

const deleted_ids: number[] = [];
this.markers.forEach((_marker: any, id: number, _map: any): void => {
if (!ids.has(id)) {
deleted_ids.push(id);
}
});

deleted_ids.forEach((id: number): void => {
const marker = this.markers.get(id);
if (marker !== undefined) {
this.delete_marker_object(marker);
}
this.markers.delete(id);
});
}
}
}

Expand Down
51 changes: 32 additions & 19 deletions src/components/map_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,18 +451,15 @@ export class MapState {
this.observers.push(observer);
}

public update_observers(changes: number): void {
if (changes === MapStateChange.NOTHING) {
console.error("MapSate.update_observers called with no changes");
}
public update_observers(changes: number, marker_id: number = -1): void {
let updatedChanges = changes;
if ((changes & (MapStateChange.MARKERS | MapStateChange.LINES)) !== 0) {
if (this.recompute_lines()) {
updatedChanges = changes | MapStateChange.LINES;
}
}
this.observers.forEach((observer: MapStateObserver): void => {
observer.update_state(updatedChanges);
observer.update_state(updatedChanges, marker_id);
});
}

Expand Down Expand Up @@ -577,7 +574,7 @@ export class MapState {
this.markers_hash.set(marker.get_id(), marker);
this.update_marker_storage(marker);
this.storage.set("markers", this.get_marker_ids_string());
this.update_observers(MapStateChange.MARKERS);
this.update_observers(MapStateChange.MARKERS, marker.get_id());

this.app.message(this.app.translate("messages.marker_created"));

Expand All @@ -599,7 +596,7 @@ export class MapState {
);
this.markers_hash.delete(id);
this.storage.set("markers", this.get_marker_ids_string());
this.update_observers(MapStateChange.MARKERS);
this.update_observers(MapStateChange.MARKERS, id);
}

public delete_all_markers(): void {
Expand All @@ -616,9 +613,13 @@ export class MapState {

return;
}
this.markers_hash.get(id)!.coordinates = coordinates;
this.storage.set_coordinates(`marker[${id}].coordinates`, coordinates);
this.update_observers(MapStateChange.MARKERS);

const marker = this.markers_hash.get(id)!;
if (!coordinates.equals(marker.coordinates)) {
marker.coordinates = coordinates;
this.storage.set_coordinates(`marker[${id}].coordinates`, coordinates);
this.update_observers(MapStateChange.MARKERS, id);
}
}

public set_marker_name(id: number, name: string): void {
Expand All @@ -627,9 +628,13 @@ export class MapState {

return;
}
this.markers_hash.get(id)!.name = name;
this.storage.set(`marker[${id}].name`, name);
this.update_observers(MapStateChange.MARKERS);

const marker = this.markers_hash.get(id)!;
if (name !== marker.name) {
marker.name = name;
this.storage.set(`marker[${id}].name`, name);
this.update_observers(MapStateChange.MARKERS, id);
}
}

public set_marker_color(id: number, color: Color): void {
Expand All @@ -638,9 +643,13 @@ export class MapState {

return;
}
this.markers_hash.get(id)!.color = color;
this.storage.set_color(`marker[${id}].color`, color);
this.update_observers(MapStateChange.MARKERS);

const marker = this.markers_hash.get(id)!;
if (!color.equals(marker.color)) {
marker.color = color;
this.storage.set_color(`marker[${id}].color`, color);
this.update_observers(MapStateChange.MARKERS, id);
}
}

public set_marker_radius(id: number, radius: number): void {
Expand All @@ -649,9 +658,13 @@ export class MapState {

return;
}
this.markers_hash.get(id)!.radius = radius;
this.storage.set_float(`marker[${id}].radius`, radius);
this.update_observers(MapStateChange.MARKERS);

const marker = this.markers_hash.get(id)!;
if (radius !== marker.radius) {
marker.radius = radius;
this.storage.set_float(`marker[${id}].radius`, radius);
this.update_observers(MapStateChange.MARKERS, id);
}
}

public update_marker_storage(marker: Marker): void {
Expand Down
2 changes: 1 addition & 1 deletion src/components/map_state_observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MapStateObserver {
app.map_state.register_observer(this);
}

public update_state(_changes: number): void {
public update_state(_changes: number, _marker_id: number = -1): void {
throw new Error("not implemented");
}
}
2 changes: 1 addition & 1 deletion src/components/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Sidebar extends MapStateObserver {
}
}

public update_state(changes: number): void {
public update_state(changes: number, _marker_id: number = -1): void {
if ((changes & MapStateChange.SIDEBAR) === MapStateChange.NOTHING) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/sidebar_hero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class SidebarHero extends SidebarItem {
super(app, id);
}

public update_state(_changes: number): void {
public update_state(_changes: number, _marker_id: number = -1): void {
// Nothing
}
}
2 changes: 1 addition & 1 deletion src/components/sidebar_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class SidebarInfo extends SidebarItem {
});
}

public update_state(_changes: number): void {
public update_state(_changes: number, _marker_id: number = -1): void {
// Nothing
}
}
2 changes: 1 addition & 1 deletion src/components/sidebar_layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class SidebarLayers extends SidebarItem {
};
}

public update_state(changes: number): void {
public update_state(changes: number, _marker_id: number = -1): void {
if ((changes & MapStateChange.MAPTYPE) === MapStateChange.NOTHING) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/sidebar_lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class SidebarLines extends SidebarItem {
});
}

public update_state(changes: number): void {
public update_state(changes: number, _marker_id: number = -1): void {
if ((changes & (MapStateChange.LINES | MapStateChange.LANGUAGE | MapStateChange.MARKERS)) === 0) {
return;
}
Expand Down
Loading

0 comments on commit 346e5fb

Please sign in to comment.