From f1f5ef530ff4128c740aa5a94a28514fa03c3a83 Mon Sep 17 00:00:00 2001 From: Arkadii Kuznetsov Date: Tue, 21 Jun 2022 09:25:31 +0300 Subject: [PATCH] Changed usage of CancellationToken --- .../InMemoryStorageProvider.cs | 22 +++++------ .../BrowserStorageProvider.cs | 38 +++++++++---------- .../ILocalStorageService.cs | 22 +++++------ src/Blazored.LocalStorage/IStorageProvider.cs | 18 ++++----- .../LocalStorageService.cs | 26 ++++++------- 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs b/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs index f72b59d..a09b688 100644 --- a/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs +++ b/src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs @@ -12,7 +12,7 @@ internal class InMemoryStorageProvider : IStorageProvider public void Clear() => _dataStore.Clear(); - public ValueTask ClearAsync(CancellationToken? cancellationToken = null) + public ValueTask ClearAsync(CancellationToken cancellationToken = default) { _dataStore.Clear(); return new ValueTask(Task.CompletedTask); @@ -21,19 +21,19 @@ public ValueTask ClearAsync(CancellationToken? cancellationToken = null) public bool ContainKey(string key) => _dataStore.ContainsKey(key); - public ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null) + public ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default) => new ValueTask(ContainKey(key)); - public string GetItem(string key) + public string GetItem(string key) => _dataStore.ContainsKey(key) ? _dataStore[key] : default; - public ValueTask GetItemAsync(string key, CancellationToken? cancellationToken = null) + public ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) => new ValueTask(GetItem(key)); public string Key(int index) => index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key; - public ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null) + public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) => new ValueTask(Key(index)); public IEnumerable Keys() @@ -41,20 +41,20 @@ public IEnumerable Keys() return _dataStore.Keys.ToList(); } - public ValueTask> KeysAsync(CancellationToken? cancellationToken = null) + public ValueTask> KeysAsync(CancellationToken cancellationToken = default) => new ValueTask>(_dataStore.Keys.ToList()); - + public int Length() => _dataStore.Count; - public ValueTask LengthAsync(CancellationToken? cancellationToken = null) + public ValueTask LengthAsync(CancellationToken cancellationToken = default) => new ValueTask(Length()); public void RemoveItem(string key) => _dataStore.Remove(key); - public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null) + public ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default) { RemoveItem(key); return new ValueTask(Task.CompletedTask); @@ -68,7 +68,7 @@ public void RemoveItems(IEnumerable keys) } } - public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken? cancellationToken = null) + public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken cancellationToken = default) { RemoveItems(keys); @@ -87,7 +87,7 @@ public void SetItem(string key, string data) } } - public ValueTask SetItemAsync(string key, string data, CancellationToken? cancellationToken = null) + public ValueTask SetItemAsync(string key, string data, CancellationToken cancellationToken = default) { SetItem(key, data); return new ValueTask(Task.CompletedTask); diff --git a/src/Blazored.LocalStorage/BrowserStorageProvider.cs b/src/Blazored.LocalStorage/BrowserStorageProvider.cs index 6d4cf12..342ac26 100644 --- a/src/Blazored.LocalStorage/BrowserStorageProvider.cs +++ b/src/Blazored.LocalStorage/BrowserStorageProvider.cs @@ -1,8 +1,8 @@ -using Microsoft.JSInterop; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Microsoft.JSInterop; namespace Blazored.LocalStorage { @@ -17,37 +17,37 @@ public BrowserStorageProvider(IJSRuntime jSRuntime) _jSInProcessRuntime = jSRuntime as IJSInProcessRuntime; } - public ValueTask ClearAsync(CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeVoidAsync("localStorage.clear", cancellationToken ?? CancellationToken.None); + public ValueTask ClearAsync(CancellationToken cancellationToken = default) + => _jSRuntime.InvokeVoidAsync("localStorage.clear", cancellationToken); - public ValueTask GetItemAsync(string key, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("localStorage.getItem", cancellationToken ?? CancellationToken.None, key); + public ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) + => _jSRuntime.InvokeAsync("localStorage.getItem", cancellationToken, key); - public ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("localStorage.key", cancellationToken ?? CancellationToken.None, index); + public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) + => _jSRuntime.InvokeAsync("localStorage.key", cancellationToken, index); - public ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("localStorage.hasOwnProperty", cancellationToken ?? CancellationToken.None, key); + public ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default) + => _jSRuntime.InvokeAsync("localStorage.hasOwnProperty", cancellationToken, key); - public ValueTask LengthAsync(CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync("eval", cancellationToken ?? CancellationToken.None, "localStorage.length"); + public ValueTask LengthAsync(CancellationToken cancellationToken = default) + => _jSRuntime.InvokeAsync("eval", cancellationToken, "localStorage.length"); - public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeVoidAsync("localStorage.removeItem", cancellationToken ?? CancellationToken.None, key); + public ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default) + => _jSRuntime.InvokeVoidAsync("localStorage.removeItem", cancellationToken, key); - public ValueTask SetItemAsync(string key, string data, CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeVoidAsync("localStorage.setItem", cancellationToken ?? CancellationToken.None, key, data); + public ValueTask SetItemAsync(string key, string data, CancellationToken cancellationToken = default) + => _jSRuntime.InvokeVoidAsync("localStorage.setItem", cancellationToken, key, data); - public ValueTask> KeysAsync(CancellationToken? cancellationToken = null) - => _jSRuntime.InvokeAsync>("eval", cancellationToken ?? CancellationToken.None, "Object.keys(localStorage)"); + public ValueTask> KeysAsync(CancellationToken cancellationToken = default) + => _jSRuntime.InvokeAsync>("eval", cancellationToken, "Object.keys(localStorage)"); - public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken? cancellationToken = null) + public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken cancellationToken = default) { if (keys != null) { foreach (var key in keys) { - _jSRuntime.InvokeVoidAsync("localStorage.removeItem", cancellationToken ?? CancellationToken.None, key); + _jSRuntime.InvokeVoidAsync("localStorage.removeItem", cancellationToken, key); } } diff --git a/src/Blazored.LocalStorage/ILocalStorageService.cs b/src/Blazored.LocalStorage/ILocalStorageService.cs index eb7dfd6..cbdd4c7 100644 --- a/src/Blazored.LocalStorage/ILocalStorageService.cs +++ b/src/Blazored.LocalStorage/ILocalStorageService.cs @@ -11,7 +11,7 @@ public interface ILocalStorageService /// Clears all data from local storage. /// /// A representing the completion of the operation. - ValueTask ClearAsync(CancellationToken? cancellationToken = null); + ValueTask ClearAsync(CancellationToken cancellationToken = default); /// /// Retrieve the specified data from local storage and deseralise it to the specfied type. @@ -22,7 +22,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask GetItemAsync(string key, CancellationToken? cancellationToken = null); + ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default); /// /// Retrieve the specified data from local storage as a . @@ -33,7 +33,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null); + ValueTask GetItemAsStringAsync(string key, CancellationToken cancellationToken = default); /// /// Return the name of the key at the specified . @@ -44,7 +44,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null); + ValueTask KeyAsync(int index, CancellationToken cancellationToken = default); /// /// Returns a collection of strings representing the names of the keys in the local storage. @@ -54,7 +54,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask> KeysAsync(CancellationToken? cancellationToken = null); + ValueTask> KeysAsync(CancellationToken cancellationToken = default); /// /// Checks if the exists in local storage, but does not check its value. @@ -65,13 +65,13 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null); + ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default); /// /// The number of items stored in local storage. /// /// A representing the completion of the operation. - ValueTask LengthAsync(CancellationToken? cancellationToken = null); + ValueTask LengthAsync(CancellationToken cancellationToken = default); /// /// Remove the data with the specified . @@ -82,7 +82,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null); + ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default); /// /// Removes a collection of . @@ -92,7 +92,7 @@ public interface ILocalStorageService /// A cancellation token to signal the cancellation of the operation. Specifying this parameter will override any default cancellations such as due to timeouts /// () from being applied. /// - ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken? cancellationToken = null); + ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken cancellationToken = default); /// /// Sets or updates the in local storage with the specified . @@ -104,7 +104,7 @@ public interface ILocalStorageService /// () from being applied. /// /// A representing the completion of the operation. - ValueTask SetItemAsync(string key, T data, CancellationToken? cancellationToken = null); + ValueTask SetItemAsync(string key, T data, CancellationToken cancellationToken = default); /// /// Sets or updates the in local storage with the specified . Does not serialize the value before storing. @@ -116,7 +116,7 @@ public interface ILocalStorageService /// () from being applied. /// /// - ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null); + ValueTask SetItemAsStringAsync(string key, string data, CancellationToken cancellationToken = default); event EventHandler Changing; event EventHandler Changed; diff --git a/src/Blazored.LocalStorage/IStorageProvider.cs b/src/Blazored.LocalStorage/IStorageProvider.cs index 34f7342..d7c181a 100644 --- a/src/Blazored.LocalStorage/IStorageProvider.cs +++ b/src/Blazored.LocalStorage/IStorageProvider.cs @@ -7,23 +7,23 @@ namespace Blazored.LocalStorage internal interface IStorageProvider { void Clear(); - ValueTask ClearAsync(CancellationToken? cancellationToken = null); + ValueTask ClearAsync(CancellationToken cancellationToken = default); bool ContainKey(string key); - ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null); + ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default); string GetItem(string key); - ValueTask GetItemAsync(string key, CancellationToken? cancellationToken = null); + ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default); string Key(int index); - ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null); + ValueTask KeyAsync(int index, CancellationToken cancellationToken = default); IEnumerable Keys(); - ValueTask> KeysAsync(CancellationToken? cancellationToken = null); + ValueTask> KeysAsync(CancellationToken cancellationToken = default); int Length(); - ValueTask LengthAsync(CancellationToken? cancellationToken = null); + ValueTask LengthAsync(CancellationToken cancellationToken = default); void RemoveItem(string key); - ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null); + ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default); void RemoveItems(IEnumerable keys); - ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken? cancellationToken = null); + ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken cancellationToken = default); void SetItem(string key, string data); - ValueTask SetItemAsync(string key, string data, CancellationToken? cancellationToken = null); + ValueTask SetItemAsync(string key, string data, CancellationToken cancellationToken = default); } } diff --git a/src/Blazored.LocalStorage/LocalStorageService.cs b/src/Blazored.LocalStorage/LocalStorageService.cs index f55db6c..4114374 100644 --- a/src/Blazored.LocalStorage/LocalStorageService.cs +++ b/src/Blazored.LocalStorage/LocalStorageService.cs @@ -18,7 +18,7 @@ public LocalStorageService(IStorageProvider storageProvider, IJsonSerializer ser _serializer = serializer; } - public async ValueTask SetItemAsync(string key, T data, CancellationToken? cancellationToken = null) + public async ValueTask SetItemAsync(string key, T data, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -34,7 +34,7 @@ public async ValueTask SetItemAsync(string key, T data, CancellationToken? ca RaiseOnChanged(key, e.OldValue, data); } - public async ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null) + public async ValueTask SetItemAsStringAsync(string key, string data, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -52,7 +52,7 @@ public async ValueTask SetItemAsStringAsync(string key, string data, Cancellatio RaiseOnChanged(key, e.OldValue, data); } - public async ValueTask GetItemAsync(string key, CancellationToken? cancellationToken = null) + public async ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -74,7 +74,7 @@ public async ValueTask GetItemAsync(string key, CancellationToken? cancell } } - public ValueTask GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null) + public ValueTask GetItemAsStringAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -82,7 +82,7 @@ public ValueTask GetItemAsStringAsync(string key, CancellationToken? can return _storageProvider.GetItemAsync(key, cancellationToken); } - public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null) + public ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); @@ -90,27 +90,27 @@ public ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToke return _storageProvider.RemoveItemAsync(key, cancellationToken); } - public ValueTask ClearAsync(CancellationToken? cancellationToken = null) + public ValueTask ClearAsync(CancellationToken cancellationToken = default) => _storageProvider.ClearAsync(cancellationToken); - public ValueTask LengthAsync(CancellationToken? cancellationToken = null) + public ValueTask LengthAsync(CancellationToken cancellationToken = default) => _storageProvider.LengthAsync(cancellationToken); - public ValueTask KeyAsync(int index, CancellationToken? cancellationToken = null) + public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) => _storageProvider.KeyAsync(index, cancellationToken); - public ValueTask> KeysAsync(CancellationToken? cancellationToken = null) + public ValueTask> KeysAsync(CancellationToken cancellationToken = default) => _storageProvider.KeysAsync(cancellationToken); - public ValueTask ContainKeyAsync(string key, CancellationToken? cancellationToken = null) + public ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default) => _storageProvider.ContainKeyAsync(key, cancellationToken); - public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken? cancellationToken = null) + public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken cancellationToken = default) => _storageProvider.RemoveItemsAsync(keys, cancellationToken); public IEnumerable Keys() { - return _storageProvider.Keys(); + return _storageProvider.Keys(); } public void SetItem(string key, T data) @@ -237,7 +237,7 @@ private ChangingEventArgs RaiseOnChangingSync(string key, object data) return e; } - private async Task GetItemInternalAsync(string key, CancellationToken? cancellationToken = null) + private async Task GetItemInternalAsync(string key, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key));