chore: refactor layer.isLoaded() (#2492)

This commit is contained in:
Yohan Boniface 2025-02-10 16:39:15 +01:00 committed by GitHub
commit e827e77bb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 10 additions and 24 deletions

View file

@ -45,8 +45,6 @@ export class DataLayer extends ServerStored {
this._features = {} this._features = {}
this._geojson = null this._geojson = null
this._propertiesIndex = [] this._propertiesIndex = []
this._loaded = false // Are layer metadata loaded
this._dataloaded = false // Are layer data loaded
this._leafletMap = leafletMap this._leafletMap = leafletMap
this.parentPane = this._leafletMap.getPane('overlayPane') this.parentPane = this._leafletMap.getPane('overlayPane')
@ -85,6 +83,7 @@ export class DataLayer extends ServerStored {
this.connectToMap() this.connectToMap()
this.permissions = new DataLayerPermissions(this._umap, this) this.permissions = new DataLayerPermissions(this._umap, this)
this._needsFetch = this.createdOnServer
if (!this.createdOnServer) { if (!this.createdOnServer) {
if (this.showAtLoad()) this.show() if (this.showAtLoad()) this.show()
} }
@ -243,7 +242,7 @@ export class DataLayer extends ServerStored {
} }
dataChanged() { dataChanged() {
if (!this.hasDataLoaded()) return if (!this.isLoaded()) return
this._umap.onDataLayersChanged() this._umap.onDataLayersChanged()
this.layer.dataChanged() this.layer.dataChanged()
} }
@ -252,13 +251,13 @@ export class DataLayer extends ServerStored {
if (!geojson) return [] if (!geojson) return []
const features = this.addData(geojson, sync) const features = this.addData(geojson, sync)
this._geojson = geojson this._geojson = geojson
this._needsFetch = false
this.onDataLoaded() this.onDataLoaded()
this.dataChanged() this.dataChanged()
return features return features
} }
onDataLoaded() { onDataLoaded() {
this._dataloaded = true
this.renderLegend() this.renderLegend()
} }
@ -268,7 +267,6 @@ export class DataLayer extends ServerStored {
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, false) else this.fromGeoJSON(geojson, false)
this._loaded = true
} }
clear() { clear() {
@ -320,7 +318,7 @@ export class DataLayer extends ServerStored {
async fetchRemoteData(force) { async fetchRemoteData(force) {
if (!this.isRemoteLayer()) return if (!this.isRemoteLayer()) return
if (!this.hasDynamicData() && this.hasDataLoaded() && !force) return if (!this.hasDynamicData() && this.isLoaded() && !force) return
if (!this.isVisible()) return if (!this.isVisible()) return
// Keep non proxied url for later use in Alert. // Keep non proxied url for later use in Alert.
const remoteUrl = this._umap.renderUrl(this.options.remoteData.url) const remoteUrl = this._umap.renderUrl(this.options.remoteData.url)
@ -345,11 +343,7 @@ export class DataLayer extends ServerStored {
} }
isLoaded() { isLoaded() {
return !this.createdOnServer || this._loaded return !this._needsFetch
}
hasDataLoaded() {
return this._dataloaded
} }
backupOptions() { backupOptions() {
@ -633,8 +627,6 @@ export class DataLayer extends ServerStored {
this.propagateDelete() this.propagateDelete()
this._leaflet_events_bk = this._leaflet_events this._leaflet_events_bk = this._leaflet_events
this.clear() this.clear()
delete this._loaded
delete this._dataloaded
} }
reset() { reset() {
@ -652,7 +644,6 @@ export class DataLayer extends ServerStored {
this.hide() this.hide()
if (this.isRemoteLayer()) this.fetchRemoteData() if (this.isRemoteLayer()) this.fetchRemoteData()
else if (this._geojson_bk) this.fromGeoJSON(this._geojson_bk) else if (this._geojson_bk) this.fromGeoJSON(this._geojson_bk)
this._loaded = true
this.show() this.show()
this.isDirty = false this.isDirty = false
} }
@ -1108,9 +1099,7 @@ export class DataLayer extends ServerStored {
async save() { async save() {
if (this.isDeleted) return await this.saveDelete() if (this.isDeleted) return await this.saveDelete()
if (!this.isLoaded()) { if (!this.isLoaded()) return
return
}
const geojson = this.umapGeoJSON() const geojson = this.umapGeoJSON()
const formData = new FormData() const formData = new FormData()
formData.append('name', this.options.name) formData.append('name', this.options.name)
@ -1172,7 +1161,6 @@ export class DataLayer extends ServerStored {
this.backupOptions() this.backupOptions()
this.backupData() this.backupData()
this.connectToMap() this.connectToMap()
this._loaded = true
this.redraw() // Needed for reordering features this.redraw() // Needed for reordering features
return true return true
} }

View file

@ -75,7 +75,7 @@ const ClassifiedMixin = {
}, },
renderLegend: function (container) { renderLegend: function (container) {
if (!this.datalayer.hasDataLoaded()) return if (!this.datalayer.isLoaded()) return
const parent = DomUtil.create('ul', '', container) const parent = DomUtil.create('ul', '', container)
const items = this.getLegendItems() const items = this.getLegendItems()
for (const [color, label] of items) { for (const [color, label] of items) {

View file

@ -55,15 +55,11 @@ export class DataLayerUpdater extends BaseUpdater {
upsert({ value }) { upsert({ value }) {
// Upsert only happens when a new datalayer is created. // Upsert only happens when a new datalayer is created.
const datalayer = this._umap.createDataLayer(value, false) const datalayer = this._umap.createDataLayer(value, false)
// Prevent the layer to get data from the server, as it will get it
// from the sync.
datalayer._loaded = true
} }
update({ key, metadata, value }) { update({ key, metadata, value }) {
const datalayer = this.getDataLayerFromID(metadata.id) const datalayer = this.getDataLayerFromID(metadata.id)
if (fieldInSchema(key)) { if (fieldInSchema(key)) {
datalayer._loaded = true
this.updateObjectValue(datalayer, key, value) this.updateObjectValue(datalayer, key, value)
} else { } else {
console.debug( console.debug(

View file

@ -398,7 +398,9 @@ def test_should_sync_datalayers(new_page, asgi_live_server, tilelayer):
peerA.locator("#map").click() peerA.locator("#map").click()
# Make sure this new marker is in Layer 2 for peerB # Make sure this new marker is in Layer 2 for peerB
expect(peerB.get_by_text("Layer 2")).to_be_visible() # Show features for this layer in the brower.
peerB.get_by_role("heading", name="Layer 2").locator(".datalayer-name").click()
expect(peerB.locator("li").filter(has_text="Layer 2")).to_be_visible()
peerB.locator(".panel.left").get_by_role("button", name="Show/hide layer").nth( peerB.locator(".panel.left").get_by_role("button", name="Show/hide layer").nth(
1 1
).click() ).click()