Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed usage of CancellationToken #184

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,40 +21,40 @@ public ValueTask ClearAsync(CancellationToken? cancellationToken = null)
public bool ContainKey(string key)
=> _dataStore.ContainsKey(key);

public ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null)
public ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default)
=> new ValueTask<bool>(ContainKey(key));

public string GetItem(string key)
public string GetItem(string key)
=> _dataStore.ContainsKey(key) ? _dataStore[key] : default;

public ValueTask<string> GetItemAsync(string key, CancellationToken? cancellationToken = null)
public ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default)
=> new ValueTask<string>(GetItem(key));

public string Key(int index)
=> index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key;

public ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null)
public ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
=> new ValueTask<string>(Key(index));

public IEnumerable<string> Keys()
{
return _dataStore.Keys.ToList();
}

public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken? cancellationToken = null)
public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
=> new ValueTask<IEnumerable<string>>(_dataStore.Keys.ToList());


public int Length()
=> _dataStore.Count;

public ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null)
public ValueTask<int> LengthAsync(CancellationToken cancellationToken = default)
=> new ValueTask<int>(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);
Expand All @@ -68,7 +68,7 @@ public void RemoveItems(IEnumerable<string> keys)
}
}

public ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken? cancellationToken = null)
public ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)
{
RemoveItems(keys);

Expand All @@ -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);
Expand Down
38 changes: 19 additions & 19 deletions src/Blazored.LocalStorage/BrowserStorageProvider.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<string> GetItemAsync(string key, CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<string>("localStorage.getItem", cancellationToken ?? CancellationToken.None, key);
public ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default)
=> _jSRuntime.InvokeAsync<string>("localStorage.getItem", cancellationToken, key);

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

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

public ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<int>("eval", cancellationToken ?? CancellationToken.None, "localStorage.length");
public ValueTask<int> LengthAsync(CancellationToken cancellationToken = default)
=> _jSRuntime.InvokeAsync<int>("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<IEnumerable<string>> KeysAsync(CancellationToken? cancellationToken = null)
=> _jSRuntime.InvokeAsync<IEnumerable<string>>("eval", cancellationToken ?? CancellationToken.None, "Object.keys(localStorage)");
public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
=> _jSRuntime.InvokeAsync<IEnumerable<string>>("eval", cancellationToken, "Object.keys(localStorage)");

public ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken? cancellationToken = null)
public ValueTask RemoveItemsAsync(IEnumerable<string> 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);
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/Blazored.LocalStorage/ILocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ILocalStorageService
/// Clears all data from local storage.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask ClearAsync(CancellationToken? cancellationToken = null);
ValueTask ClearAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Retrieve the specified data from local storage and deseralise it to the specfied type.
Expand All @@ -22,7 +22,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<T> GetItemAsync<T>(string key, CancellationToken? cancellationToken = null);
ValueTask<T> GetItemAsync<T>(string key, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieve the specified data from local storage as a <see cref="string"/>.
Expand All @@ -33,7 +33,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<string> GetItemAsStringAsync(string key, CancellationToken? cancellationToken = null);
ValueTask<string> GetItemAsStringAsync(string key, CancellationToken cancellationToken = default);

/// <summary>
/// Return the name of the key at the specified <paramref name="index"/>.
Expand All @@ -44,7 +44,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null);
ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default);

/// <summary>
/// Returns a collection of strings representing the names of the keys in the local storage.
Expand All @@ -54,7 +54,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<IEnumerable<string>> KeysAsync(CancellationToken? cancellationToken = null);
ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Checks if the <paramref name="key"/> exists in local storage, but does not check its value.
Expand All @@ -65,13 +65,13 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null);
ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default);

/// <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(CancellationToken? cancellationToken = null);
ValueTask<int> LengthAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Remove the data with the specified <paramref name="key"/>.
Expand All @@ -82,7 +82,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask RemoveItemAsync(string key, CancellationToken? cancellationToken = null);
ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default);

/// <summary>
/// Removes a collection of <paramref name="keys"/>.
Expand All @@ -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
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken? cancellationToken = null);
ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default);

/// <summary>
/// Sets or updates the <paramref name="data"/> in local storage with the specified <paramref name="key"/>.
Expand All @@ -104,7 +104,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask SetItemAsync<T>(string key, T data, CancellationToken? cancellationToken = null);
ValueTask SetItemAsync<T>(string key, T data, CancellationToken cancellationToken = default);

/// <summary>
/// Sets or updates the <paramref name="data"/> in local storage with the specified <paramref name="key"/>. Does not serialize the value before storing.
Expand All @@ -116,7 +116,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns></returns>
ValueTask SetItemAsStringAsync(string key, string data, CancellationToken? cancellationToken = null);
ValueTask SetItemAsStringAsync(string key, string data, CancellationToken cancellationToken = default);

event EventHandler<ChangingEventArgs> Changing;
event EventHandler<ChangedEventArgs> Changed;
Expand Down
18 changes: 9 additions & 9 deletions src/Blazored.LocalStorage/IStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> ContainKeyAsync(string key, CancellationToken? cancellationToken = null);
ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default);
string GetItem(string key);
ValueTask<string> GetItemAsync(string key, CancellationToken? cancellationToken = null);
ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default);
string Key(int index);
ValueTask<string> KeyAsync(int index, CancellationToken? cancellationToken = null);
ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default);
IEnumerable<string> Keys();
ValueTask<IEnumerable<string>> KeysAsync(CancellationToken? cancellationToken = null);
ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default);
int Length();
ValueTask<int> LengthAsync(CancellationToken? cancellationToken = null);
ValueTask<int> 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<string> keys);
ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken? cancellationToken = null);
ValueTask RemoveItemsAsync(IEnumerable<string> 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);

}
}
Loading