feat(sync): Allow the sync of datalayer creation

This commit is contained in:
Alexis Métaireau 2024-05-13 13:46:48 +02:00
parent 458d68a4bb
commit 8e5c434988
4 changed files with 17 additions and 10 deletions

View file

@ -54,9 +54,7 @@ export class MessagesDispatcher {
metadata.featureType metadata.featureType
) )
if (featureTypeExists) { if (featureTypeExists) {
const updater = this.updaters[metadata.featureType] return this.updaters[metadata.featureType]
console.log(`found updater ${metadata.featureType}, ${updater}`)
return updater
} }
case 'map': case 'map':
case 'datalayer': case 'datalayer':
@ -67,7 +65,7 @@ export class MessagesDispatcher {
} }
dispatch({ kind, ...payload }) { dispatch({ kind, ...payload }) {
console.log(kind, payload) console.log('received message', kind, payload)
if (kind == 'operation') { if (kind == 'operation') {
let updater = this.getUpdater(payload.subject, payload.metadata) let updater = this.getUpdater(payload.subject, payload.metadata)
updater.applyMessage(payload) updater.applyMessage(payload)

View file

@ -48,16 +48,19 @@ class BaseUpdater {
export class MapUpdater extends BaseUpdater { export class MapUpdater extends BaseUpdater {
update({ key, value }) { update({ key, value }) {
console.log(key, value)
this.updateObjectValue(this.map, key, value) this.updateObjectValue(this.map, key, value)
this.map.render([key]) this.map.render([key])
} }
} }
export class DataLayerUpdater extends BaseUpdater { export class DataLayerUpdater extends BaseUpdater {
upsert({ key, metadata, value }) {
// Inserts does not happen (we use multiple updates instead).
this.map.createDataLayer(value, false)
}
update({ key, metadata, value }) { update({ key, metadata, value }) {
const datalayer = this.getDataLayerFromID(metadata.id) const datalayer = this.getDataLayerFromID(metadata.id)
console.log('datalayer', datalayer, key, value)
this.updateObjectValue(datalayer, key, value) this.updateObjectValue(datalayer, key, value)
datalayer.render([key]) datalayer.render([key])
} }

View file

@ -812,11 +812,11 @@ U.Map = L.Map.extend({
return L.Map.prototype.setMaxBounds.call(this, bounds) return L.Map.prototype.setMaxBounds.call(this, bounds)
}, },
createDataLayer: function (datalayer) { createDataLayer: function (datalayer, sync) {
datalayer = datalayer || { datalayer = datalayer || {
name: `${L._('Layer')} ${this.datalayers_index.length + 1}`, name: `${L._('Layer')} ${this.datalayers_index.length + 1}`,
} }
return new U.DataLayer(this, datalayer) return new U.DataLayer(this, datalayer, sync)
}, },
newDataLayer: function () { newDataLayer: function () {

View file

@ -521,7 +521,7 @@ U.DataLayer = L.Evented.extend({
editMode: 'advanced', editMode: 'advanced',
}, },
initialize: function (map, data) { initialize: function (map, data, sync) {
this.map = map this.map = map
this._index = Array() this._index = Array()
this._layers = {} this._layers = {}
@ -605,6 +605,12 @@ U.DataLayer = L.Evented.extend({
// be in the "forced visibility" mode // be in the "forced visibility" mode
if (this.autoLoaded()) this.map.on('zoomend', this.onZoomEnd, this) if (this.autoLoaded()) this.map.on('zoomend', this.onZoomEnd, this)
this.on('datachanged', this.map.onDataLayersChanged, this.map) this.on('datachanged', this.map.onDataLayersChanged, this.map)
if (sync !== false) {
const { engine, subject, metadata } = this.getSyncMetadata()
const geoJSON = this.umapGeoJSON()
engine.upsert(subject, metadata, geoJSON)
}
}, },
getSyncMetadata: function () { getSyncMetadata: function () {
@ -612,7 +618,7 @@ U.DataLayer = L.Evented.extend({
engine: this.map.sync, engine: this.map.sync,
subject: 'datalayer', subject: 'datalayer',
metadata: { metadata: {
id: this.umap_id, // XXX: should we specify it's the layerID here? id: this.umap_id,
}, },
} }
}, },