Fix: attempt to fix layers which don't hide

This commit is contained in:
Pieter Vander Vennet 2025-04-23 18:24:53 +02:00
parent 26902c488f
commit 01034b186e
7 changed files with 80 additions and 38 deletions

View file

@ -262,9 +262,33 @@ class LineRenderingLayer {
this._visibility = visibility
this._fetchStore = fetchStore
this._onClick = onClick
features.features.addCallbackAndRunD(() => this.update(features.features))
map.on("styledata", () => this.update(features.features))
const updateNeededSrc = new UIEventSource(false)
updateNeededSrc.stabilized(100).addCallbackAndRunD(async updateNeeded => {
if (updateNeeded) {
await this.awaitStyleLoaded()
await this.update(features.features.data)
if (features.features.data.length === 0 && "mapcomplete_pedestrian_path_linerendering_2" === this._layername) {
console.trace(this._layername, "is empty")
}
updateNeededSrc.set(false)
}
})
features.features.addCallbackAndRunD(() => {
console.log("New features!", this._layername, features.features.data.length)
updateNeededSrc.set(true)
})
map.on("styledata", () => updateNeededSrc.set(true))
this.awaitStyleLoaded().then(() => {
const feats = features.features.data
if (feats.length > 0) {
this.update(feats)
} else {
updateNeededSrc.set(true)
}
}
)
}
public destruct(): void {
@ -343,20 +367,15 @@ class LineRenderingLayer {
return calculatedProps
}
private async update(featureSource: Store<Feature[]>) {
const map = this._map
while (!map.isStyleLoaded()) {
private async awaitStyleLoaded() {
while (!this._map.isStyleLoaded()) {
await Utils.waitFor(100)
}
}
private async update(features: Feature[]) {
const map = this._map
// After waiting 'till the map has loaded, the data might have changed already
// As such, we only now read the features from the featureSource and compare with the previously set data
const features = featureSource.data
if (!features || features.length === 0) {
// This is a very ugly workaround for https://source.mapcomplete.org/MapComplete/MapComplete/issues/2312,
// but I couldn't find the root cause
return
}
const src = <GeoJSONSource>map.getSource(this._layername)
{
// Add source to the map or update the feature source