forked from MapComplete/MapComplete
Small tweaks, stabilizing local source cache
This commit is contained in:
parent
f33fe081d0
commit
fa4fb71e06
8 changed files with 53 additions and 37 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue