From fbc31d4c29fa09fc2728e53c123d8f6e40d6b559 Mon Sep 17 00:00:00 2001 From: Chris Sainty Date: Fri, 8 Oct 2021 23:05:24 +0100 Subject: [PATCH] Added missing cancellation token support --- .../BrowserStorageProvider.cs | 14 +++---- .../ISessionStorageService.cs | 19 +++++----- .../SessionStorageService.cs | 37 ++++++++++--------- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/Blazored.SessionStorage/BrowserStorageProvider.cs b/src/Blazored.SessionStorage/BrowserStorageProvider.cs index 626f9a7..6c1e398 100644 --- a/src/Blazored.SessionStorage/BrowserStorageProvider.cs +++ b/src/Blazored.SessionStorage/BrowserStorageProvider.cs @@ -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 GetItemAsync(string key, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("sessionStorage.getItem", key); + => _jSRuntime.InvokeAsync("sessionStorage.getItem", cancellationToken ?? CancellationToken.None, key); public ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("sessionStorage.key", index); + => _jSRuntime.InvokeAsync("sessionStorage.key", cancellationToken ?? CancellationToken.None, index); public ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("sessionStorage.hasOwnProperty", key); + => _jSRuntime.InvokeAsync("sessionStorage.hasOwnProperty", cancellationToken ?? CancellationToken.None, key); public ValueTask LengthAsync(CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("eval", "sessionStorage.length"); + => _jSRuntime.InvokeAsync("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() { diff --git a/src/Blazored.SessionStorage/ISessionStorageService.cs b/src/Blazored.SessionStorage/ISessionStorageService.cs index f9d7a3f..c61c98c 100644 --- a/src/Blazored.SessionStorage/ISessionStorageService.cs +++ b/src/Blazored.SessionStorage/ISessionStorageService.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Threading.Tasks; namespace Blazored.SessionStorage @@ -9,48 +10,48 @@ public interface ISessionStorageService /// Clears all data from session storage. /// /// A representing the completion of the operation. - ValueTask ClearAsync(); + ValueTask ClearAsync(CancellationToken? cancellationToken = null); /// /// Retrieve the specified data from session storage and deseralise it to the specfied type. /// /// A value specifying the name of the session storage slot to use /// A representing the completion of the operation. - ValueTask GetItemAsync(string key); + ValueTask GetItemAsync(string key, CancellationToken? cancellationToken = null); /// /// Retrieve the specified data from session storage as a . /// /// A value specifying the name of the storage slot to use /// A representing the completion of the operation. - ValueTask GetItemAsStringAsync(string key); + ValueTask GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null); /// /// Return the name of the key at the specified . /// /// /// A representing the completion of the operation. - ValueTask KeyAsync(int index); + ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null); /// /// Checks if the exists in session storage, but does not check its value. /// /// A value specifying the name of the storage slot to use /// A representing the completion of the operation. - ValueTask ContainKeyAsync(string key); + ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null); /// /// The number of items stored in session storage. /// /// A representing the completion of the operation. - ValueTask LengthAsync(); + ValueTask LengthAsync(CancellationToken? cancellationToken = null); /// /// Remove the data with the specified . /// /// A value specifying the name of the storage slot to use /// A representing the completion of the operation. - ValueTask RemoveItemAsync(string key); + ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null); /// /// Sets or updates the in session storage with the specified . @@ -58,7 +59,7 @@ public interface ISessionStorageService /// A value specifying the name of the storage slot to use /// The data to be saved /// A representing the completion of the operation. - ValueTask SetItemAsync(string key, T data); + ValueTask SetItemAsync(string key, T data, CancellationToken? cancellationToken = null); /// /// Sets or updates the in session storage with the specified . Does not serialize the value before storing. @@ -66,7 +67,7 @@ public interface ISessionStorageService /// A value specifying the name of the storage slot to use /// The string to be saved /// - ValueTask SetItemAsStringAsync(string key, string data); + ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null); event EventHandler Changing; event EventHandler Changed; diff --git a/src/Blazored.SessionStorage/SessionStorageService.cs b/src/Blazored.SessionStorage/SessionStorageService.cs index 64434c3..d020994 100644 --- a/src/Blazored.SessionStorage/SessionStorageService.cs +++ b/src/Blazored.SessionStorage/SessionStorageService.cs @@ -1,5 +1,6 @@ using System; using System.Text.Json; +using System.Threading; using System.Threading.Tasks; using Blazored.SessionStorage.Serialization; @@ -16,7 +17,7 @@ public SessionStorageService(IStorageProvider storageProvider, IJsonSerializer s _serializer = serializer; } - public async ValueTask SetItemAsync(string key, T data) + public async ValueTask SetItemAsync(string key, T data, CancellationToken? cancellationToken = null) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -27,12 +28,12 @@ public async ValueTask SetItemAsync(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)); @@ -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 GetItemAsync(string key) + public async ValueTask GetItemAsync(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; @@ -72,33 +73,33 @@ public async ValueTask GetItemAsync(string key) } } - public ValueTask GetItemAsStringAsync(string key) + public ValueTask 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 LengthAsync() - => _storageProvider.LengthAsync(); + public ValueTask LengthAsync(CancellationToken? cancellationToken = null) + => _storageProvider.LengthAsync(cancellationToken); - public ValueTask KeyAsync(int index) - => _storageProvider.KeyAsync(index); + public ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null) + => _storageProvider.KeyAsync(index, cancellationToken); - public ValueTask ContainKeyAsync(string key) - => _storageProvider.ContainKeyAsync(key); + public ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null) + => _storageProvider.ContainKeyAsync(key, cancellationToken); public void SetItem(string key, T data) {