Skip to content

Commit 501bc38

Browse files
aesalazargalvesribeiro
authored andcommitted
send storage values to javascriopt as objects rather than strings (#10)
1 parent b1cce57 commit 501bc38

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

src/Blazor.Extensions.Storage.JS/src/BrowserStorage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class BrowserStorage implements IBrowserStorage {
2626
return null;
2727
};
2828

29-
public SetItem(storage: string, key: string, keyValue: string): void {
30-
window[storage].setItem(key, keyValue);
29+
public SetItem(storage: string, key: string, keyValue: any): void {
30+
window[storage].setItem(key, JSON.stringify(keyValue));
3131
};
3232

3333
public RemoveItem(storage: string, key: string): void {

src/Blazor.Extensions.Storage/LocalStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Task SetItem<TItem>(string key, TItem item)
3030
{
3131
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
3232

33-
return JSRuntime.Current.InvokeAsync<object>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key, Json.Serialize(item));
33+
return JSRuntime.Current.InvokeAsync<object>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.LOCAL_STORAGE, key, item);
3434
}
3535
}
3636
}

src/Blazor.Extensions.Storage/SessionStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Task SetItem<TItem>(string key, TItem item)
3030
{
3131
if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key));
3232

33-
return JSRuntime.Current.InvokeAsync<object>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.SESSION_STORAGE, key, Json.Serialize(item));
33+
return JSRuntime.Current.InvokeAsync<TItem>(MethodNames.SET_ITEM_METHOD, StorageTypeNames.SESSION_STORAGE, key, item);
3434
}
3535
}
3636
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.JSInterop;
3+
4+
namespace Blazor.Extensions.Storage.Test.Interop
5+
{
6+
/// <summary>
7+
/// Interop calls to JavaScript Local and Session storage via Blazor's <see cref="JSRuntime"/>.
8+
/// </summary>
9+
public class InteropStorage
10+
{
11+
/// <summary>
12+
/// Returns a session storage value.
13+
/// </summary>
14+
/// <param name="key">Key associated with stroed value.</param>
15+
/// <returns>Task that returns the stored value as a string.</returns>
16+
public async Task<string> GetSessionStorage(string key)
17+
{
18+
return await JSRuntime.Current.InvokeAsync<string>(
19+
"getSessionStorage",
20+
key
21+
);
22+
}
23+
24+
/// <summary>
25+
/// Returns a local storage value.
26+
/// </summary>
27+
/// <param name="key">Key associated with stroed value.</param>
28+
/// <returns>Task that returns the stored value as a string.</returns>
29+
public async Task<string> GetLocalStorage(string key)
30+
{
31+
return await JSRuntime.Current.InvokeAsync<string>(
32+
"getLocalStorage",
33+
key
34+
);
35+
}
36+
}
37+
}

test/Blazor.Extensions.Storage.Test/Pages/Index.cshtml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
@inject LocalStorage LocalStorage
44
@inject HttpClient Http
55
@inject ILogger<Index> logger
6+
@using Blazor.Extensions.Storage.Test.Interop
67

78
<h1>Session and Local Storage Test, Open up your browser devtools to see the logs!</h1>
8-
99
@functions {
10-
WeatherForecast[] forecasts;
10+
private WeatherForecast[] forecasts;
11+
private readonly InteropStorage interopStorage = new InteropStorage();
1112

1213
protected override async Task OnInitAsync()
1314
{
@@ -17,6 +18,18 @@
1718
{
1819
await SessionStorage.SetItem<WeatherForecast[]>(key, forecasts);
1920
await LocalStorage.SetItem<WeatherForecast[]>(key, forecasts);
21+
22+
var fromIopSession = await interopStorage.GetSessionStorage(key);
23+
var fromIopLocal = await interopStorage.GetLocalStorage(key);
24+
25+
var iopSessionHasQuotes = fromIopSession.StartsWith("\"") || fromIopSession.EndsWith("\"");
26+
var iopLocalHasQuotes = fromIopLocal.StartsWith("\"") || fromIopLocal.EndsWith("\"");
27+
28+
logger.LogInformation($"Interop get from session storage contains extra quotes: {iopSessionHasQuotes}");
29+
logger.LogInformation(fromIopSession);
30+
logger.LogInformation($"Interop get from local storage contains extra quotes: {iopLocalHasQuotes}");
31+
logger.LogInformation(fromIopLocal);
32+
2033
var fromSession = await SessionStorage.GetItem<WeatherForecast[]>(key);
2134
var fromLocal = await LocalStorage.GetItem<WeatherForecast[]>(key);
2235

test/Blazor.Extensions.Storage.Test/wwwroot/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,16 @@
1010
<app>Loading...</app>
1111

1212
<script src="_framework/blazor.webassembly.js"></script>
13+
<script type="text/javascript">
14+
15+
function getLocalStorage(key) {
16+
return localStorage.getItem(key);
17+
}
18+
19+
function getSessionStorage(key) {
20+
return sessionStorage.getItem(key);
21+
}
22+
23+
</script>
1324
</body>
1425
</html>

0 commit comments

Comments
 (0)