2021-01-15 00:29:07 +01:00
|
|
|
import FeatureSource from "./FeatureSource";
|
|
|
|
import {UIEventSource} from "../UIEventSource";
|
|
|
|
import LocalStorageSaver from "./LocalStorageSaver";
|
2021-01-15 01:57:46 +01:00
|
|
|
import LayoutConfig from "../../Customizations/JSON/LayoutConfig";
|
2021-01-15 00:29:07 +01:00
|
|
|
|
|
|
|
export default class LocalStorageSource implements FeatureSource {
|
|
|
|
public features: UIEventSource<{ feature: any; freshness: Date }[]>;
|
|
|
|
|
2021-01-15 01:57:46 +01:00
|
|
|
constructor(layout: UIEventSource<LayoutConfig>) {
|
2021-01-15 00:29:07 +01:00
|
|
|
this.features = new UIEventSource<{ feature: any; freshness: Date }[]>([])
|
2021-01-15 01:57:46 +01:00
|
|
|
const key = LocalStorageSaver.storageKey + layout.data.id
|
2021-02-20 01:45:51 +01:00
|
|
|
layout.addCallbackAndRun(_ => {
|
|
|
|
|
2021-01-15 00:29:07 +01:00
|
|
|
|
2021-02-20 01:45:51 +01:00
|
|
|
try {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2021-01-15 00:29:07 +01:00
|
|
|
}
|
|
|
|
}
|