Skip to content

Commit

Permalink
Added missing cancellation token support
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissainty committed Oct 8, 2021
1 parent 27cac1d commit fbc31d4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
14 changes: 7 additions & 7 deletions src/Blazored.SessionStorage/BrowserStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ public BrowserStorageProvider(IJSRuntime jSRuntime)
}

public ValueTask ClearAsync(CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeVoidAsync("sessionStorage.clear");
=> _jSRuntime.InvokeVoidAsync("sessionStorage.clear", cancellationToken ?? CancellationToken.None);

public ValueTask<string> GetItemAsync(string key, CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<string>("sessionStorage.getItem", key);
=> _jSRuntime.InvokeAsync<string>("sessionStorage.getItem", cancellationToken ?? CancellationToken.None, key);

public ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<string>("sessionStorage.key", index);
=> _jSRuntime.InvokeAsync<string>("sessionStorage.key", cancellationToken ?? CancellationToken.None, index);

public ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<bool>("sessionStorage.hasOwnProperty", key);
=> _jSRuntime.InvokeAsync<bool>("sessionStorage.hasOwnProperty", cancellationToken ?? CancellationToken.None, key);

public ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<int>("eval", "sessionStorage.length");
=> _jSRuntime.InvokeAsync<int>("eval", cancellationToken ?? CancellationToken.None, "sessionStorage.length");

public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeVoidAsync("sessionStorage.removeItem", key);
=> _jSRuntime.InvokeVoidAsync("sessionStorage.removeItem", cancellationToken ?? CancellationToken.None, key);

public ValueTask SetItemAsync(string key, string data, CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", key, data);
=> _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", cancellationToken ?? CancellationToken.None, key, data);

public void Clear()
{
Expand Down
19 changes: 10 additions & 9 deletions src/Blazored.SessionStorage/ISessionStorageService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Blazored.SessionStorage
Expand All @@ -9,64 +10,64 @@ public interface ISessionStorageService
/// Clears all data from session storage.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask ClearAsync();
ValueTask ClearAsync(CancellationToken? cancellationToken = null);

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

/// <summary>
/// Retrieve the specified data from session 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);
ValueTask<string> GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null);

/// <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);
ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null);

/// <summary>
/// Checks if the <paramref name="key"/> exists in session storage, but does not check its value.
/// </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<bool> ContainKeyAsync(string key);
ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null);

/// <summary>
/// The number of items stored in session storage.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<int> LengthAsync();
ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null);

/// <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);
ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null);

/// <summary>
/// Sets or updates the <paramref name="data"/> in session 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);
ValueTask SetItemAsync<T>(string key, T data, CancellationToken? cancellationToken = null);

/// <summary>
/// Sets or updates the <paramref name="data"/> in session storage with the specified <paramref name="key"/>. Does not serialize the value before storing.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <param name="data">The string to be saved</param>
/// <returns></returns>
ValueTask SetItemAsStringAsync(string key, string data);
ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null);

event EventHandler<ChangingEventArgs> Changing;
event EventHandler<ChangedEventArgs> Changed;
Expand Down
37 changes: 19 additions & 18 deletions src/Blazored.SessionStorage/SessionStorageService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Blazored.SessionStorage.Serialization;

Expand All @@ -16,7 +17,7 @@ public SessionStorageService(IStorageProvider storageProvider, IJsonSerializer s
_serializer = serializer;
}

public async ValueTask SetItemAsync<T>(string key, T data)
public async ValueTask SetItemAsync<T>(string key, T data, CancellationToken? cancellationToken = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -27,12 +28,12 @@ public async ValueTask SetItemAsync<T>(string key, T data)
return;

var serialisedData = _serializer.Serialize(data);
await _storageProvider.SetItemAsync(key, serialisedData).ConfigureAwait(false);
await _storageProvider.SetItemAsync(key, serialisedData, cancellationToken).ConfigureAwait(false);

RaiseOnChanged(key, e.OldValue, data);
}

public async ValueTask SetItemAsStringAsync(string key, string data)
public async ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -45,17 +46,17 @@ public async ValueTask SetItemAsStringAsync(string key, string data)
if (e.Cancel)
return;

await _storageProvider.SetItemAsync(key, data).ConfigureAwait(false);
await _storageProvider.SetItemAsync(key, data, cancellationToken).ConfigureAwait(false);

RaiseOnChanged(key, e.OldValue, data);
}

public async ValueTask<T> GetItemAsync<T>(string key)
public async ValueTask<T> GetItemAsync<T>(string key, CancellationToken? cancellationToken = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

var serialisedData = await _storageProvider.GetItemAsync(key).ConfigureAwait(false);
var serialisedData = await _storageProvider.GetItemAsync(key, cancellationToken).ConfigureAwait(false);

if (string.IsNullOrWhiteSpace(serialisedData))
return default;
Expand All @@ -72,33 +73,33 @@ public async ValueTask<T> GetItemAsync<T>(string key)
}
}

public ValueTask<string> GetItemAsStringAsync(string key)
public ValueTask<string> GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

return _storageProvider.GetItemAsync(key);
return _storageProvider.GetItemAsync(key, cancellationToken);
}

public ValueTask RemoveItemAsync(string key)
public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

return _storageProvider.RemoveItemAsync(key);
return _storageProvider.RemoveItemAsync(key, cancellationToken);
}

public ValueTask ClearAsync()
=> _storageProvider.ClearAsync();
public ValueTask ClearAsync(CancellationToken? cancellationToken = null)
=> _storageProvider.ClearAsync(cancellationToken);

public ValueTask<int> LengthAsync()
=> _storageProvider.LengthAsync();
public ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null)
=> _storageProvider.LengthAsync(cancellationToken);

public ValueTask<string> KeyAsync(int index)
=> _storageProvider.KeyAsync(index);
public ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null)
=> _storageProvider.KeyAsync(index, cancellationToken);

public ValueTask<bool> ContainKeyAsync(string key)
=> _storageProvider.ContainKeyAsync(key);
public ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null)
=> _storageProvider.ContainKeyAsync(key, cancellationToken);

public void SetItem<T>(string key, T data)
{
Expand Down

0 comments on commit fbc31d4

Please sign in to comment.