| 
									
										
										
										
											2020-08-17 17:23:15 +02:00
										 |  |  | import { UIEventSource } from "../UIEventSource" | 
					
						
							| 
									
										
										
										
											2024-10-17 04:13:27 +02:00
										 |  |  | import { Utils } from "../../Utils" | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-29 14:10:20 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * UIEventsource-wrapper around localStorage | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  | export class LocalStorageSource { | 
					
						
							| 
									
										
										
										
											2024-10-17 02:10:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private static readonly _cache: Record<string, UIEventSource<string>> = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static getParsed<T>(key: string, defaultValue: T): UIEventSource<T> { | 
					
						
							|  |  |  |         return LocalStorageSource.get(key).sync( | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |             (str) => { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 if (str === undefined) { | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |                     return defaultValue | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 try { | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |                     return JSON.parse(str) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 } catch { | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |                     return defaultValue | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             [], | 
					
						
							| 
									
										
										
										
											2024-10-17 02:10:25 +02:00
										 |  |  |             (value) => JSON.stringify(value), | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-17 02:10:25 +02:00
										 |  |  |     static get(key: string, defaultValue: string = undefined): UIEventSource<string> { | 
					
						
							|  |  |  |         const cached = LocalStorageSource._cache[key] | 
					
						
							|  |  |  |         if (cached) { | 
					
						
							|  |  |  |             return cached | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         let saved = defaultValue | 
					
						
							| 
									
										
										
										
											2024-10-17 04:06:03 +02:00
										 |  |  |         if (!Utils.runningFromConsole) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 saved = localStorage.getItem(key) | 
					
						
							|  |  |  |                 if (saved === "undefined") { | 
					
						
							|  |  |  |                     saved = undefined | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 console.error("Could not get value", key, "from local storage") | 
					
						
							| 
									
										
										
										
											2023-12-26 22:30:27 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-10-17 02:10:25 +02:00
										 |  |  |         const source = new UIEventSource<string>(saved ?? defaultValue, "localstorage:" + key) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         source.addCallback((data) => { | 
					
						
							|  |  |  |             if (data === undefined || data === "" || data === null) { | 
					
						
							|  |  |  |                 localStorage.removeItem(key) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 localStorage.setItem(key, data) | 
					
						
							|  |  |  |             } catch (e) { | 
					
						
							|  |  |  |                 // Probably exceeded the quota with this item!
 | 
					
						
							|  |  |  |                 // Let's nuke everything
 | 
					
						
							|  |  |  |                 localStorage.clear() | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         LocalStorageSource._cache[key] = source | 
					
						
							|  |  |  |         return source | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  | } |