@@ -119,6 +119,15 @@ export default abstract class<
119119 protected infoWindows: Array < InfoWindow > = [ ] ;
120120
121121 private isConnected = false ;
122+ private createMarker: ( {
123+ definition,
124+ } : { definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > } ) => Marker ;
125+ private createPolygon: ( {
126+ definition,
127+ } : { definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > } ) => Polygon ;
128+ private createPolyline: ( {
129+ definition,
130+ } : { definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > } ) => Polyline ;
122131
123132 protected abstract dispatchEvent ( name : string , payload : Record < string , unknown > ) : void ;
124133
@@ -127,10 +136,14 @@ export default abstract class<
127136
128137 this . dispatchEvent ( 'pre-connect' , { options } ) ;
129138
139+ this . createMarker = this . createDrawingFactory ( 'marker' , this . markers , this . doCreateMarker . bind ( this ) ) ;
140+ this . createPolygon = this . createDrawingFactory ( 'polygon' , this . polygons , this . doCreatePolygon . bind ( this ) ) ;
141+ this . createPolyline = this . createDrawingFactory ( 'polyline' , this . polylines , this . doCreatePolyline . bind ( this ) ) ;
142+
130143 this . map = this . doCreateMap ( { center : this . centerValue , zoom : this . zoomValue , options } ) ;
131- this . markersValue . forEach ( ( marker ) => this . createMarker ( marker ) ) ;
132- this . polygonsValue . forEach ( ( polygon ) => this . createPolygon ( polygon ) ) ;
133- this . polylinesValue . forEach ( ( polyline ) => this . createPolyline ( polyline ) ) ;
144+ this . markersValue . forEach ( ( definition ) => this . createMarker ( { definition } ) ) ;
145+ this . polygonsValue . forEach ( ( definition ) => this . createPolygon ( { definition } ) ) ;
146+ this . polylinesValue . forEach ( ( definition ) => this . createPolyline ( { definition } ) ) ;
134147
135148 if ( this . fitBoundsToMarkersValue ) {
136149 this . doFitBoundsToMarkers ( ) ;
@@ -148,36 +161,6 @@ export default abstract class<
148161 }
149162
150163 //region Public API
151- public createMarker ( definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > ) : Marker {
152- this . dispatchEvent ( 'marker:before-create' , { definition } ) ;
153- const marker = this . doCreateMarker ( definition ) ;
154- this . dispatchEvent ( 'marker:after-create' , { marker } ) ;
155-
156- this . markers . set ( definition [ '@id' ] , marker ) ;
157-
158- return marker ;
159- }
160-
161- public createPolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon {
162- this . dispatchEvent ( 'polygon:before-create' , { definition } ) ;
163- const polygon = this . doCreatePolygon ( definition ) ;
164- this . dispatchEvent ( 'polygon:after-create' , { polygon } ) ;
165-
166- this . polygons . set ( definition [ '@id' ] , polygon ) ;
167-
168- return polygon ;
169- }
170-
171- public createPolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline {
172- this . dispatchEvent ( 'polyline:before-create' , { definition } ) ;
173- const polyline = this . doCreatePolyline ( definition ) ;
174- this . dispatchEvent ( 'polyline:after-create' , { polyline } ) ;
175-
176- this . polylines . set ( definition [ '@id' ] , polyline ) ;
177-
178- return polyline ;
179- }
180-
181164 public createInfoWindow ( {
182165 definition,
183166 element,
@@ -219,7 +202,7 @@ export default abstract class<
219202
220203 this . markersValue . forEach ( ( definition ) => {
221204 if ( ! this . markers . has ( definition [ '@id' ] ) ) {
222- this . createMarker ( definition ) ;
205+ this . createMarker ( { definition } ) ;
223206 }
224207 } ) ;
225208
@@ -247,7 +230,7 @@ export default abstract class<
247230
248231 this . polygonsValue . forEach ( ( definition ) => {
249232 if ( ! this . polygons . has ( definition [ '@id' ] ) ) {
250- this . createPolygon ( definition ) ;
233+ this . createPolygon ( { definition } ) ;
251234 }
252235 } ) ;
253236 }
@@ -271,10 +254,11 @@ export default abstract class<
271254
272255 this . polylinesValue . forEach ( ( definition ) => {
273256 if ( ! this . polylines . has ( definition [ '@id' ] ) ) {
274- this . createPolyline ( definition ) ;
257+ this . createPolyline ( { definition } ) ;
275258 }
276259 } ) ;
277260 }
261+
278262 //endregion
279263
280264 //region Abstract factory methods to be implemented by the concrete classes, they are specific to the map provider
@@ -290,13 +274,22 @@ export default abstract class<
290274
291275 protected abstract doFitBoundsToMarkers ( ) : void ;
292276
293- protected abstract doCreateMarker ( definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > ) : Marker ;
277+ protected abstract doCreateMarker ( {
278+ definition,
279+ } : { definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > } ) : Marker ;
280+
294281 protected abstract doRemoveMarker ( marker : Marker ) : void ;
295282
296- protected abstract doCreatePolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon ;
283+ protected abstract doCreatePolygon ( {
284+ definition,
285+ } : { definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > } ) : Polygon ;
286+
297287 protected abstract doRemovePolygon ( polygon : Polygon ) : void ;
298288
299- protected abstract doCreatePolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline ;
289+ protected abstract doCreatePolyline ( {
290+ definition,
291+ } : { definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > } ) : Polyline ;
292+
300293 protected abstract doRemovePolyline ( polyline : Polyline ) : void ;
301294
302295 protected abstract doCreateInfoWindow ( {
@@ -307,4 +300,46 @@ export default abstract class<
307300 element: Marker | Polygon | Polyline ;
308301 } ) : InfoWindow ;
309302 //endregion
303+
304+ //region Private APIs
305+
306+ private createDrawingFactory (
307+ type : 'marker' ,
308+ draws : typeof this . markers ,
309+ factory : typeof this . doCreateMarker
310+ ) : typeof this . doCreateMarker ;
311+ private createDrawingFactory (
312+ type : 'polygon' ,
313+ draws : typeof this . polygons ,
314+ factory : typeof this . doCreatePolygon
315+ ) : typeof this . doCreatePolygon ;
316+ private createDrawingFactory (
317+ type : 'polyline' ,
318+ draws : typeof this . polylines ,
319+ factory : typeof this . doCreatePolyline
320+ ) : typeof this . doCreatePolyline ;
321+ private createDrawingFactory <
322+ Factory extends typeof this . doCreateMarker | typeof this . doCreatePolygon | typeof this . doCreatePolyline ,
323+ Draw extends ReturnType < Factory > ,
324+ > (
325+ type : 'marker' | 'polygon' | 'polyline' ,
326+ draws : globalThis . Map < WithIdentifier < any > , Draw > ,
327+ factory : Factory
328+ ) : Factory {
329+ const eventBefore = `${type } :before-create`;
330+ const eventAfter = `${type } :after-create`;
331+
332+ // @ts -expect-error IDK what to do with this error
333+ // 'Factory' could be instantiated with an arbitrary type which could be unrelated to '({ definition }: { definition: WithIdentifier<any>; }) => Draw'
334+ return ( { definition } : { definition : WithIdentifier < any > } ) => {
335+ this . dispatchEvent ( eventBefore , { definition } ) ;
336+ const drawing = factory ( definition ) as Draw ;
337+ this . dispatchEvent ( eventAfter , { [ type ] : drawing } ) ;
338+
339+ draws . set ( definition [ '@id' ] , drawing ) ;
340+
341+ return drawing ;
342+ } ;
343+ }
344+ //endregion
310345}
0 commit comments