Skip to content

Commit

Permalink
Rename of method and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissainty committed Jul 23, 2020
1 parent 0bfbc7d commit 06d494a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 58 deletions.
1 change: 0 additions & 1 deletion Blazored.LocalStorage.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorServerSide", "samples
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9170D7A9-70CE-48E3-88A3-F11D2983103E}"
ProjectSection(SolutionItems) = preProject
azure-pipelines.yml = azure-pipelines.yml
README.md = README.md
EndProjectSection
EndProject
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Blazored LocalStorage
A library to provide access to local storage in Blazor applications

[![Build Status](https://dev.azure.com/blazored/LocalStorage/_apis/build/status/Blazored.LocalStorage?branchName=master)](https://dev.azure.com/blazored/LocalStorage/_build/latest?definitionId=1&branchName=master)
![Build & Test Main](https://github.com/Blazored/LocalStorage/workflows/Build%20&%20Test%20Main/badge.svg)

[![Nuget](https://img.shields.io/nuget/v/blazored.localstorage.svg)](https://www.nuget.org/packages/Blazored.LocalStorage/)

Expand Down Expand Up @@ -121,6 +121,7 @@ The APIs available are:
- asynchronous via `ILocalStorageService`:
- SetItemAsync()
- GetItemAsync()
- GetItemAsStringAsync()
- RemoveItemAsync()
- ClearAsync()
- LengthAsync()
Expand All @@ -130,10 +131,13 @@ The APIs available are:
- synchronous via `ISyncLocalStorageService` (Synchronous methods are **only** available in Blazor WebAssembly):
- SetItem()
- GetItem()
- GetItemAsString()
- RemoveItem()
- Clear()
- Length()
- Key()
- ContainsKey()

**Note:** Blazored.LocalStorage methods will handle the serialisation and de-serialisation of the data for you.
**Note:** Blazored.LocalStorage methods will handle the serialisation and de-serialisation of the data for you, the exception is the `GetItemAsString[Async]` method.

If you want to handle serialising and de-serialising yourself, serialise the data to a string and save using the `SetItem[Async]` method, as normal -- This method will not attempt to serialise a string value. You can then read out the data using the `GetItemAsString[Async]` method and de-serialise it yourself.
2 changes: 1 addition & 1 deletion samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

async Task GetStringFromLocalStorage()
{
StringFromLocalStorage = await localStorage.GetStringAsync("name");
StringFromLocalStorage = await localStorage.GetItemAsStringAsync("name");

if (string.IsNullOrEmpty(StringFromLocalStorage))
{
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorWebAssembly/Pages/Sync.razor
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@

void GetStringFromLocalStorage()
{
StringFromLocalStorage = localStorage.GetString("name");
StringFromLocalStorage = localStorage.GetItemAsString("name");

if (string.IsNullOrEmpty(StringFromLocalStorage))
{
Expand Down
58 changes: 46 additions & 12 deletions src/Blazored.LocalStorage/ILocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,61 @@ namespace Blazored.LocalStorage
{
public interface ILocalStorageService
{
Task ClearAsync();
/// <summary>
/// Clears all data from local storage.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask ClearAsync();

Task<T> GetItemAsync<T>(string key);
/// <summary>
/// Retrieve the specified data from local storage and deseralise it to the specfied type.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the local storage slot to use</param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<T> GetItemAsync<T>(string key);

Task<String> GetStringAsync(string key);
/// <summary>
/// Retrieve the specified data from local storage as a <see cref="string"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<string> GetItemAsStringAsync(string key);

Task<string> KeyAsync(int index);
/// <summary>
/// Return the name of the key at the specified <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<string> KeyAsync(int index);

/// <summary>
/// Checks if the key exists in local storage but does not check the value.
/// Checks if the <paramref name="key"/> exists in local storage, but does not check its value.
/// </summary>
/// <param name="key">name of the key</param>
/// <returns>True if the key exist, false otherwise</returns>
Task<bool> ContainKeyAsync(string key);

Task<int> LengthAsync();
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<bool> ContainKeyAsync(string key);

Task RemoveItemAsync(string key);
/// <summary>
/// The number of items stored in local storage.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<int> LengthAsync();

Task SetItemAsync<T>(string key, T data);
/// <summary>
/// Remove the data with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask RemoveItemAsync(string key);

/// <summary>
/// Sets or updates the <paramref name="data"/> in local storage with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <param name="data">The data to be saved</param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask SetItemAsync<T>(string key, T data);

event EventHandler<ChangingEventArgs> Changing;
event EventHandler<ChangedEventArgs> Changed;
}
Expand Down
41 changes: 36 additions & 5 deletions src/Blazored.LocalStorage/ISyncLocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,56 @@ namespace Blazored.LocalStorage
{
public interface ISyncLocalStorageService
{
/// <summary>
/// Clears all data from local storage.
/// </summary>
void Clear();

/// <summary>
/// Retrieve the specified data from local storage as a <typeparamref name="T"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the local storage slot to use</param>
/// <returns>The data from the specified <paramref name="key"/> as a <typeparamref name="T"/></returns>
T GetItem<T>(string key);

string GetString(string key);

/// <summary>
/// Retrieve the specified data from local storage as a <see cref="string"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <returns>The data associated with the specified <paramref name="key"/> as a <see cref="string"/></returns>
string GetItemAsString(string key);

/// <summary>
/// Return the name of the key at the specified <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns>The name of the key at the specified <paramref name="index"/></returns>
string Key(int index);

/// <summary>
/// Checks if the key exists in local storage but does not check the value.
/// Checks if the <paramref name="key"/> exists in local storage, but does not check its value.
/// </summary>
/// <param name="key">name of the key</param>
/// <returns>True if the key exist, false otherwise</returns>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <returns>Boolean indicating if the specified <paramref name="key"/> exists</returns>
bool ContainKey(string key);

/// <summary>
/// The number of items stored in local storage.
/// </summary>
/// <returns>The number of items stored in local storage</returns>
int Length();

/// <summary>
/// Remove the data with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
void RemoveItem(string key);

/// <summary>
/// Sets or updates the <paramref name="data"/> in local storage with the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <param name="data">The data to be saved</param>
void SetItem<T>(string key, T data);

event EventHandler<ChangingEventArgs> Changing;
Expand Down
48 changes: 12 additions & 36 deletions src/Blazored.LocalStorage/LocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public LocalStorageService(IJSRuntime jSRuntime, IOptions<LocalStorageOptions> o
_jSInProcessRuntime = jSRuntime as IJSInProcessRuntime;
}

public async Task SetItemAsync<T>(string key, T data)
public async ValueTask SetItemAsync<T>(string key, T data)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -43,7 +43,7 @@ public async Task SetItemAsync<T>(string key, T data)
RaiseOnChanged(key, e.OldValue, data);
}

public async Task<T> GetItemAsync<T>(string key)
public async ValueTask<T> GetItemAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -65,29 +65,29 @@ public async Task<T> GetItemAsync<T>(string key)
}
}

public async Task<string> GetStringAsync(string key)
public async ValueTask<string> GetItemAsStringAsync(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));

return await _jSRuntime.InvokeAsync<string>("localStorage.getItem", key);
}

public async Task RemoveItemAsync(string key)
public async ValueTask RemoveItemAsync(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));

await _jSRuntime.InvokeVoidAsync("localStorage.removeItem", key);
}

public async Task ClearAsync() => await _jSRuntime.InvokeVoidAsync("localStorage.clear");
public async ValueTask ClearAsync() => await _jSRuntime.InvokeVoidAsync("localStorage.clear");

public async Task<int> LengthAsync() => await _jSRuntime.InvokeAsync<int>("eval", "localStorage.length");
public async ValueTask<int> LengthAsync() => await _jSRuntime.InvokeAsync<int>("eval", "localStorage.length");

public async Task<string> KeyAsync(int index) => await _jSRuntime.InvokeAsync<string>("localStorage.key", index);
public async ValueTask<string> KeyAsync(int index) => await _jSRuntime.InvokeAsync<string>("localStorage.key", index);

public async Task<bool> ContainKeyAsync(string key) => await _jSRuntime.InvokeAsync<bool>("localStorage.hasOwnProperty", key);
public async ValueTask<bool> ContainKeyAsync(string key) => await _jSRuntime.InvokeAsync<bool>("localStorage.hasOwnProperty", key);

public void SetItem<T>(string key, T data)
{
Expand Down Expand Up @@ -138,7 +138,7 @@ public T GetItem<T>(string key)
return (T)(object)serialisedData;
}
}
public string GetString(string key)
public string GetItemAsString(string key)

{
if (string.IsNullOrEmpty(key))
Expand Down Expand Up @@ -208,27 +208,6 @@ private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object da
return e;
}

private async Task<T> GetItemInternalAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));

var serialisedData = await _jSRuntime.InvokeAsync<string>("localStorage.getItem", key);

if (string.IsNullOrWhiteSpace(serialisedData))
return default;

if (serialisedData.StartsWith("{") && serialisedData.EndsWith("}")
|| serialisedData.StartsWith("\"") && serialisedData.EndsWith("\""))
{
return JsonSerializer.Deserialize<T>(serialisedData, _jsonOptions);
}
else
{
return (T)(object)serialisedData;
}
}

private ChangingEventArgs RaiseOnChangingSync(string key, object data)
{
var e = new ChangingEventArgs
Expand All @@ -243,15 +222,12 @@ private ChangingEventArgs RaiseOnChangingSync(string key, object data)
return e;
}

public T GetItemInternal<T>(string key)
private async Task<T> GetItemInternalAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));

if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");

var serialisedData = _jSInProcessRuntime.Invoke<string>("localStorage.getItem", key);
var serialisedData = await _jSRuntime.InvokeAsync<string>("localStorage.getItem", key);

if (string.IsNullOrWhiteSpace(serialisedData))
return default;
Expand All @@ -267,7 +243,7 @@ public T GetItemInternal<T>(string key)
}
}

public object GetItemInternal(string key)
private object GetItemInternal(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
Expand Down

0 comments on commit 06d494a

Please sign in to comment.