| 
									
										
										
										
											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-01-15 00:29:07 +01:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-01-15 01:57:46 +01:00
										 |  |  |             const fromStorage = localStorage.getItem(key); | 
					
						
							| 
									
										
										
										
											2021-01-15 00:29:07 +01:00
										 |  |  |             if (fromStorage == null) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const loaded = JSON.parse(fromStorage); | 
					
						
							|  |  |  |             this.features.setData(loaded); | 
					
						
							| 
									
										
										
										
											2021-01-15 01:57:46 +01:00
										 |  |  |             console.log("Loaded ",loaded.length," features from localstorage as cache") | 
					
						
							| 
									
										
										
										
											2021-01-15 00:29:07 +01:00
										 |  |  |         } catch (e) { | 
					
						
							|  |  |  |             console.log("Could not load features from localStorage:", e) | 
					
						
							| 
									
										
										
										
											2021-01-15 01:57:46 +01:00
										 |  |  |             localStorage.removeItem(key) | 
					
						
							| 
									
										
										
										
											2021-01-15 00:29:07 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |