Skip to content

Commit efea3ef

Browse files
committed
Fix some strange code
1 parent ae3759c commit efea3ef

3 files changed

Lines changed: 42 additions & 95 deletions

File tree

Src/Drogecode.Blazor.ExpireStorage/Drogecode.Blazor.ExpireStorage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<RootNamespace>Drogecode.Blazor.ExpireStorage</RootNamespace>
8-
<Version>1.0.0-rc3</Version>
8+
<Version>1.0.0-rc4</Version>
99
<Title>Drogecode.Blazor.ExpireStorage</Title>
1010
<Authors>Taco Droogers</Authors>
1111
<PackageProjectUrl>https://github.com/Drogecode/Drogecode.Blazor.ExpireStorage</PackageProjectUrl>

Src/Drogecode.Blazor.ExpireStorage/Services/ExpireStorageService.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,38 +82,18 @@ public ExpireStorageService(
8282

8383
if ((request.CachedAndReplace || request.OneCallPerLocalStorage || (IsOffline && request.CacheWhenOffline)) && !request.IgnoreCache)
8484
{
85-
var cacheResult = await _localStorageExpireService.GetItemAsync<TRes?>(cacheKey, clt);
86-
if (cacheResult is not null)
85+
var storageResult = await _localStorageExpireService.GetItemAsync<TRes?>(cacheKey, clt);
86+
if (storageResult is not null)
8787
{
88-
return BuildResponse(cacheResult, HandledBy.LocalStorage);
88+
return BuildResponse(storageResult, HandledBy.LocalStorage);
8989
}
9090
}
9191

9292
if (!request.CachedAndReplace)
9393
{
9494
return await RunSaveAndReturn(cacheKey, function, request, clt);
9595
}
96-
}
97-
catch (HttpRequestException)
98-
{
99-
ConsoleHelper.WriteLine("a HttpRequestException");
100-
IsOffline = true;
101-
}
102-
catch (TaskCanceledException)
103-
{
104-
}
105-
catch (Exception ex)
106-
{
107-
ConsoleHelper.WriteLine(ex);
108-
}
109-
110-
if (request.IgnoreCache)
111-
{
112-
return BuildResponse(defaultResponse, HandledBy.Default);
113-
}
114-
115-
try
116-
{
96+
11797
var cacheResult = await _localStorageExpireService.GetItemAsync<TRes?>(cacheKey, clt);
11898
if (cacheResult is not null)
11999
{
@@ -131,7 +111,7 @@ public ExpireStorageService(
131111
}
132112
catch (HttpRequestException)
133113
{
134-
ConsoleHelper.WriteLine("b HttpRequestException");
114+
ConsoleHelper.WriteLine("HttpRequestException");
135115
IsOffline = true;
136116
}
137117
catch (TaskCanceledException)
Lines changed: 36 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Text.Json;
2-
using Drogecode.Blazor.ExpireStorage.Helpers;
32
using Microsoft.JSInterop;
43

54
namespace 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

Comments
 (0)