1+ using System . Buffers . Text ;
12using System . Text ;
23using System . Text . Json ;
34using Microsoft . JSInterop ;
@@ -16,65 +17,83 @@ public JsStorageService(IJSRuntime jsRuntime)
1617
1718 public T RetrieveItem < T > ( string storageKey , StorageLocation storage , T defaultIfNull )
1819 {
19- var base64String = storage switch
20+ var stringFromCache = storage switch
2021 {
2122 StorageLocation . BrowserLocal => ( ( IJSInProcessRuntime ) _jsRuntime ) . Invoke < string ? > ( "localStorage.getItem" , storageKey ) ?? string . Empty ,
2223 StorageLocation . BrowserSession => ( ( IJSInProcessRuntime ) _jsRuntime ) . Invoke < string ? > ( "sessionStorage.getItem" , storageKey ) ?? string . Empty ,
2324 _ => _pageCache [ storageKey ]
2425 } ;
25- if ( string . IsNullOrEmpty ( base64String ) ) return defaultIfNull ;
26- var utf8Byes = Convert . FromBase64String ( base64String ) ;
27- var jsonString = Encoding . UTF8 . GetString ( utf8Byes ) ;
26+ if ( string . IsNullOrEmpty ( stringFromCache ) ) return defaultIfNull ;
27+
28+ string jsonString ;
29+ if ( Base64 . IsValid ( stringFromCache ) )
30+ {
31+ var utf8Byes = Convert . FromBase64String ( stringFromCache ) ;
32+ jsonString = Encoding . UTF8 . GetString ( utf8Byes ) ;
33+ }
34+ else
35+ {
36+ jsonString = stringFromCache ;
37+ }
2838
2939 if ( string . IsNullOrEmpty ( jsonString ) ) return defaultIfNull ;
3040
3141 return JsonSerializer . Deserialize < T > ( jsonString ) ?? defaultIfNull ;
3242 }
3343
34- public async Task StoreItem < T > ( string storageKey , StorageLocation storageLocation , T itemToStore )
44+ public async Task < T ? > RetrieveItem < T > ( string storageKey , StorageLocation storageLocation , CancellationToken clt = default )
45+ {
46+ var stringFromCache = storageLocation switch
47+ {
48+ StorageLocation . BrowserLocal => await _jsRuntime . InvokeAsync < string ? > ( "localStorage.getItem" , clt , storageKey ) ?? string . Empty ,
49+ StorageLocation . BrowserSession => await _jsRuntime . InvokeAsync < string ? > ( "sessionStorage.getItem" , clt , storageKey ) ?? string . Empty ,
50+ _ => _pageCache . TryGetValue ( storageKey , out string ? cachedItem ) ? cachedItem : string . Empty
51+ } ;
52+ string jsonString ;
53+ if ( Base64 . IsValid ( stringFromCache ) )
54+ {
55+ var utf8Byes = Convert . FromBase64String ( stringFromCache ) ;
56+ jsonString = Encoding . UTF8 . GetString ( utf8Byes ) ;
57+ }
58+ else
59+ {
60+ jsonString = stringFromCache ;
61+ }
62+
63+ if ( string . IsNullOrEmpty ( jsonString ) ) return default ;
64+
65+ return JsonSerializer . Deserialize < T > ( jsonString ) ?? default ;
66+ }
67+
68+ public async Task StoreItem < T > ( string storageKey , StorageLocation storageLocation , T itemToStore , CancellationToken clt = default )
3569 {
3670 var utf8Byes = JsonSerializer . SerializeToUtf8Bytes < T > ( itemToStore ) ;
3771 var base64String = Convert . ToBase64String ( utf8Byes ) ;
3872
3973 switch ( storageLocation )
4074 {
4175 case StorageLocation . BrowserLocal :
42- await _jsRuntime . InvokeVoidAsync ( "localStorage.setItem" , storageKey , base64String ) ;
76+ await _jsRuntime . InvokeVoidAsync ( "localStorage.setItem" , clt , storageKey , base64String ) ;
4377 break ;
4478 case StorageLocation . BrowserSession :
45- await _jsRuntime . InvokeVoidAsync ( "sessionStorage.setItem" , storageKey , base64String ) ;
79+ await _jsRuntime . InvokeVoidAsync ( "sessionStorage.setItem" , clt , storageKey , base64String ) ;
4680 break ;
4781 default :
4882 _pageCache [ storageKey ] = base64String ;
4983 break ;
5084 }
5185 }
5286
53- public async Task < T ? > RetrieveItem < T > ( string storageKey , StorageLocation storageLocation )
54- {
55- var base64String = storageLocation switch
56- {
57- StorageLocation . BrowserLocal => await _jsRuntime . InvokeAsync < string ? > ( "localStorage.getItem" , storageKey ) ?? string . Empty ,
58- StorageLocation . BrowserSession => await _jsRuntime . InvokeAsync < string ? > ( "sessionStorage.getItem" , storageKey ) ?? string . Empty ,
59- _ => _pageCache . TryGetValue ( storageKey , out string ? cachedItem ) ? cachedItem : string . Empty
60- } ;
61- if ( string . IsNullOrEmpty ( base64String ) ) return default ;
62- var utf8Byes = Convert . FromBase64String ( base64String ) ;
63- var jsonString = Encoding . UTF8 . GetString ( utf8Byes ) ;
64-
65- return string . IsNullOrEmpty ( jsonString ) ? default : JsonSerializer . Deserialize < T > ( jsonString ) ;
66- }
67-
6887
69- public async Task RemoveItem ( string storageKey , StorageLocation storageLocation )
88+ public async Task RemoveItem ( string storageKey , StorageLocation storageLocation , CancellationToken clt = default )
7089 {
7190 switch ( storageLocation )
7291 {
7392 case StorageLocation . BrowserLocal :
74- await _jsRuntime . InvokeVoidAsync ( "localStorage.removeItem" , storageKey ) ;
93+ await _jsRuntime . InvokeVoidAsync ( "localStorage.removeItem" , clt , storageKey ) ;
7594 break ;
7695 case StorageLocation . BrowserSession :
77- await _jsRuntime . InvokeVoidAsync ( "sessionStorage.removeItem" , storageKey ) ;
96+ await _jsRuntime . InvokeVoidAsync ( "sessionStorage.removeItem" , clt , storageKey ) ;
7897 break ;
7998 default :
8099 _pageCache . Remove ( storageKey ) ;
0 commit comments