| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  | /*** | 
					
						
							|  |  |  |  * Saves all the features that are passed in to localstorage, so they can be retrieved on the next run | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Technically, more an Actor then a featuresource, but it fits more neatly this ay | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-09-21 02:10:42 +02:00
										 |  |  | import {FeatureSourceForLayer} from "../FeatureSource"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  | export default class SaveTileToLocalStorageActor { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |     public static readonly storageKey: string = "cached-features"; | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |     public static readonly formatVersion: string = "1" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |     constructor(source: FeatureSourceForLayer, tileIndex: number) { | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |         source.features.addCallbackAndRunD(features => { | 
					
						
							| 
									
										
										
										
											2021-09-22 05:02:09 +02:00
										 |  |  |             const key = `${SaveTileToLocalStorageActor.storageKey}-${source.layer.layerDef.id}-${tileIndex}` | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |             const now = new Date() | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |                 if (features.length > 0) { | 
					
						
							|  |  |  |                     localStorage.setItem(key, JSON.stringify(features)); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // We _still_ write the time to know that this tile is empty!
 | 
					
						
							|  |  |  |                 SaveTileToLocalStorageActor.MarkVisited(source.layer.layerDef.id, tileIndex, now) | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 console.warn("Could not save the features to local storage:", e) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 04:13:23 +02:00
										 |  |  |     public static MarkVisited(layerId: string, tileId: number, freshness: Date){ | 
					
						
							|  |  |  |         const key = `${SaveTileToLocalStorageActor.storageKey}-${layerId}-${tileId}` | 
					
						
							|  |  |  |         localStorage.setItem(key + "-time", JSON.stringify(freshness.getTime())) | 
					
						
							|  |  |  |         localStorage.setItem(key + "-format", SaveTileToLocalStorageActor.formatVersion) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-20 17:14:55 +02:00
										 |  |  | } |