@@ -119,6 +119,15 @@ export default abstract class<
119
119
protected infoWindows: Array < InfoWindow > = [ ] ;
120
120
121
121
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 ;
122
131
123
132
protected abstract dispatchEvent ( name : string , payload : Record < string , unknown > ) : void ;
124
133
@@ -127,10 +136,14 @@ export default abstract class<
127
136
128
137
this . dispatchEvent ( 'pre-connect' , { options } ) ;
129
138
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
+
130
143
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 } ) ) ;
134
147
135
148
if ( this . fitBoundsToMarkersValue ) {
136
149
this . doFitBoundsToMarkers ( ) ;
@@ -148,36 +161,6 @@ export default abstract class<
148
161
}
149
162
150
163
//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
-
181
164
public createInfoWindow ( {
182
165
definition,
183
166
element,
@@ -219,7 +202,7 @@ export default abstract class<
219
202
220
203
this . markersValue . forEach ( ( definition ) => {
221
204
if ( ! this . markers . has ( definition [ '@id' ] ) ) {
222
- this . createMarker ( definition ) ;
205
+ this . createMarker ( { definition } ) ;
223
206
}
224
207
} ) ;
225
208
@@ -247,7 +230,7 @@ export default abstract class<
247
230
248
231
this . polygonsValue . forEach ( ( definition ) => {
249
232
if ( ! this . polygons . has ( definition [ '@id' ] ) ) {
250
- this . createPolygon ( definition ) ;
233
+ this . createPolygon ( { definition } ) ;
251
234
}
252
235
} ) ;
253
236
}
@@ -271,10 +254,11 @@ export default abstract class<
271
254
272
255
this . polylinesValue . forEach ( ( definition ) => {
273
256
if ( ! this . polylines . has ( definition [ '@id' ] ) ) {
274
- this . createPolyline ( definition ) ;
257
+ this . createPolyline ( { definition } ) ;
275
258
}
276
259
} ) ;
277
260
}
261
+
278
262
//endregion
279
263
280
264
//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<
290
274
291
275
protected abstract doFitBoundsToMarkers ( ) : void ;
292
276
293
- protected abstract doCreateMarker ( definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > ) : Marker ;
277
+ protected abstract doCreateMarker ( {
278
+ definition,
279
+ } : { definition : MarkerDefinition < MarkerOptions , InfoWindowOptions > } ) : Marker ;
280
+
294
281
protected abstract doRemoveMarker ( marker : Marker ) : void ;
295
282
296
- protected abstract doCreatePolygon ( definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > ) : Polygon ;
283
+ protected abstract doCreatePolygon ( {
284
+ definition,
285
+ } : { definition : PolygonDefinition < PolygonOptions , InfoWindowOptions > } ) : Polygon ;
286
+
297
287
protected abstract doRemovePolygon ( polygon : Polygon ) : void ;
298
288
299
- protected abstract doCreatePolyline ( definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > ) : Polyline ;
289
+ protected abstract doCreatePolyline ( {
290
+ definition,
291
+ } : { definition : PolylineDefinition < PolylineOptions , InfoWindowOptions > } ) : Polyline ;
292
+
300
293
protected abstract doRemovePolyline ( polyline : Polyline ) : void ;
301
294
302
295
protected abstract doCreateInfoWindow ( {
@@ -307,4 +300,46 @@ export default abstract class<
307
300
element: Marker | Polygon | Polyline ;
308
301
} ) : InfoWindow ;
309
302
//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
310
345
}
0 commit comments