Small tweaks, stabilizing local source cache

This commit is contained in:
Pieter Vander Vennet 2021-01-15 01:57:46 +01:00
parent f33fe081d0
commit fa4fb71e06
8 changed files with 53 additions and 37 deletions

View file

@ -10,38 +10,41 @@ import {UIEventSource} from "../UIEventSource";
import LocalStorageSaver from "./LocalStorageSaver";
import LayerConfig from "../../Customizations/JSON/LayerConfig";
import LocalStorageSource from "./LocalStorageSource";
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
export default class FeaturePipeline implements FeatureSource {
public features: UIEventSource<{ feature: any; freshness: Date }[]>;
constructor(flayers: { isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig }[], updater: FeatureSource) {
const overpassSource = new WayHandlingApplyingFeatureSource(flayers,
new NoOverlapSource(flayers, new FeatureDuplicatorPerLayer(flayers, updater))
);
constructor(flayers: { isDisplayed: UIEventSource<boolean>, layerDef: LayerConfig }[],
updater: FeatureSource,
layout: UIEventSource<LayoutConfig>) {
const amendedOverpassSource =
new RememberingSource(new LocalStorageSaver(
overpassSource
));
new RememberingSource(
new WayHandlingApplyingFeatureSource(flayers,
new NoOverlapSource(flayers, new FeatureDuplicatorPerLayer(flayers,
new LocalStorageSaver(updater, layout)))
)
);
const amendedLocalStorageSource =
new RememberingSource(
new WayHandlingApplyingFeatureSource(flayers,
new NoOverlapSource(flayers, new FeatureDuplicatorPerLayer(flayers, new LocalStorageSource(layout)))
));
const merged = new FeatureSourceMerger([
amendedOverpassSource,
new FeatureDuplicatorPerLayer(flayers, State.state.changes),
new LocalStorageSource()
amendedLocalStorageSource
]);
merged.features.addCallbackAndRun(feats => console.log("Merged has",feats?.length))
const source =
new FilteringFeatureSource(
flayers,
State.state.locationControl,
merged
);
source.features.addCallbackAndRun(feats => console.log("Filtered has",feats?.length))
this.features = source.features;
}

View file

@ -5,12 +5,13 @@
*/
import FeatureSource from "./FeatureSource";
import {UIEventSource} from "../UIEventSource";
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
export default class LocalStorageSaver implements FeatureSource {
public static readonly storageKey: string = "cached-features";
public readonly features: UIEventSource<{ feature: any; freshness: Date }[]>;
constructor(source: FeatureSource) {
constructor(source: FeatureSource, layout: UIEventSource<LayoutConfig>) {
this.features = source.features;
this.features.addCallbackAndRun(features => {
@ -22,7 +23,9 @@ export default class LocalStorageSaver implements FeatureSource {
}
try {
localStorage.setItem(LocalStorageSaver.storageKey, JSON.stringify(features));
const key = LocalStorageSaver.storageKey+layout.data.id
localStorage.setItem(key, JSON.stringify(features));
console.log("Saved ",features.length, "elements to",key)
} catch (e) {
console.warn("Could not save the features to local storage:", e)
}

View file

@ -1,21 +1,25 @@
import FeatureSource from "./FeatureSource";
import {UIEventSource} from "../UIEventSource";
import LocalStorageSaver from "./LocalStorageSaver";
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
export default class LocalStorageSource implements FeatureSource {
public features: UIEventSource<{ feature: any; freshness: Date }[]>;
constructor() {
constructor(layout: UIEventSource<LayoutConfig>) {
this.features = new UIEventSource<{ feature: any; freshness: Date }[]>([])
const key = LocalStorageSaver.storageKey + layout.data.id
try {
const fromStorage = localStorage.getItem(LocalStorageSaver.storageKey);
const fromStorage = localStorage.getItem(key);
if (fromStorage == null) {
return;
}
const loaded = JSON.parse(fromStorage);
this.features.setData(loaded);
console.log("Loaded ",loaded.length," features from localstorage as cache")
} catch (e) {
console.log("Could not load features from localStorage:", e)
localStorage.removeItem(key)
}
}

View file

@ -161,7 +161,7 @@ class BBox{
}
static get(feature) {
if (feature.bbox === undefined) {
if (feature.bbox?.overlapsWith === undefined) {
if (feature.geometry.type === "MultiPolygon") {
let coordinates = [];

View file

@ -4,11 +4,11 @@ import {Utils} from "../../Utils";
export class OsmPreferences {
private auth: any;
private userDetails: UIEventSource<UserDetails>;
public preferences = new UIEventSource<any>({});
public preferenceSources: any = {}
private auth: any;
private userDetails: UIEventSource<UserDetails>;
private longPreferences = {};
constructor(auth, osmConnection: OsmConnection) {
this.auth = auth;
@ -17,8 +17,6 @@ export class OsmPreferences {
osmConnection.OnLoggedIn(() => self.UpdatePreferences());
}
private longPreferences = {};
/**
* OSM preferences can be at most 255 chars
* @param key
@ -43,8 +41,8 @@ export class OsmPreferences {
if (str === undefined || str === "") {
return;
}
if(str === null){
console.error("Deleting "+allStartWith);
if (str === null) {
console.error("Deleting " + allStartWith);
let count = parseInt(length.data);
for (let i = 0; i < count; i++) {
// Delete all the preferences
@ -99,7 +97,7 @@ export class OsmPreferences {
public GetPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource<string> {
key = prefix + key;
if(key.length >= 255){
if (key.length >= 255) {
throw "Preferences: key length to big";
}
if (this.preferenceSources[key] !== undefined) {
@ -158,14 +156,14 @@ export class OsmPreferences {
if (v === undefined || v === "") {
this.auth.xhr({
method: 'DELETE',
path: '/api/0.6/user/preferences/' + k,
path: '/api/0.6/user/preferences/' + encodeURIComponent(k),
options: {header: {'Content-Type': 'text/plain'}},
}, function (error) {
if (error) {
console.log("Could not remove preference", error);
return;
}
console.log("Preference ",k,"removed!");
console.log("Preference ", k, "removed!");
});
return;
@ -174,7 +172,7 @@ export class OsmPreferences {
this.auth.xhr({
method: 'PUT',
path: '/api/0.6/user/preferences/' + k,
path: '/api/0.6/user/preferences/' + encodeURIComponent(k),
options: {header: {'Content-Type': 'text/plain'}},
content: v
}, function (error) {