@@ -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 < { } > > (
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}
0 commit comments