fix(sync): do not send messages when loading data from the server

This introduces a `sync=true/false` parameter that can be passed along
when loading data from our own server, in which case we don't want to
send "operation" messages.
This commit is contained in:
Alexis Métaireau 2024-06-24 19:08:25 +02:00
parent 0dcedeb525
commit 7a7977c5cd
3 changed files with 11 additions and 10 deletions

View file

@ -23,7 +23,7 @@ U.FeatureMixin = {
// event triggered to cause a sync event, as it would reintroduce // event triggered to cause a sync event, as it would reintroduce
// deleted features. // deleted features.
// The `._marked_for_deletion` private property is here to track this status. // The `._marked_for_deletion` private property is here to track this status.
if (this._marked_for_deletion == true) { if (this._marked_for_deletion === true) {
this._marked_for_deletion = false this._marked_for_deletion = false
return return
} }
@ -37,7 +37,7 @@ U.FeatureMixin = {
initialize: function (map, latlng, options, id) { initialize: function (map, latlng, options, id) {
this.map = map this.map = map
this.sync = map.sync_engine.proxy(this) this.sync = map.sync_engine.proxy(this)
this._mark_for_deletion = false this._marked_for_deletion = false
if (typeof options === 'undefined') { if (typeof options === 'undefined') {
options = {} options = {}

View file

@ -465,7 +465,8 @@ U.Map = L.Map.extend({
initDataLayers: async function (datalayers) { initDataLayers: async function (datalayers) {
datalayers = datalayers || this.options.datalayers datalayers = datalayers || this.options.datalayers
for (const options of datalayers) { for (const options of datalayers) {
this.createDataLayer(options) // `false` to not propagate syncing elements served from uMap
this.createDataLayer(options, false)
} }
await this.loadDataLayers() await this.loadDataLayers()
}, },

View file

@ -730,8 +730,8 @@ U.DataLayer = L.Evented.extend({
} }
}, },
fromGeoJSON: function (geojson) { fromGeoJSON: function (geojson, sync = true) {
this.addData(geojson) this.addData(geojson, sync)
this._geojson = geojson this._geojson = geojson
this._dataloaded = true this._dataloaded = true
this.fire('dataloaded') this.fire('dataloaded')
@ -742,7 +742,7 @@ U.DataLayer = L.Evented.extend({
if (geojson._storage) geojson._umap_options = geojson._storage // Retrocompat if (geojson._storage) geojson._umap_options = geojson._storage // Retrocompat
if (geojson._umap_options) this.setOptions(geojson._umap_options) if (geojson._umap_options) this.setOptions(geojson._umap_options)
if (this.isRemoteLayer()) await this.fetchRemoteData() if (this.isRemoteLayer()) await this.fetchRemoteData()
else this.fromGeoJSON(geojson) else this.fromGeoJSON(geojson, false)
this._loaded = true this._loaded = true
}, },
@ -924,11 +924,11 @@ U.DataLayer = L.Evented.extend({
if (idx !== -1) this._propertiesIndex.splice(idx, 1) if (idx !== -1) this._propertiesIndex.splice(idx, 1)
}, },
addData: function (geojson) { addData: function (geojson, sync) {
try { try {
// Do not fail if remote data is somehow invalid, // Do not fail if remote data is somehow invalid,
// otherwise the layer becomes uneditable. // otherwise the layer becomes uneditable.
this.geojsonToFeatures(geojson) this.geojsonToFeatures(geojson, sync)
} catch (err) { } catch (err) {
console.log('Error with DataLayer', this.umap_id) console.log('Error with DataLayer', this.umap_id)
console.error(err) console.error(err)
@ -1015,7 +1015,7 @@ U.DataLayer = L.Evented.extend({
// The choice of the name is not ours, because it is required by Leaflet. // The choice of the name is not ours, because it is required by Leaflet.
// It is misleading, as the returned objects are uMap objects, and not // It is misleading, as the returned objects are uMap objects, and not
// GeoJSON features. // GeoJSON features.
geojsonToFeatures: function (geojson) { geojsonToFeatures: function (geojson, sync) {
if (!geojson) return if (!geojson) return
const features = geojson instanceof Array ? geojson : geojson.features const features = geojson instanceof Array ? geojson : geojson.features
let i let i
@ -1034,7 +1034,7 @@ U.DataLayer = L.Evented.extend({
const feature = this.geoJSONToLeaflet({ geometry, geojson }) const feature = this.geoJSONToLeaflet({ geometry, geojson })
if (feature) { if (feature) {
this.addLayer(feature) this.addLayer(feature)
feature.onCommit() if (sync) feature.onCommit()
return feature return feature
} }
}, },