forked from MapComplete/MapComplete
More refactoring of the featuresources, cleanup, small changes
This commit is contained in:
parent
d144f70ffb
commit
c11ff652b8
7 changed files with 121 additions and 79 deletions
|
@ -1,35 +0,0 @@
|
|||
import FeatureSource from "./FeatureSource";
|
||||
import {UIEventSource} from "../UIEventSource";
|
||||
import LocalStorageSaverActor from "./LocalStorageSaverActor";
|
||||
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig";
|
||||
|
||||
export default class LocalStorageSource implements FeatureSource {
|
||||
public features: UIEventSource<{ feature: any; freshness: Date }[]>;
|
||||
public readonly name = "LocalStorageSource";
|
||||
|
||||
constructor(layout: UIEventSource<LayoutConfig>) {
|
||||
this.features = new UIEventSource<{ feature: any; freshness: Date }[]>([])
|
||||
const key = LocalStorageSaverActor.storageKey + layout.data.id
|
||||
layout.addCallbackAndRun(_ => {
|
||||
try {
|
||||
const fromStorage = localStorage.getItem(key);
|
||||
if (fromStorage == null) {
|
||||
return;
|
||||
}
|
||||
const loaded: { feature: any; freshness: Date | string }[] =
|
||||
JSON.parse(fromStorage);
|
||||
|
||||
const parsed: { feature: any; freshness: Date }[] = loaded.map(ff => ({
|
||||
feature: ff.feature,
|
||||
freshness: typeof ff.freshness == "string" ? new Date(ff.freshness) : ff.freshness
|
||||
}))
|
||||
|
||||
this.features.setData(parsed);
|
||||
console.log("Loaded ", loaded.length, " features from localstorage as cache")
|
||||
} catch (e) {
|
||||
console.log("Could not load features from localStorage:", e)
|
||||
localStorage.removeItem(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
20
Logic/FeatureSource/Sources/StaticFeatureSource.ts
Normal file
20
Logic/FeatureSource/Sources/StaticFeatureSource.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import FeatureSource from "../FeatureSource";
|
||||
import {UIEventSource} from "../../UIEventSource";
|
||||
|
||||
/**
|
||||
* A simple dummy implementation for whenever it is needed
|
||||
*/
|
||||
export default class StaticFeatureSource implements FeatureSource {
|
||||
public readonly features: UIEventSource<{ feature: any; freshness: Date }[]>;
|
||||
public readonly name: string = "StaticFeatureSource"
|
||||
|
||||
constructor(features: any[]) {
|
||||
const now = new Date();
|
||||
this.features = new UIEventSource(features.map(f => ({
|
||||
feature: f,
|
||||
freshness: now
|
||||
})))
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue