More refactoring!

This commit is contained in:
Pieter Vander Vennet 2021-01-03 00:19:42 +01:00
parent b2c234b51d
commit 6ac8ec84e4
22 changed files with 170 additions and 158 deletions

View file

@ -1,16 +1,27 @@
import {Or, TagsFilter} from "./Tags";
import {UIEventSource} from "./UIEventSource";
import {FilteredLayer} from "./FilteredLayer";
import {Bounds} from "./Bounds";
import Bounds from "../Models/Bounds";
import {Overpass} from "./Osm/Overpass";
import State from "../State";
import MetaTagging from "./MetaTagging";
import Loc from "../Models/Loc";
import LayoutConfig from "../Customizations/JSON/LayoutConfig";
import FeatureSource from "./Actors/FeatureSource";
export class UpdateFromOverpass {
export default class UpdateFromOverpass implements FeatureSource{
/**
* The last loaded features of the geojson
*/
public readonly features: UIEventSource<any[]> = new UIEventSource<any[]>(undefined);
/**
* The time of updating according to Overpass
*/
public readonly freshness:UIEventSource<Date> = new UIEventSource<Date>(undefined);
public readonly sufficientlyZoomed: UIEventSource<boolean>;
public readonly runningQuery: UIEventSource<boolean> = new UIEventSource<boolean>(false);
public readonly retries: UIEventSource<number> = new UIEventSource<number>(0);
/**
* The previous bounds for which the query has been run at the given zoom level
*
@ -18,55 +29,58 @@ export class UpdateFromOverpass {
* If the map location changes, we check for each layer if it is loaded:
* we start checking the bounds at the first zoom level the layer might operate. If in bounds - no reload needed, otherwise we continue walking down
*/
private readonly previousBounds: Map<number, Bounds[]> = new Map<number, Bounds[]>();
private readonly _previousBounds: Map<number, Bounds[]> = new Map<number, Bounds[]>();
private readonly _location: UIEventSource<Loc>;
private readonly _layoutToUse: UIEventSource<LayoutConfig>;
private readonly _leafletMap: UIEventSource<L.Map>;
private readonly state: State;
/**
* The most important layer should go first, as that one gets first pick for the questions
*/
constructor(state: State) {
this.state = state;
constructor(
location: UIEventSource<Loc>,
layoutToUse: UIEventSource<LayoutConfig>,
leafletMap: UIEventSource<L.Map>) {
this._location = location;
this._layoutToUse = layoutToUse;
this._leafletMap = leafletMap;
const self = this;
this.sufficientlyZoomed = State.state.locationControl.map(location => {
this.sufficientlyZoomed = location.map(location => {
if(location?.zoom === undefined){
return false;
}
let minzoom = Math.min(...state.layoutToUse.data.layers.map(layer => layer.minzoom ?? 18));
let minzoom = Math.min(...layoutToUse.data.layers.map(layer => layer.minzoom ?? 18));
return location.zoom >= minzoom;
}, [state.layoutToUse]
}, [layoutToUse]
);
for (let i = 0; i < 25; i++) {
// This update removes all data on all layers -> erase the map on lower levels too
this.previousBounds.set(i, []);
this._previousBounds.set(i, []);
}
state.locationControl.addCallback(() => {
self.update(state)
layoutToUse.addCallback(() => {
self.update()
});
state.layoutToUse.addCallback(() => {
self.update(state)
location.addCallbackAndRun(() => {
self.update()
});
self.update(state);
}
public ForceRefresh() {
for (let i = 0; i < 25; i++) {
this.previousBounds.set(i, []);
this._previousBounds.set(i, []);
}
this.update(this.state);
this.update();
}
private GetFilter(state: State) {
private GetFilter() {
const filters: TagsFilter[] = [];
state = state ?? State.state;
for (const layer of state.layoutToUse.data.layers) {
for (const layer of this._layoutToUse.data.layers) {
if(typeof(layer) === "string"){
continue;
}
if (state.locationControl.data.zoom < layer.minzoom) {
if (this._location.data.zoom < layer.minzoom) {
continue;
}
if(layer.doNotDownload){
@ -77,12 +91,12 @@ export class UpdateFromOverpass {
// Check if data for this layer has already been loaded
let previouslyLoaded = false;
for (let z = layer.minzoom; z < 25 && !previouslyLoaded; z++) {
const previousLoadedBounds = this.previousBounds.get(z);
const previousLoadedBounds = this._previousBounds.get(z);
if (previousLoadedBounds === undefined) {
continue;
}
for (const previousLoadedBound of previousLoadedBounds) {
previouslyLoaded = previouslyLoaded || this.IsInBounds(state, previousLoadedBound);
previouslyLoaded = previouslyLoaded || this.IsInBounds(previousLoadedBound);
if(previouslyLoaded){
break;
}
@ -98,61 +112,8 @@ export class UpdateFromOverpass {
}
return new Or(filters);
}
private handleData(geojson: any) {
const self = this;
self.retries.setData(0);
let newIds = 1;
for (const feature of geojson.features) {
if (feature.properties.id === undefined) {
feature.properties.id = "ext/" + newIds;
newIds++;
}
}
geojson.features.forEach(feature => {
State.state.allElements.addElement(feature);
})
MetaTagging.addMetatags(geojson.features);
function renderLayers(layers: FilteredLayer[]) {
if (layers.length === 0) {
self.runningQuery.setData(false);
if (geojson.features.length > 0) {
console.warn("Got some leftovers: ", geojson)
}
return;
}
// We use window.setTimeout to give JS some time to update everything and make the interface not too laggy
window.setTimeout(() => {
const layer = layers[0];
const rest = layers.slice(1, layers.length);
geojson = layer.SetApplicableData(geojson);
renderLayers(rest);
}, 50)
}
renderLayers(State.state.filteredLayers.data);
}
private handleFail(state: State, reason: any) {
this.retries.data++;
this.ForceRefresh();
console.log(`QUERY FAILED (retrying in ${5 * this.retries.data} sec)`, reason);
this.retries.ping();
this.runningQuery.setData(false)
const self = this;
window?.setTimeout(
function () {
self.update(state)
}, this.retries.data * 5000
)
}
private update(state: State): void {
const filter = this.GetFilter(state);
private update(): void {
const filter = this.GetFilter();
if (filter === undefined) {
return;
}
@ -162,9 +123,9 @@ export class UpdateFromOverpass {
return;
}
const bounds = state.leafletMap.data.getBounds();
const bounds = this._leafletMap.data.getBounds();
const diff = state.layoutToUse.data.widenFactor;
const diff = this._layoutToUse.data.widenFactor;
const n = Math.min(90, bounds.getNorth() + diff);
const e = Math.min(180, bounds.getEast() + diff);
@ -172,18 +133,30 @@ export class UpdateFromOverpass {
const w = Math.max(-180, bounds.getWest() - diff);
const queryBounds = {north: n, east: e, south: s, west: w};
const z = Math.floor(state.locationControl.data.zoom);
const z = Math.floor(this._location.data.zoom);
this.runningQuery.setData(true);
const self = this;
const overpass = new Overpass(filter);
overpass.queryGeoJson(queryBounds,
function (data) {
self.previousBounds.get(z).push(queryBounds);
self.handleData(data)
function (data, date) {
self._previousBounds.get(z).push(queryBounds);
self.retries.setData(0);
self.freshness.setData(date);
self.features.setData(data.features);
self.runningQuery.setData(false);
},
function (reason) {
self.handleFail(state, reason)
self.retries.data++;
self.ForceRefresh();
console.log(`QUERY FAILED (retrying in ${5 * self.retries.data} sec)`, undefined);
self.retries.ping();
self.runningQuery.setData(false)
window?.setTimeout(
function () {
self.update()
}, self.retries.data * 5000
)
}
);
@ -191,12 +164,12 @@ export class UpdateFromOverpass {
}
private IsInBounds(state: State, bounds: Bounds): boolean {
if (this.previousBounds === undefined) {
private IsInBounds(bounds: Bounds): boolean {
if (this._previousBounds === undefined) {
return false;
}
const b = state.leafletMap.data.getBounds();
const b = this._leafletMap.data.getBounds();
return b.getSouth() >= bounds.south &&
b.getNorth() <= bounds.north &&
b.getEast() <= bounds.east &&