Stabilize popup

This commit is contained in:
Pieter Vander Vennet 2021-05-07 01:43:32 +02:00
parent 729f28fbf5
commit 292bad5df7
6 changed files with 74 additions and 45 deletions

View file

@ -21,27 +21,48 @@ export default class FeatureSourceMerger implements FeatureSource {
}
private Update() {
let all = {}; // Mapping 'id' -> {feature, freshness}
let somethingChanged = false;
const all: Map<string, { feature: any, freshness: Date }> = new Map<string, { feature: any; freshness: Date }>();
// We seed the dictionary with the previously loaded features
const oldValues = this.features.data ?? [];
for (const oldValue of oldValues) {
all.set(oldValue.feature.id, oldValue)
}
for (const source of this._sources) {
if (source?.features?.data === undefined) {
continue;
}
for (const f of source.features.data) {
const id = f.feature.properties.id;
const oldV = all[id];
if (oldV === undefined) {
all[id] = f;
} else {
if (oldV.freshness < f.freshness) {
all[id] = f;
}
if (!all.has(id)) {
// This is a new feature
somethingChanged = true;
all.set(id, f);
continue;
}
// This value has been seen already, either in a previous run or by a previous datasource
// Let's figure out if something changed
const oldV = all.get(id);
if (oldV.freshness < f.freshness) {
// Jup, this feature is fresher
all.set(id, f);
somethingChanged = true;
}
}
}
const newList = [];
for (const id in all) {
newList.push(all[id]);
if(!somethingChanged){
// We don't bother triggering an update
return;
}
const newList = [];
all.forEach((value, key) => {
newList.push(value)
})
this.features.setData(newList);
}

View file

@ -34,7 +34,7 @@ export default class FilteringFeatureSource implements FeatureSource {
const newFeatures = features.filter(f => {
const layerId = f.feature._matching_layer_id;
if(selectedElement.data === f.feature){
if(selectedElement.data !== undefined && selectedElement.data?.id === f.feature.id){
// This is the selected object - it gets a free pass even if zoom is not sufficient
return true;
}

View file

@ -22,10 +22,8 @@ export default class OsmApiFeatureSource implements FeatureSource {
public load(id: string) {
console.log("Updating from OSM API: ", id)
OsmObject.DownloadObject(id, (element, meta) => {
const geojson = element.asGeoJson();
console.warn(geojson)
geojson.id = geojson.properties.id;
this.features.setData([{feature: geojson, freshness: meta["_last_edit:timestamp"]}])
})