Robustify in the case of werid hashes

This commit is contained in:
Pieter Vander Vennet 2021-07-28 00:09:17 +02:00
parent 04cd7b6c16
commit cd1ee64f62

View file

@ -1,6 +1,6 @@
import {UIEventSource} from "../UIEventSource"; import {UIEventSource} from "../UIEventSource";
import FeatureSource from "../FeatureSource/FeatureSource"; import FeatureSource from "../FeatureSource/FeatureSource";
import {OsmObject, OsmObjectMeta} from "../Osm/OsmObject"; import {OsmObject} from "../Osm/OsmObject";
import Loc from "../../Models/Loc"; import Loc from "../../Models/Loc";
import FeaturePipeline from "../FeatureSource/FeaturePipeline"; import FeaturePipeline from "../FeatureSource/FeaturePipeline";
import OsmApiFeatureSource from "../FeatureSource/OsmApiFeatureSource"; import OsmApiFeatureSource from "../FeatureSource/OsmApiFeatureSource";
@ -13,10 +13,10 @@ export default class SelectedFeatureHandler {
private readonly _hash: UIEventSource<string>; private readonly _hash: UIEventSource<string>;
private readonly _selectedFeature: UIEventSource<any>; private readonly _selectedFeature: UIEventSource<any>;
private static readonly _no_trigger_on = ["welcome","copyright","layers","new"] private static readonly _no_trigger_on = ["welcome", "copyright", "layers", "new"]
private readonly _osmApiSource: OsmApiFeatureSource; private readonly _osmApiSource: OsmApiFeatureSource;
constructor(hash: UIEventSource<string>, constructor(hash: UIEventSource<string>,
selectedFeature: UIEventSource<any>, selectedFeature: UIEventSource<any>,
featureSource: FeaturePipeline, featureSource: FeaturePipeline,
osmApiSource: OsmApiFeatureSource) { osmApiSource: OsmApiFeatureSource) {
@ -28,24 +28,30 @@ export default class SelectedFeatureHandler {
hash.addCallback(h => { hash.addCallback(h => {
if (h === undefined || h === "") { if (h === undefined || h === "") {
selectedFeature.setData(undefined); selectedFeature.setData(undefined);
}else{ } else {
self.selectFeature(); self.selectFeature();
} }
}) })
hash.addCallbackAndRun(h => self.downloadFeature(h)) hash.addCallbackAndRunD(h => {
try {
self.downloadFeature(h)
} catch (e) {
console.error("Could not download feature, probably a weird hash")
}
})
featureSource.features.addCallback(_ => self.selectFeature()); featureSource.features.addCallback(_ => self.selectFeature());
selectedFeature.addCallback(feature => { selectedFeature.addCallback(feature => {
if(feature === undefined){ if (feature === undefined) {
if(SelectedFeatureHandler._no_trigger_on.indexOf(hash.data) < 0){ if (SelectedFeatureHandler._no_trigger_on.indexOf(hash.data) < 0) {
hash.setData("") hash.setData("")
} }
} }
const h = feature?.properties?.id; const h = feature?.properties?.id;
if(h !== undefined){ if (h !== undefined) {
hash.setData(h) hash.setData(h)
} }
}) })
@ -53,55 +59,60 @@ export default class SelectedFeatureHandler {
this.selectFeature(); this.selectFeature();
} }
// If a feature is selected via the hash, zoom there // If a feature is selected via the hash, zoom there
public zoomToSelectedFeature(location: UIEventSource<Loc>){ public zoomToSelectedFeature(location: UIEventSource<Loc>) {
const hash = this._hash.data; const hash = this._hash.data;
if(hash === undefined || SelectedFeatureHandler._no_trigger_on.indexOf(hash) >= 0){ if (hash === undefined || SelectedFeatureHandler._no_trigger_on.indexOf(hash) >= 0) {
return; // No valid feature selected return; // No valid feature selected
} }
// We should have a valid osm-ID and zoom to it... But we wrap it in try-catch to be sure // We should have a valid osm-ID and zoom to it... But we wrap it in try-catch to be sure
try{ try {
OsmObject.DownloadObject(hash).addCallbackAndRunD(element => { OsmObject.DownloadObject(hash).addCallbackAndRunD(element => {
const centerpoint = element.centerpoint(); const centerpoint = element.centerpoint();
console.log("Zooming to location for select point: ", centerpoint) console.log("Zooming to location for select point: ", centerpoint)
location.data.lat = centerpoint[0] location.data.lat = centerpoint[0]
location.data.lon = centerpoint[1] location.data.lon = centerpoint[1]
location.ping(); location.ping();
}) })
}catch(e){ } catch (e) {
console.error("Could not download OSM-object with id", hash, " - probably a weird hash") console.error("Could not download OSM-object with id", hash, " - probably a weird hash")
} }
} }
private downloadFeature(hash: string){ private downloadFeature(hash: string) {
if(hash === undefined || hash === ""){ if (hash === undefined || hash === "") {
return; return;
} }
if(SelectedFeatureHandler._no_trigger_on.indexOf(hash) >= 0){ if (SelectedFeatureHandler._no_trigger_on.indexOf(hash) >= 0) {
return; return;
} }
this._osmApiSource.load(hash) try {
this._osmApiSource.load(hash)
} catch (e) {
console.log("Could not download feature, probably a weird hash:", hash)
}
} }
private selectFeature(){ private selectFeature() {
const features = this._featureSource?.features?.data; const features = this._featureSource?.features?.data;
if(features === undefined){ if (features === undefined) {
return; return;
} }
if(this._selectedFeature.data?.properties?.id === this._hash.data){ if (this._selectedFeature.data?.properties?.id === this._hash.data) {
// Feature already selected // Feature already selected
return; return;
} }
const hash = this._hash.data; const hash = this._hash.data;
if(hash === undefined || hash === "" || hash === "#"){ if (hash === undefined || hash === "" || hash === "#") {
return; return;
} }
for (const feature of features) { for (const feature of features) {
const id = feature.feature?.properties?.id; const id = feature.feature?.properties?.id;
if(id === hash){ if (id === hash) {
this._selectedFeature.setData(feature.feature); this._selectedFeature.setData(feature.feature);
break; break;
} }