Skip to content

Commit fbc31d4

Browse files
committed
Added missing cancellation token support
1 parent 27cac1d commit fbc31d4

3 files changed

Lines changed: 36 additions & 34 deletions

File tree

‎src/Blazored.SessionStorage/BrowserStorageProvider.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ public BrowserStorageProvider(IJSRuntime jSRuntime)
1717
}
1818

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

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

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

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

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

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

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

4040
public void Clear()
4141
{

‎src/Blazored.SessionStorage/ISessionStorageService.cs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

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

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

2122
/// <summary>
2223
/// Retrieve the specified data from session storage as a <see cref="string"/>.
2324
/// </summary>
2425
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
2526
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
26-
ValueTask<string> GetItemAsStringAsync(string key);
27+
ValueTask<string> GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null);
2728

2829
/// <summary>
2930
/// Return the name of the key at the specified <paramref name="index"/>.
3031
/// </summary>
3132
/// <param name="index"></param>
3233
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
33-
ValueTask<string> KeyAsync(int index);
34+
ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null);
3435

3536
/// <summary>
3637
/// Checks if the <paramref name="key"/> exists in session storage, but does not check its value.
3738
/// </summary>
3839
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
3940
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
40-
ValueTask<bool> ContainKeyAsync(string key);
41+
ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null);
4142

4243
/// <summary>
4344
/// The number of items stored in session storage.
4445
/// </summary>
4546
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
46-
ValueTask<int> LengthAsync();
47+
ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null);
4748

4849
/// <summary>
4950
/// Remove the data with the specified <paramref name="key"/>.
5051
/// </summary>
5152
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
5253
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
53-
ValueTask RemoveItemAsync(string key);
54+
ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null);
5455

5556
/// <summary>
5657
/// Sets or updates the <paramref name="data"/> in session storage with the specified <paramref name="key"/>.
5758
/// </summary>
5859
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
5960
/// <param name="data">The data to be saved</param>
6061
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
61-
ValueTask SetItemAsync<T>(string key, T data);
62+
ValueTask SetItemAsync<T>(string key, T data, CancellationToken? cancellationToken = null);
6263

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

7172
event EventHandler<ChangingEventArgs> Changing;
7273
event EventHandler<ChangedEventArgs> Changed;

‎src/Blazored.SessionStorage/SessionStorageService.cs‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text.Json;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Blazored.SessionStorage.Serialization;
56

@@ -16,7 +17,7 @@ public SessionStorageService(IStorageProvider storageProvider, IJsonSerializer s
1617
_serializer = serializer;
1718
}
1819

19-
public async ValueTask SetItemAsync<T>(string key, T data)
20+
public async ValueTask SetItemAsync<T>(string key, T data, CancellationToken? cancellationToken = null)
2021
{
2122
if (string.IsNullOrWhiteSpace(key))
2223
throw new ArgumentNullException(nameof(key));
@@ -27,12 +28,12 @@ public async ValueTask SetItemAsync<T>(string key, T data)
2728
return;
2829

2930
var serialisedData = _serializer.Serialize(data);
30-
await _storageProvider.SetItemAsync(key, serialisedData).ConfigureAwait(false);
31+
await _storageProvider.SetItemAsync(key, serialisedData, cancellationToken).ConfigureAwait(false);
3132

3233
RaiseOnChanged(key, e.OldValue, data);
3334
}
3435

35-
public async ValueTask SetItemAsStringAsync(string key, string data)
36+
public async ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null)
3637
{
3738
if (string.IsNullOrWhiteSpace(key))
3839
throw new ArgumentNullException(nameof(key));
@@ -45,17 +46,17 @@ public async ValueTask SetItemAsStringAsync(string key, string data)
4546
if (e.Cancel)
4647
return;
4748

48-
await _storageProvider.SetItemAsync(key, data).ConfigureAwait(false);
49+
await _storageProvider.SetItemAsync(key, data, cancellationToken).ConfigureAwait(false);
4950

5051
RaiseOnChanged(key, e.OldValue, data);
5152
}
5253

53-
public async ValueTask<T> GetItemAsync<T>(string key)
54+
public async ValueTask<T> GetItemAsync<T>(string key, CancellationToken? cancellationToken = null)
5455
{
5556
if (string.IsNullOrWhiteSpace(key))
5657
throw new ArgumentNullException(nameof(key));
5758

58-
var serialisedData = await _storageProvider.GetItemAsync(key).ConfigureAwait(false);
59+
var serialisedData = await _storageProvider.GetItemAsync(key, cancellationToken).ConfigureAwait(false);
5960

6061
if (string.IsNullOrWhiteSpace(serialisedData))
6162
return default;
@@ -72,33 +73,33 @@ public async ValueTask<T> GetItemAsync<T>(string key)
7273
}
7374
}
7475

75-
public ValueTask<string> GetItemAsStringAsync(string key)
76+
public ValueTask<string> GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null)
7677
{
7778
if (string.IsNullOrWhiteSpace(key))
7879
throw new ArgumentNullException(nameof(key));
7980

80-
return _storageProvider.GetItemAsync(key);
81+
return _storageProvider.GetItemAsync(key, cancellationToken);
8182
}
8283

83-
public ValueTask RemoveItemAsync(string key)
84+
public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null)
8485
{
8586
if (string.IsNullOrWhiteSpace(key))
8687
throw new ArgumentNullException(nameof(key));
8788

88-
return _storageProvider.RemoveItemAsync(key);
89+
return _storageProvider.RemoveItemAsync(key, cancellationToken);
8990
}
9091

91-
public ValueTask ClearAsync()
92-
=> _storageProvider.ClearAsync();
92+
public ValueTask ClearAsync(CancellationToken? cancellationToken = null)
93+
=> _storageProvider.ClearAsync(cancellationToken);
9394

94-
public ValueTask<int> LengthAsync()
95-
=> _storageProvider.LengthAsync();
95+
public ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null)
96+
=> _storageProvider.LengthAsync(cancellationToken);
9697

97-
public ValueTask<string> KeyAsync(int index)
98-
=> _storageProvider.KeyAsync(index);
98+
public ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null)
99+
=> _storageProvider.KeyAsync(index, cancellationToken);
99100

100-
public ValueTask<bool> ContainKeyAsync(string key)
101-
=> _storageProvider.ContainKeyAsync(key);
101+
public ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null)
102+
=> _storageProvider.ContainKeyAsync(key, cancellationToken);
102103

103104
public void SetItem<T>(string key, T data)
104105
{

0 commit comments

Comments
 (0)