@@ -176,6 +176,7 @@ export default abstract class<
176
176
177
177
return infoWindow ;
178
178
}
179
+
179
180
//endregion
180
181
181
182
//region Hooks called by Stimulus when the values change
@@ -188,23 +189,7 @@ export default abstract class<
188
189
return ;
189
190
}
190
191
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 ) ;
208
193
209
194
if ( this . fitBoundsToMarkersValue ) {
210
195
this . doFitBoundsToMarkers ( ) ;
@@ -216,47 +201,15 @@ export default abstract class<
216
201
return ;
217
202
}
218
203
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 ) ;
236
205
}
237
206
238
207
public polylinesValueChanged ( ) : void {
239
208
if ( ! this . isConnected ) {
240
209
return ;
241
210
}
242
211
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 ) ;
260
213
}
261
214
262
215
//endregion
@@ -282,13 +235,17 @@ export default abstract class<
282
235
283
236
protected abstract doCreatePolygon ( {
284
237
definition,
285
- } : { definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > } ) : Polygon ;
238
+ } : {
239
+ definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ;
240
+ } ) : Polygon ;
286
241
287
242
protected abstract doRemovePolygon ( polygon : Polygon ) : void ;
288
243
289
244
protected abstract doCreatePolyline ( {
290
245
definition,
291
- } : { definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > } ) : Polyline ;
246
+ } : {
247
+ definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ;
248
+ } ) : Polyline ;
292
249
293
250
protected abstract doRemovePolyline ( polyline : Polyline ) : void ;
294
251
@@ -299,10 +256,10 @@ export default abstract class<
299
256
definition : InfoWindowWithoutPositionDefinition < InfoWindowOptions > ;
300
257
element: Marker | Polygon | Polyline ;
301
258
} ) : InfoWindow ;
259
+
302
260
//endregion
303
261
304
262
//region Private APIs
305
-
306
263
private createDrawingFactory (
307
264
type : 'marker' ,
308
265
draws : typeof this . markers ,
@@ -333,13 +290,56 @@ export default abstract class<
333
290
// 'Factory' could be instantiated with an arbitrary type which could be unrelated to '({ definition }: { definition: WithIdentifier<any>; }) => Draw'
334
291
return ( { definition } : { definition : WithIdentifier < any > } ) => {
335
292
this . dispatchEvent ( eventBefore , { definition } ) ;
336
- const drawing = factory ( definition ) as Draw ;
293
+ const drawing = factory ( { definition } ) as Draw ;
337
294
this . dispatchEvent ( eventAfter , { [ type ] : drawing } ) ;
338
295
339
296
draws . set ( definition [ '@id' ] , drawing ) ;
340
297
341
298
return drawing ;
342
299
} ;
343
300
}
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
+ }
344
344
//endregion
345
345
}
0 commit comments