From 17b61cd98d4c47305377d22f5a09c2db9ccbf819 Mon Sep 17 00:00:00 2001 From: dvolper Date: Sun, 10 Sep 2023 11:49:31 +0200 Subject: [PATCH] cancellationToken support for ILocalStorage --- .../Ignis.Components.Web/ILocalStorage.cs | 22 ++++++++-------- packages/Ignis.Components.Web/LocalStorage.cs | 25 ++++++++++--------- .../Components/ThemeSelector.razor | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/Ignis.Components.Web/ILocalStorage.cs b/packages/Ignis.Components.Web/ILocalStorage.cs index a2beacad..51f33bc5 100644 --- a/packages/Ignis.Components.Web/ILocalStorage.cs +++ b/packages/Ignis.Components.Web/ILocalStorage.cs @@ -2,15 +2,15 @@ public interface ILocalStorage { - Task SetItemAsync(string key, T value); - - Task GetItemAsync(string key); - - Task RemoveItemAsync(string key); - - Task ClearAsync(); - - Task GetLengthAsync(); - - Task GetKeyAsync(int index); + Task SetItemAsync(string key, T value, CancellationToken cancellationToken = default); + + Task GetItemAsync(string key, CancellationToken cancellationToken = default); + + Task RemoveItemAsync(string key, CancellationToken cancellationToken = default); + + Task ClearAsync(CancellationToken cancellationToken = default); + + Task GetLengthAsync(CancellationToken cancellationToken = default); + + Task GetKeyAsync(int index, CancellationToken cancellationToken = default); } diff --git a/packages/Ignis.Components.Web/LocalStorage.cs b/packages/Ignis.Components.Web/LocalStorage.cs index cbfe7f60..3feb0e7b 100644 --- a/packages/Ignis.Components.Web/LocalStorage.cs +++ b/packages/Ignis.Components.Web/LocalStorage.cs @@ -12,41 +12,42 @@ public LocalStorage(IJSRuntime jsRuntime) _jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); } - public async Task SetItemAsync(string key, T value) + public async Task SetItemAsync(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 GetItemAsync(string key) + public async Task GetItemAsync(string key, CancellationToken cancellationToken = default) { if (key == null) throw new ArgumentNullException(nameof(key)); - var json = await _jsRuntime.InvokeAsync("localStorage.getItem", key); + var json = await _jsRuntime.InvokeAsync("localStorage.getItem", cancellationToken, key); return json == null ? default : JsonSerializer.Deserialize(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 GetLengthAsync() + public async Task GetLengthAsync(CancellationToken cancellationToken = default) { - return await _jsRuntime.InvokeAsync("localStorage.length"); + return await _jsRuntime.InvokeAsync("localStorage.length", cancellationToken); } - public async Task GetKeyAsync(int index) + public async Task GetKeyAsync(int index, CancellationToken cancellationToken = default) { - return await _jsRuntime.InvokeAsync("localStorage.key", index); + return await _jsRuntime.InvokeAsync("localStorage.key", cancellationToken, index); } } diff --git a/website/Ignis.Website/Components/ThemeSelector.razor b/website/Ignis.Website/Components/ThemeSelector.razor index efab741e..310cba95 100644 --- a/website/Ignis.Website/Components/ThemeSelector.razor +++ b/website/Ignis.Website/Components/ThemeSelector.razor @@ -59,7 +59,7 @@ protected override async Task OnInitializedAsync(CancellationToken cancellationToken) { - _selectedTheme = await LocalStorage.GetItemAsync("theme") ?? Theme.System; + _selectedTheme = await LocalStorage.GetItemAsync("theme", cancellationToken) ?? Theme.System; } private Type GetThemeIcon(Theme theme)