Skip to content

Commit a443ad8

Browse files
committed
[Map] Re-organize methods order, simplify types, don't dirty-append @id anymore
1 parent 9219cdf commit a443ad8

File tree

9 files changed

+434
-399
lines changed

9 files changed

+434
-399
lines changed

src/Map/assets/dist/abstract_map_controller.d.ts

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@ export type Point = {
33
lat: number;
44
lng: number;
55
};
6-
export type MarkerDefinition<MarkerOptions, InfoWindowOptions> = {
7-
'@id': string;
6+
export type Identifier = string;
7+
export type WithIdentifier<T extends Record<string, unknown>> = T & {
8+
'@id': Identifier;
9+
};
10+
export type MarkerDefinition<MarkerOptions, InfoWindowOptions> = WithIdentifier<{
811
position: Point;
912
title: string | null;
10-
infoWindow?: Omit<InfoWindowDefinition<InfoWindowOptions>, 'position'>;
13+
infoWindow?: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
1114
rawOptions?: MarkerOptions;
1215
extra: Record<string, unknown>;
13-
};
14-
export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = {
15-
'@id': string;
16-
infoWindow?: Omit<InfoWindowDefinition<InfoWindowOptions>, 'position'>;
16+
}>;
17+
export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = WithIdentifier<{
18+
infoWindow?: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
1719
points: Array<Point>;
1820
title: string | null;
1921
rawOptions?: PolygonOptions;
2022
extra: Record<string, unknown>;
21-
};
22-
export type PolylineDefinition<PolylineOptions, InfoWindowOptions> = {
23-
'@id': string;
24-
infoWindow?: Omit<InfoWindowDefinition<InfoWindowOptions>, 'position'>;
23+
}>;
24+
export type PolylineDefinition<PolylineOptions, InfoWindowOptions> = WithIdentifier<{
25+
infoWindow?: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
2526
points: Array<Point>;
2627
title: string | null;
2728
rawOptions?: PolylineOptions;
2829
extra: Record<string, unknown>;
29-
};
30+
}>;
3031
export type InfoWindowDefinition<InfoWindowOptions> = {
3132
headerContent: string | null;
3233
content: string | null;
@@ -36,6 +37,7 @@ export type InfoWindowDefinition<InfoWindowOptions> = {
3637
rawOptions?: InfoWindowOptions;
3738
extra: Record<string, unknown>;
3839
};
40+
export type InfoWindowWithoutPositionDefinition<InfoWindowOptions> = Omit<InfoWindowDefinition<InfoWindowOptions>, 'position'>;
3941
export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindowOptions, InfoWindow, PolygonOptions, Polygon, PolylineOptions, Polyline> extends Controller<HTMLElement> {
4042
static values: {
4143
providerOptions: ObjectConstructor;
@@ -55,50 +57,39 @@ export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindow
5557
polylinesValue: Array<PolylineDefinition<PolylineOptions, InfoWindowOptions>>;
5658
optionsValue: MapOptions;
5759
protected map: Map;
58-
protected markers: globalThis.Map<any, any>;
60+
protected markers: globalThis.Map<string, Marker>;
61+
protected polygons: globalThis.Map<string, Polygon>;
62+
protected polylines: globalThis.Map<string, Polyline>;
5963
protected infoWindows: Array<InfoWindow>;
60-
protected polygons: globalThis.Map<any, any>;
61-
protected polylines: globalThis.Map<any, any>;
64+
private isConnected;
65+
protected abstract dispatchEvent(name: string, payload: Record<string, unknown>): void;
6266
connect(): void;
67+
createMarker(definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>): Marker;
68+
createPolygon(definition: PolygonDefinition<PolygonOptions, InfoWindowOptions>): Polygon;
69+
createPolyline(definition: PolylineDefinition<PolylineOptions, InfoWindowOptions>): Polyline;
70+
createInfoWindow({ definition, element, }: {
71+
definition: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
72+
element: Marker | Polygon | Polyline;
73+
}): InfoWindow;
74+
abstract centerValueChanged(): void;
75+
abstract zoomValueChanged(): void;
76+
markersValueChanged(): void;
77+
polygonsValueChanged(): void;
78+
polylinesValueChanged(): void;
6379
protected abstract doCreateMap({ center, zoom, options, }: {
6480
center: Point | null;
6581
zoom: number | null;
6682
options: MapOptions;
6783
}): Map;
68-
createMarker(definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>): Marker;
69-
protected abstract removeMarker(marker: Marker): void;
84+
protected abstract doFitBoundsToMarkers(): void;
7085
protected abstract doCreateMarker(definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>): Marker;
71-
createPolygon(definition: PolygonDefinition<PolygonOptions, InfoWindowOptions>): Polygon;
72-
protected abstract removePolygon(polygon: Polygon): void;
86+
protected abstract doRemoveMarker(marker: Marker): void;
7387
protected abstract doCreatePolygon(definition: PolygonDefinition<PolygonOptions, InfoWindowOptions>): Polygon;
74-
createPolyline(definition: PolylineDefinition<PolylineOptions, InfoWindowOptions>): Polyline;
75-
protected abstract removePolyline(polyline: Polyline): void;
88+
protected abstract doRemovePolygon(polygon: Polygon): void;
7689
protected abstract doCreatePolyline(definition: PolylineDefinition<PolylineOptions, InfoWindowOptions>): Polyline;
77-
protected createInfoWindow({ definition, element, }: {
78-
definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>['infoWindow'];
79-
element: Marker;
80-
} | {
81-
definition: PolygonDefinition<PolygonOptions, InfoWindowOptions>['infoWindow'];
82-
element: Polygon;
83-
} | {
84-
definition: PolylineDefinition<PolylineOptions, InfoWindowOptions>['infoWindow'];
85-
element: Polyline;
86-
}): InfoWindow;
90+
protected abstract doRemovePolyline(polyline: Polyline): void;
8791
protected abstract doCreateInfoWindow({ definition, element, }: {
88-
definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>['infoWindow'];
89-
element: Marker;
90-
} | {
91-
definition: PolygonDefinition<PolygonOptions, InfoWindowOptions>['infoWindow'];
92-
element: Polygon;
93-
} | {
94-
definition: PolylineDefinition<PolylineOptions, InfoWindowOptions>['infoWindow'];
95-
element: Polyline;
92+
definition: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
93+
element: Marker | Polygon | Polyline;
9694
}): InfoWindow;
97-
protected abstract doFitBoundsToMarkers(): void;
98-
protected abstract dispatchEvent(name: string, payload: Record<string, unknown>): void;
99-
abstract centerValueChanged(): void;
100-
abstract zoomValueChanged(): void;
101-
markersValueChanged(): void;
102-
polygonsValueChanged(): void;
103-
polylinesValueChanged(): void;
10495
}

src/Map/assets/dist/abstract_map_controller.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ class default_1 extends Controller {
44
constructor() {
55
super(...arguments);
66
this.markers = new Map();
7-
this.infoWindows = [];
87
this.polygons = new Map();
98
this.polylines = new Map();
9+
this.infoWindows = [];
10+
this.isConnected = false;
1011
}
1112
connect() {
1213
const options = this.optionsValue;
@@ -25,28 +26,26 @@ class default_1 extends Controller {
2526
polylines: [...this.polylines.values()],
2627
infoWindows: this.infoWindows,
2728
});
29+
this.isConnected = true;
2830
}
2931
createMarker(definition) {
3032
this.dispatchEvent('marker:before-create', { definition });
3133
const marker = this.doCreateMarker(definition);
3234
this.dispatchEvent('marker:after-create', { marker });
33-
marker['@id'] = definition['@id'];
3435
this.markers.set(definition['@id'], marker);
3536
return marker;
3637
}
3738
createPolygon(definition) {
3839
this.dispatchEvent('polygon:before-create', { definition });
3940
const polygon = this.doCreatePolygon(definition);
4041
this.dispatchEvent('polygon:after-create', { polygon });
41-
polygon['@id'] = definition['@id'];
4242
this.polygons.set(definition['@id'], polygon);
4343
return polygon;
4444
}
4545
createPolyline(definition) {
4646
this.dispatchEvent('polyline:before-create', { definition });
4747
const polyline = this.doCreatePolyline(definition);
4848
this.dispatchEvent('polyline:after-create', { polyline });
49-
polyline['@id'] = definition['@id'];
5049
this.polylines.set(definition['@id'], polyline);
5150
return polyline;
5251
}
@@ -58,53 +57,62 @@ class default_1 extends Controller {
5857
return infoWindow;
5958
}
6059
markersValueChanged() {
61-
if (!this.map) {
60+
if (!this.isConnected) {
6261
return;
6362
}
64-
this.markers.forEach((marker) => {
65-
if (!this.markersValue.find((m) => m['@id'] === marker['@id'])) {
66-
this.removeMarker(marker);
67-
this.markers.delete(marker['@id']);
68-
}
63+
const idsToRemove = new Set(this.markers.keys());
64+
this.markersValue.forEach((definition) => {
65+
idsToRemove.delete(definition['@id']);
66+
});
67+
idsToRemove.forEach((id) => {
68+
const marker = this.markers.get(id);
69+
this.doRemoveMarker(marker);
70+
this.markers.delete(id);
6971
});
70-
this.markersValue.forEach((marker) => {
71-
if (!this.markers.has(marker['@id'])) {
72-
this.createMarker(marker);
72+
this.markersValue.forEach((definition) => {
73+
if (!this.markers.has(definition['@id'])) {
74+
this.createMarker(definition);
7375
}
7476
});
7577
if (this.fitBoundsToMarkersValue) {
7678
this.doFitBoundsToMarkers();
7779
}
7880
}
7981
polygonsValueChanged() {
80-
if (!this.map) {
82+
if (!this.isConnected) {
8183
return;
8284
}
83-
this.polygons.forEach((polygon) => {
84-
if (!this.polygonsValue.find((p) => p['@id'] === polygon['@id'])) {
85-
this.removePolygon(polygon);
86-
this.polygons.delete(polygon['@id']);
87-
}
85+
const idsToRemove = new Set(this.polygons.keys());
86+
this.polygonsValue.forEach((definition) => {
87+
idsToRemove.delete(definition['@id']);
88+
});
89+
idsToRemove.forEach((id) => {
90+
const polygon = this.polygons.get(id);
91+
this.doRemovePolygon(polygon);
92+
this.polygons.delete(id);
8893
});
89-
this.polygonsValue.forEach((polygon) => {
90-
if (!this.polygons.has(polygon['@id'])) {
91-
this.createPolygon(polygon);
94+
this.polygonsValue.forEach((definition) => {
95+
if (!this.polygons.has(definition['@id'])) {
96+
this.createPolygon(definition);
9297
}
9398
});
9499
}
95100
polylinesValueChanged() {
96-
if (!this.map) {
101+
if (!this.isConnected) {
97102
return;
98103
}
99-
this.polylines.forEach((polyline) => {
100-
if (!this.polylinesValue.find((p) => p['@id'] === polyline['@id'])) {
101-
this.removePolyline(polyline);
102-
this.polylines.delete(polyline['@id']);
103-
}
104+
const idsToRemove = new Set(this.polylines.keys());
105+
this.polylinesValue.forEach((definition) => {
106+
idsToRemove.delete(definition['@id']);
107+
});
108+
idsToRemove.forEach((id) => {
109+
const polyline = this.polylines.get(id);
110+
this.doRemovePolyline(polyline);
111+
this.polylines.delete(id);
104112
});
105-
this.polylinesValue.forEach((polyline) => {
106-
if (!this.polylines.has(polyline['@id'])) {
107-
this.createPolyline(polyline);
113+
this.polylinesValue.forEach((definition) => {
114+
if (!this.polylines.has(definition['@id'])) {
115+
this.createPolyline(definition);
108116
}
109117
});
110118
}

0 commit comments

Comments
 (0)