| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { UIEventSource } from "../UIEventSource" | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     static GetParsed<T>(key: string, defaultValue: T): UIEventSource<T> { | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |         return LocalStorageSource.Get(key).sync( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +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 | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             [], | 
					
						
							|  |  |  |             (value) => JSON.stringify(value) | 
					
						
							| 
									
										
										
										
											2021-07-10 15:52:52 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static Get(key: string, defaultValue: string = undefined): UIEventSource<string> { | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             const saved = localStorage.getItem(key) | 
					
						
							|  |  |  |             const source = new UIEventSource<string>(saved ?? defaultValue, "localstorage:" + key) | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  |             source.addCallback((data) => { | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 try { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     localStorage.setItem(key, data) | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 } catch (e) { | 
					
						
							| 
									
										
										
										
											2021-03-29 14:10:20 +02:00
										 |  |  |                     // Probably exceeded the quota with this item!
 | 
					
						
							|  |  |  |                     // Lets nuke everything
 | 
					
						
							|  |  |  |                     localStorage.clear() | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             }) | 
					
						
							|  |  |  |             return source | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  |         } catch (e) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             return new UIEventSource<string>(defaultValue) | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-24 17:53:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-31 17:11:44 +02:00
										 |  |  | } |