refactor(sync): Remove unnecessary complexity

Because we're relying on the `geoJSONToFeatures` method, we don't need
anymore updaters, the default ones (map, datalayer, feature) are enough.

It also makes the codebase compatible with our eslint configuration.
This commit is contained in:
Alexis Métaireau 2024-05-13 16:28:05 +02:00
parent 9e36476abe
commit 80eaa151de
3 changed files with 7 additions and 47 deletions

View file

@ -1,11 +1,5 @@
import { WebSocketTransport } from './websocket.js' import { WebSocketTransport } from './websocket.js'
import { import { MapUpdater, DataLayerUpdater, FeatureUpdater } from './updaters.js'
MapUpdater,
MarkerUpdater,
PolygonUpdater,
PolylineUpdater,
DataLayerUpdater,
} from './updaters.js'
export class SyncEngine { export class SyncEngine {
constructor(map) { constructor(map) {
@ -40,28 +34,16 @@ export class MessagesDispatcher {
this.map = map this.map = map
this.updaters = { this.updaters = {
map: new MapUpdater(this.map), map: new MapUpdater(this.map),
marker: new MarkerUpdater(this.map), feature: new FeatureUpdater(this.map),
polyline: new PolylineUpdater(this.map),
polygon: new PolygonUpdater(this.map),
datalayer: new DataLayerUpdater(this.map), datalayer: new DataLayerUpdater(this.map),
} }
} }
getUpdater(subject, metadata) { getUpdater(subject, metadata) {
switch (subject) { if (Object.keys(this.updaters).includes(subject)) {
case 'feature': return this.updaters[subject]
const featureTypeExists = Object.keys(this.updaters).includes(
metadata.featureType
)
if (featureTypeExists) {
return this.updaters[metadata.featureType]
}
case 'map':
case 'datalayer':
return this.updaters[subject]
default:
throw new Error(`Unknown updater ${subject}, ${metadata}`)
} }
throw new Error(`Unknown updater ${subject}, ${metadata}`)
} }
dispatch({ kind, ...payload }) { dispatch({ kind, ...payload }) {

View file

@ -74,7 +74,7 @@ export class DataLayerUpdater extends BaseUpdater {
* - `featureClass`: the name of the class to create the feature * - `featureClass`: the name of the class to create the feature
* - `featureArgument`: an object with the properties to pass to the class when bulding it. * - `featureArgument`: an object with the properties to pass to the class when bulding it.
**/ **/
class FeatureUpdater extends BaseUpdater { export class FeatureUpdater extends BaseUpdater {
getFeatureFromMetadata({ id, layerId }) { getFeatureFromMetadata({ id, layerId }) {
const datalayer = this.getDataLayerFromID(layerId) const datalayer = this.getDataLayerFromID(layerId)
return datalayer.getFeatureById(id) return datalayer.getFeatureById(id)
@ -122,25 +122,3 @@ class FeatureUpdater extends BaseUpdater {
if (feature) feature.del() if (feature) feature.del()
} }
} }
class PathUpdater extends FeatureUpdater {}
class MarkerUpdater extends FeatureUpdater {
featureType = 'marker'
featureClass = U.Marker
featureArgument = 'latlng'
}
class PolygonUpdater extends PathUpdater {
featureType = 'polygon'
featureClass = U.Polygon
featureArgument = 'latlngs'
}
class PolylineUpdater extends PathUpdater {
featureType = 'polyline'
featureClass = U.Polyline
featureArgument = 'latlngs'
}
export { MarkerUpdater, PolygonUpdater, PolylineUpdater }

View file

@ -1082,7 +1082,7 @@ U.DataLayer = L.Evented.extend({
let latlng, latlngs let latlng, latlngs
// Create a default geojson if none is provided // Create a default geojson if none is provided
geojson ??= { type: 'Feature', geometry: geometry } if (geojson === undefined) geojson = { type: 'Feature', geometry: geometry }
switch (geometry.type) { switch (geometry.type) {
case 'Point': case 'Point':