|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | +using Microsoft.Extensions.Options; |
| 5 | +using Microsoft.JSInterop; |
| 6 | + |
| 7 | +namespace LoreSoft.Blazor.Controls; |
| 8 | + |
| 9 | +public class StorageService |
| 10 | +{ |
| 11 | + public const string LocalStorage = "localStorage"; |
| 12 | + public const string SessionStorage = "sessionStorage"; |
| 13 | + |
| 14 | + private readonly IJSRuntime _javaScript; |
| 15 | + private readonly JsonSerializerOptions _options; |
| 16 | + private readonly ILogger<StorageService> _logger; |
| 17 | + |
| 18 | + public StorageService( |
| 19 | + IJSRuntime javaScript, |
| 20 | + IOptions<JsonSerializerOptions> options, |
| 21 | + ILogger<StorageService> logger) |
| 22 | + { |
| 23 | + _javaScript = javaScript; |
| 24 | + _options = options.Value; |
| 25 | + _logger = logger; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public async ValueTask<T?> GetItemAsync<T>(string key, StoreType storeType = StoreType.Session) |
| 30 | + { |
| 31 | + ArgumentException.ThrowIfNullOrEmpty(key); |
| 32 | + |
| 33 | + var module = storeType == StoreType.Local ? LocalStorage : SessionStorage; |
| 34 | + var method = $"{module}.getItem"; |
| 35 | + |
| 36 | + var json = await _javaScript.InvokeAsync<string?>(method, key); |
| 37 | + if (string.IsNullOrWhiteSpace(json)) |
| 38 | + return default; |
| 39 | + |
| 40 | + _logger.LogInformation("Retrieved {StoreType} storage item {Key} with value {Value}", storeType, key, json); |
| 41 | + |
| 42 | + return JsonSerializer.Deserialize<T>(json, _options); |
| 43 | + } |
| 44 | + |
| 45 | + public async ValueTask SetItemAsync<T>(string key, T value, StoreType storeType = StoreType.Session) |
| 46 | + { |
| 47 | + ArgumentException.ThrowIfNullOrEmpty(key); |
| 48 | + |
| 49 | + var module = storeType == StoreType.Local ? LocalStorage : SessionStorage; |
| 50 | + var method = $"{module}.setItem"; |
| 51 | + |
| 52 | + var json = value is null ? string.Empty : JsonSerializer.Serialize(value, _options); |
| 53 | + |
| 54 | + _logger.LogInformation("Setting {StoreType} storage item {Key} to {Value}", storeType, key, json); |
| 55 | + |
| 56 | + await _javaScript.InvokeVoidAsync(method, key, json); |
| 57 | + } |
| 58 | + |
| 59 | + public async ValueTask RemoveItemAsync(string key, StoreType storeType = StoreType.Session) |
| 60 | + { |
| 61 | + ArgumentException.ThrowIfNullOrEmpty(key); |
| 62 | + |
| 63 | + var module = storeType == StoreType.Local ? LocalStorage : SessionStorage; |
| 64 | + var method = $"{module}.removeItem"; |
| 65 | + |
| 66 | + await _javaScript.InvokeVoidAsync(method, key); |
| 67 | + } |
| 68 | + |
| 69 | + public async ValueTask ClearAsync(StoreType storeType = StoreType.Session) |
| 70 | + { |
| 71 | + var module = storeType == StoreType.Local ? LocalStorage : SessionStorage; |
| 72 | + var method = $"{module}.clear"; |
| 73 | + |
| 74 | + await _javaScript.InvokeVoidAsync(method); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +public enum StoreType |
| 79 | +{ |
| 80 | + Local, |
| 81 | + Session |
| 82 | +} |
| 83 | + |
0 commit comments