11using System . Text . Json ;
2- using Drogecode . Blazor . ExpireStorage . Helpers ;
32using Microsoft . JSInterop ;
43
54namespace Drogecode . Blazor . ExpireStorage ;
@@ -16,94 +15,62 @@ public JsStorageService(IJSRuntime jsRuntime)
1615
1716 public T RetrieveItem < T > ( string storageKey , StorageLocation storage , T defaultIfNull )
1817 {
19- try
18+ var stringFromCache = storage switch
2019 {
21- var stringFromCache = storage switch
22- {
23- StorageLocation . BrowserLocal => ( ( IJSInProcessRuntime ) _jsRuntime ) . Invoke < string ? > ( "localStorage.getItem" , storageKey ) ?? string . Empty ,
24- StorageLocation . BrowserSession => ( ( IJSInProcessRuntime ) _jsRuntime ) . Invoke < string ? > ( "sessionStorage.getItem" , storageKey ) ?? string . Empty ,
25- _ => _pageCache [ storageKey ]
26- } ;
27- if ( string . IsNullOrEmpty ( stringFromCache ) ) return defaultIfNull ;
20+ StorageLocation . BrowserLocal => ( ( IJSInProcessRuntime ) _jsRuntime ) . Invoke < string ? > ( "localStorage.getItem" , storageKey ) ?? string . Empty ,
21+ StorageLocation . BrowserSession => ( ( IJSInProcessRuntime ) _jsRuntime ) . Invoke < string ? > ( "sessionStorage.getItem" , storageKey ) ?? string . Empty ,
22+ _ => _pageCache [ storageKey ]
23+ } ;
24+ if ( string . IsNullOrEmpty ( stringFromCache ) ) return defaultIfNull ;
2825
29- return JsonSerializer . Deserialize < T > ( stringFromCache ) ?? defaultIfNull ;
30- }
31- catch ( Exception ex )
32- {
33- ConsoleHelper . WriteLine ( "Exception in RetrieveItem" , ex ) ;
34- return defaultIfNull ;
35- }
26+ return JsonSerializer . Deserialize < T > ( stringFromCache ) ?? defaultIfNull ;
3627 }
3728
3829 public async Task < T ? > RetrieveItem < T > ( string storageKey , StorageLocation storageLocation , CancellationToken clt = default )
3930 {
40- try
31+ var stringFromCache = storageLocation switch
4132 {
42- var stringFromCache = storageLocation switch
43- {
44- StorageLocation . BrowserLocal => await _jsRuntime . InvokeAsync < string ? > ( "localStorage.getItem" , clt , storageKey ) ?? string . Empty ,
45- StorageLocation . BrowserSession => await _jsRuntime . InvokeAsync < string ? > ( "sessionStorage.getItem" , clt , storageKey ) ?? string . Empty ,
46- _ => _pageCache . TryGetValue ( storageKey , out string ? cachedItem ) ? cachedItem : string . Empty
47- } ;
33+ StorageLocation . BrowserLocal => await _jsRuntime . InvokeAsync < string ? > ( "localStorage.getItem" , clt , storageKey ) ?? string . Empty ,
34+ StorageLocation . BrowserSession => await _jsRuntime . InvokeAsync < string ? > ( "sessionStorage.getItem" , clt , storageKey ) ?? string . Empty ,
35+ _ => _pageCache . TryGetValue ( storageKey , out string ? cachedItem ) ? cachedItem : string . Empty
36+ } ;
4837
49- if ( string . IsNullOrEmpty ( stringFromCache ) ) return default ;
38+ if ( string . IsNullOrEmpty ( stringFromCache ) ) return default ;
5039
51- return JsonSerializer . Deserialize < T > ( stringFromCache ) ?? default ;
52- }
53-
54- catch ( Exception ex )
55- {
56- ConsoleHelper . WriteLine ( "Exception in RetrieveItem (async)" , ex ) ;
57- return default ;
58- }
40+ return JsonSerializer . Deserialize < T > ( stringFromCache ) ?? default ;
5941 }
6042
6143 public async Task StoreItem < T > ( string storageKey , StorageLocation storageLocation , T itemToStore , CancellationToken clt = default )
6244 {
63- try
64- {
65- var asString = JsonSerializer . Serialize ( itemToStore ) ;
66-
67- switch ( storageLocation )
68- {
69- case StorageLocation . BrowserLocal :
70- await _jsRuntime . InvokeVoidAsync ( "localStorage.setItem" , clt , storageKey , asString ) ;
71- break ;
72- case StorageLocation . BrowserSession :
73- await _jsRuntime . InvokeVoidAsync ( "sessionStorage.setItem" , clt , storageKey , asString ) ;
74- break ;
75- default :
76- _pageCache [ storageKey ] = asString ;
77- break ;
78- }
79- }
45+ var asString = JsonSerializer . Serialize ( itemToStore ) ;
8046
81- catch ( Exception ex )
47+ switch ( storageLocation )
8248 {
83- ConsoleHelper . WriteLine ( "Exception in StoreItem" , ex ) ;
49+ case StorageLocation . BrowserLocal :
50+ await _jsRuntime . InvokeVoidAsync ( "localStorage.setItem" , clt , storageKey , asString ) ;
51+ break ;
52+ case StorageLocation . BrowserSession :
53+ await _jsRuntime . InvokeVoidAsync ( "sessionStorage.setItem" , clt , storageKey , asString ) ;
54+ break ;
55+ default :
56+ _pageCache [ storageKey ] = asString ;
57+ break ;
8458 }
8559 }
8660
8761 public async Task RemoveItem ( string storageKey , StorageLocation storageLocation , CancellationToken clt = default )
8862 {
89- try
90- {
91- switch ( storageLocation )
92- {
93- case StorageLocation . BrowserLocal :
94- await _jsRuntime . InvokeVoidAsync ( "localStorage.removeItem" , clt , storageKey ) ;
95- break ;
96- case StorageLocation . BrowserSession :
97- await _jsRuntime . InvokeVoidAsync ( "sessionStorage.removeItem" , clt , storageKey ) ;
98- break ;
99- default :
100- _pageCache . Remove ( storageKey ) ;
101- break ;
102- }
103- }
104- catch ( Exception ex )
63+ switch ( storageLocation )
10564 {
106- ConsoleHelper . WriteLine ( "Exception in RemoveItem" , ex ) ;
65+ case StorageLocation . BrowserLocal :
66+ await _jsRuntime . InvokeVoidAsync ( "localStorage.removeItem" , clt , storageKey ) ;
67+ break ;
68+ case StorageLocation . BrowserSession :
69+ await _jsRuntime . InvokeVoidAsync ( "sessionStorage.removeItem" , clt , storageKey ) ;
70+ break ;
71+ default :
72+ _pageCache . Remove ( storageKey ) ;
73+ break ;
10774 }
10875 }
109- }
76+ }
0 commit comments