Skip to content

Commit

Permalink
cancellationToken support for ILocalStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVollmers committed Sep 10, 2023
1 parent 22cff01 commit 17b61cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
22 changes: 11 additions & 11 deletions packages/Ignis.Components.Web/ILocalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

public interface ILocalStorage
{
Task SetItemAsync<T>(string key, T value);
Task<T?> GetItemAsync<T>(string key);
Task RemoveItemAsync(string key);
Task ClearAsync();
Task<int> GetLengthAsync();
Task<string?> GetKeyAsync(int index);
Task SetItemAsync<T>(string key, T value, CancellationToken cancellationToken = default);

Task<T?> GetItemAsync<T>(string key, CancellationToken cancellationToken = default);

Task RemoveItemAsync(string key, CancellationToken cancellationToken = default);

Task ClearAsync(CancellationToken cancellationToken = default);

Task<int> GetLengthAsync(CancellationToken cancellationToken = default);

Task<string?> GetKeyAsync(int index, CancellationToken cancellationToken = default);
}
25 changes: 13 additions & 12 deletions packages/Ignis.Components.Web/LocalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,42 @@ public LocalStorage(IJSRuntime jsRuntime)
_jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
}

public async Task SetItemAsync<T>(string key, T value)
public async Task SetItemAsync<T>(string key, T value, CancellationToken cancellationToken = default)
{
if (key == null) throw new ArgumentNullException(nameof(key));

await _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, JsonSerializer.Serialize(value));
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", cancellationToken, key,
JsonSerializer.Serialize(value));
}

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

var json = await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", key);
var json = await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", cancellationToken, key);

return json == null ? default : JsonSerializer.Deserialize<T>(json);
}

public async Task RemoveItemAsync(string key)
public async Task RemoveItemAsync(string key, CancellationToken cancellationToken = default)
{
if (key == null) throw new ArgumentNullException(nameof(key));

await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", key);
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", cancellationToken, key);
}

public async Task ClearAsync()
public async Task ClearAsync(CancellationToken cancellationToken = default)
{
await _jsRuntime.InvokeVoidAsync("localStorage.clear");
await _jsRuntime.InvokeVoidAsync("localStorage.clear", cancellationToken);
}

public async Task<int> GetLengthAsync()
public async Task<int> GetLengthAsync(CancellationToken cancellationToken = default)
{
return await _jsRuntime.InvokeAsync<int>("localStorage.length");
return await _jsRuntime.InvokeAsync<int>("localStorage.length", cancellationToken);
}

public async Task<string?> GetKeyAsync(int index)
public async Task<string?> GetKeyAsync(int index, CancellationToken cancellationToken = default)
{
return await _jsRuntime.InvokeAsync<string?>("localStorage.key", index);
return await _jsRuntime.InvokeAsync<string?>("localStorage.key", cancellationToken, index);
}
}
2 changes: 1 addition & 1 deletion website/Ignis.Website/Components/ThemeSelector.razor
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

protected override async Task OnInitializedAsync(CancellationToken cancellationToken)
{
_selectedTheme = await LocalStorage.GetItemAsync<Theme?>("theme") ?? Theme.System;
_selectedTheme = await LocalStorage.GetItemAsync<Theme?>("theme", cancellationToken) ?? Theme.System;
}

private Type GetThemeIcon(Theme theme)
Expand Down

0 comments on commit 17b61cd

Please sign in to comment.