Skip to content

Commit 1d33a5f

Browse files
committed
[Map] Create "onDrawChanged" to refactor methods "markersValueChanged"/"polygonsValueChanged"/"polylinesValueChanged" (not identical but follow the same pattern)
1 parent d9d9ff9 commit 1d33a5f

File tree

5 files changed

+116
-184
lines changed

5 files changed

+116
-184
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,5 @@ export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindow
9999
element: Marker | Polygon | Polyline;
100100
}): InfoWindow;
101101
private createDrawingFactory;
102+
private onDrawChanged;
102103
}

src/Map/assets/dist/abstract_map_controller.js

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,7 @@ class default_1 extends Controller {
4242
if (!this.isConnected) {
4343
return;
4444
}
45-
const idsToRemove = new Set(this.markers.keys());
46-
this.markersValue.forEach((definition) => {
47-
idsToRemove.delete(definition['@id']);
48-
});
49-
idsToRemove.forEach((id) => {
50-
const marker = this.markers.get(id);
51-
this.doRemoveMarker(marker);
52-
this.markers.delete(id);
53-
});
54-
this.markersValue.forEach((definition) => {
55-
if (!this.markers.has(definition['@id'])) {
56-
this.createMarker({ definition });
57-
}
58-
});
45+
this.onDrawChanged(this.markers, this.markersValue, this.createMarker, this.doRemoveMarker);
5946
if (this.fitBoundsToMarkersValue) {
6047
this.doFitBoundsToMarkers();
6148
}
@@ -64,51 +51,41 @@ class default_1 extends Controller {
6451
if (!this.isConnected) {
6552
return;
6653
}
67-
const idsToRemove = new Set(this.polygons.keys());
68-
this.polygonsValue.forEach((definition) => {
69-
idsToRemove.delete(definition['@id']);
70-
});
71-
idsToRemove.forEach((id) => {
72-
const polygon = this.polygons.get(id);
73-
this.doRemovePolygon(polygon);
74-
this.polygons.delete(id);
75-
});
76-
this.polygonsValue.forEach((definition) => {
77-
if (!this.polygons.has(definition['@id'])) {
78-
this.createPolygon({ definition });
79-
}
80-
});
54+
this.onDrawChanged(this.polygons, this.polygonsValue, this.createPolygon, this.doRemovePolygon);
8155
}
8256
polylinesValueChanged() {
8357
if (!this.isConnected) {
8458
return;
8559
}
86-
const idsToRemove = new Set(this.polylines.keys());
87-
this.polylinesValue.forEach((definition) => {
88-
idsToRemove.delete(definition['@id']);
89-
});
90-
idsToRemove.forEach((id) => {
91-
const polyline = this.polylines.get(id);
92-
this.doRemovePolyline(polyline);
93-
this.polylines.delete(id);
94-
});
95-
this.polylinesValue.forEach((definition) => {
96-
if (!this.polylines.has(definition['@id'])) {
97-
this.createPolyline({ definition });
98-
}
99-
});
60+
this.onDrawChanged(this.polylines, this.polylinesValue, this.createPolyline, this.doRemovePolyline);
10061
}
10162
createDrawingFactory(type, draws, factory) {
10263
const eventBefore = `${type}:before-create`;
10364
const eventAfter = `${type}:after-create`;
10465
return ({ definition }) => {
10566
this.dispatchEvent(eventBefore, { definition });
106-
const drawing = factory(definition);
67+
const drawing = factory({ definition });
10768
this.dispatchEvent(eventAfter, { [type]: drawing });
10869
draws.set(definition['@id'], drawing);
10970
return drawing;
11071
};
11172
}
73+
onDrawChanged(draws, newDrawDefinitions, factory, remover) {
74+
const idsToRemove = new Set(draws.keys());
75+
newDrawDefinitions.forEach((definition) => {
76+
idsToRemove.delete(definition['@id']);
77+
});
78+
idsToRemove.forEach((id) => {
79+
const draw = draws.get(id);
80+
remover(draw);
81+
draws.delete(id);
82+
});
83+
newDrawDefinitions.forEach((definition) => {
84+
if (!draws.has(definition['@id'])) {
85+
factory({ definition });
86+
}
87+
});
88+
}
11289
}
11390
default_1.values = {
11491
providerOptions: Object,

src/Map/assets/src/abstract_map_controller.ts

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export default abstract class<
176176

177177
return infoWindow;
178178
}
179+
179180
//endregion
180181

181182
//region Hooks called by Stimulus when the values change
@@ -188,23 +189,7 @@ export default abstract class<
188189
return;
189190
}
190191

191-
const idsToRemove = new Set(this.markers.keys());
192-
this.markersValue.forEach((definition) => {
193-
idsToRemove.delete(definition['@id']);
194-
});
195-
196-
idsToRemove.forEach((id) => {
197-
// biome-ignore lint/style/noNonNullAssertion: the ids are coming from the keys of the map
198-
const marker = this.markers.get(id)!;
199-
this.doRemoveMarker(marker);
200-
this.markers.delete(id);
201-
});
202-
203-
this.markersValue.forEach((definition) => {
204-
if (!this.markers.has(definition['@id'])) {
205-
this.createMarker({ definition });
206-
}
207-
});
192+
this.onDrawChanged(this.markers, this.markersValue, this.createMarker, this.doRemoveMarker);
208193

209194
if (this.fitBoundsToMarkersValue) {
210195
this.doFitBoundsToMarkers();
@@ -216,47 +201,15 @@ export default abstract class<
216201
return;
217202
}
218203

219-
const idsToRemove = new Set(this.polygons.keys());
220-
this.polygonsValue.forEach((definition) => {
221-
idsToRemove.delete(definition['@id']);
222-
});
223-
224-
idsToRemove.forEach((id) => {
225-
// biome-ignore lint/style/noNonNullAssertion: the ids are coming from the keys of the map
226-
const polygon = this.polygons.get(id)!;
227-
this.doRemovePolygon(polygon);
228-
this.polygons.delete(id);
229-
});
230-
231-
this.polygonsValue.forEach((definition) => {
232-
if (!this.polygons.has(definition['@id'])) {
233-
this.createPolygon({ definition });
234-
}
235-
});
204+
this.onDrawChanged(this.polygons, this.polygonsValue, this.createPolygon, this.doRemovePolygon);
236205
}
237206

238207
public polylinesValueChanged(): void {
239208
if (!this.isConnected) {
240209
return;
241210
}
242211

243-
const idsToRemove = new Set(this.polylines.keys());
244-
this.polylinesValue.forEach((definition) => {
245-
idsToRemove.delete(definition['@id']);
246-
});
247-
248-
idsToRemove.forEach((id) => {
249-
// biome-ignore lint/style/noNonNullAssertion: the ids are coming from the keys of the map
250-
const polyline = this.polylines.get(id)!;
251-
this.doRemovePolyline(polyline);
252-
this.polylines.delete(id);
253-
});
254-
255-
this.polylinesValue.forEach((definition) => {
256-
if (!this.polylines.has(definition['@id'])) {
257-
this.createPolyline({ definition });
258-
}
259-
});
212+
this.onDrawChanged(this.polylines, this.polylinesValue, this.createPolyline, this.doRemovePolyline);
260213
}
261214

262215
//endregion
@@ -282,13 +235,17 @@ export default abstract class<
282235

283236
protected abstract doCreatePolygon({
284237
definition,
285-
}: { definition: PolygonDefinition<PolygonOptions, InfoWindowOptions> }): Polygon;
238+
}: {
239+
definition: PolygonDefinition<PolygonOptions, InfoWindowOptions>;
240+
}): Polygon;
286241

287242
protected abstract doRemovePolygon(polygon: Polygon): void;
288243

289244
protected abstract doCreatePolyline({
290245
definition,
291-
}: { definition: PolylineDefinition<PolylineOptions, InfoWindowOptions> }): Polyline;
246+
}: {
247+
definition: PolylineDefinition<PolylineOptions, InfoWindowOptions>;
248+
}): Polyline;
292249

293250
protected abstract doRemovePolyline(polyline: Polyline): void;
294251

@@ -299,10 +256,10 @@ export default abstract class<
299256
definition: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
300257
element: Marker | Polygon | Polyline;
301258
}): InfoWindow;
259+
302260
//endregion
303261

304262
//region Private APIs
305-
306263
private createDrawingFactory(
307264
type: 'marker',
308265
draws: typeof this.markers,
@@ -333,13 +290,56 @@ export default abstract class<
333290
// 'Factory' could be instantiated with an arbitrary type which could be unrelated to '({ definition }: { definition: WithIdentifier<any>; }) => Draw'
334291
return ({ definition }: { definition: WithIdentifier<any> }) => {
335292
this.dispatchEvent(eventBefore, { definition });
336-
const drawing = factory(definition) as Draw;
293+
const drawing = factory({ definition }) as Draw;
337294
this.dispatchEvent(eventAfter, { [type]: drawing });
338295

339296
draws.set(definition['@id'], drawing);
340297

341298
return drawing;
342299
};
343300
}
301+
302+
private onDrawChanged(
303+
draws: typeof this.markers,
304+
newDrawDefinitions: typeof this.markersValue,
305+
factory: typeof this.createMarker,
306+
remover: typeof this.doRemoveMarker
307+
): void;
308+
private onDrawChanged(
309+
draws: typeof this.polygons,
310+
newDrawDefinitions: typeof this.polygonsValue,
311+
factory: typeof this.createPolygon,
312+
remover: typeof this.doRemovePolygon
313+
): void;
314+
private onDrawChanged(
315+
draws: typeof this.polylines,
316+
newDrawDefinitions: typeof this.polylinesValue,
317+
factory: typeof this.createPolyline,
318+
remover: typeof this.doRemovePolyline
319+
): void;
320+
private onDrawChanged<Draw, DrawDefinition extends WithIdentifier<Record<string, unknown>>>(
321+
draws: globalThis.Map<WithIdentifier<any>, Draw>,
322+
newDrawDefinitions: Array<DrawDefinition>,
323+
factory: (args: { definition: DrawDefinition }) => Draw,
324+
remover: (args: Draw) => void
325+
): void {
326+
const idsToRemove = new Set(draws.keys());
327+
newDrawDefinitions.forEach((definition) => {
328+
idsToRemove.delete(definition['@id']);
329+
});
330+
331+
idsToRemove.forEach((id) => {
332+
// biome-ignore lint/style/noNonNullAssertion: the ids are coming from the keys of the map
333+
const draw = draws.get(id)!;
334+
remover(draw);
335+
draws.delete(id);
336+
});
337+
338+
newDrawDefinitions.forEach((definition) => {
339+
if (!draws.has(definition['@id'])) {
340+
factory({ definition });
341+
}
342+
});
343+
}
344344
//endregion
345345
}

src/Map/src/Bridge/Google/assets/dist/map_controller.js

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,7 @@ class default_1 extends Controller {
4343
if (!this.isConnected) {
4444
return;
4545
}
46-
const idsToRemove = new Set(this.markers.keys());
47-
this.markersValue.forEach((definition) => {
48-
idsToRemove.delete(definition['@id']);
49-
});
50-
idsToRemove.forEach((id) => {
51-
const marker = this.markers.get(id);
52-
this.doRemoveMarker(marker);
53-
this.markers.delete(id);
54-
});
55-
this.markersValue.forEach((definition) => {
56-
if (!this.markers.has(definition['@id'])) {
57-
this.createMarker({ definition });
58-
}
59-
});
46+
this.onDrawChanged(this.markers, this.markersValue, this.createMarker, this.doRemoveMarker);
6047
if (this.fitBoundsToMarkersValue) {
6148
this.doFitBoundsToMarkers();
6249
}
@@ -65,51 +52,41 @@ class default_1 extends Controller {
6552
if (!this.isConnected) {
6653
return;
6754
}
68-
const idsToRemove = new Set(this.polygons.keys());
69-
this.polygonsValue.forEach((definition) => {
70-
idsToRemove.delete(definition['@id']);
71-
});
72-
idsToRemove.forEach((id) => {
73-
const polygon = this.polygons.get(id);
74-
this.doRemovePolygon(polygon);
75-
this.polygons.delete(id);
76-
});
77-
this.polygonsValue.forEach((definition) => {
78-
if (!this.polygons.has(definition['@id'])) {
79-
this.createPolygon({ definition });
80-
}
81-
});
55+
this.onDrawChanged(this.polygons, this.polygonsValue, this.createPolygon, this.doRemovePolygon);
8256
}
8357
polylinesValueChanged() {
8458
if (!this.isConnected) {
8559
return;
8660
}
87-
const idsToRemove = new Set(this.polylines.keys());
88-
this.polylinesValue.forEach((definition) => {
89-
idsToRemove.delete(definition['@id']);
90-
});
91-
idsToRemove.forEach((id) => {
92-
const polyline = this.polylines.get(id);
93-
this.doRemovePolyline(polyline);
94-
this.polylines.delete(id);
95-
});
96-
this.polylinesValue.forEach((definition) => {
97-
if (!this.polylines.has(definition['@id'])) {
98-
this.createPolyline({ definition });
99-
}
100-
});
61+
this.onDrawChanged(this.polylines, this.polylinesValue, this.createPolyline, this.doRemovePolyline);
10162
}
10263
createDrawingFactory(type, draws, factory) {
10364
const eventBefore = `${type}:before-create`;
10465
const eventAfter = `${type}:after-create`;
10566
return ({ definition }) => {
10667
this.dispatchEvent(eventBefore, { definition });
107-
const drawing = factory(definition);
68+
const drawing = factory({ definition });
10869
this.dispatchEvent(eventAfter, { [type]: drawing });
10970
draws.set(definition['@id'], drawing);
11071
return drawing;
11172
};
11273
}
74+
onDrawChanged(draws, newDrawDefinitions, factory, remover) {
75+
const idsToRemove = new Set(draws.keys());
76+
newDrawDefinitions.forEach((definition) => {
77+
idsToRemove.delete(definition['@id']);
78+
});
79+
idsToRemove.forEach((id) => {
80+
const draw = draws.get(id);
81+
remover(draw);
82+
draws.delete(id);
83+
});
84+
newDrawDefinitions.forEach((definition) => {
85+
if (!draws.has(definition['@id'])) {
86+
factory({ definition });
87+
}
88+
});
89+
}
11390
}
11491
default_1.values = {
11592
providerOptions: Object,

0 commit comments

Comments
 (0)