From 86d30812245a286a403a0f73ddb1eb641206a18b Mon Sep 17 00:00:00 2001 From: dhiogoacioli Date: Wed, 20 Jan 2021 11:12:40 -0300 Subject: [PATCH] Add ContainKeyAsync --- .../ISessionStorageService.cs | 10 +- .../ISyncSessionStorageService.cs | 8 + .../SessionStorageService.cs | 335 +++++++++--------- 3 files changed, 190 insertions(+), 163 deletions(-) diff --git a/src/Blazored.SessionStorage/ISessionStorageService.cs b/src/Blazored.SessionStorage/ISessionStorageService.cs index 97724f7..0be8e7e 100644 --- a/src/Blazored.SessionStorage/ISessionStorageService.cs +++ b/src/Blazored.SessionStorage/ISessionStorageService.cs @@ -11,6 +11,13 @@ public interface ISessionStorageService Task KeyAsync(int index); + /// + /// Checks if the key exists in Session Storage but does not check the value. + /// + /// name of the key + /// True if the key exist, false otherwise + Task ContainKeyAsync(string key); + Task LengthAsync(); Task RemoveItemAsync(string key); @@ -18,6 +25,7 @@ public interface ISessionStorageService Task SetItemAsync(string key, T data); event EventHandler Changing; + event EventHandler Changed; } -} +} \ No newline at end of file diff --git a/src/Blazored.SessionStorage/ISyncSessionStorageService.cs b/src/Blazored.SessionStorage/ISyncSessionStorageService.cs index a994f6a..1bdad3b 100644 --- a/src/Blazored.SessionStorage/ISyncSessionStorageService.cs +++ b/src/Blazored.SessionStorage/ISyncSessionStorageService.cs @@ -10,6 +10,13 @@ public interface ISyncSessionStorageService string Key(int index); + /// + /// Checks if the key exists in Session Storage but does not check the value. + /// + /// name of the key + /// True if the key exist, false otherwise + bool ContainKey(string key); + int Length(); void RemoveItem(string key); @@ -17,6 +24,7 @@ public interface ISyncSessionStorageService void SetItem(string key, T data); event EventHandler Changing; + event EventHandler Changed; } } \ No newline at end of file diff --git a/src/Blazored.SessionStorage/SessionStorageService.cs b/src/Blazored.SessionStorage/SessionStorageService.cs index efb0922..fbbe878 100644 --- a/src/Blazored.SessionStorage/SessionStorageService.cs +++ b/src/Blazored.SessionStorage/SessionStorageService.cs @@ -1,181 +1,192 @@ using Blazored.SessionStorage.StorageOptions; using Microsoft.Extensions.Options; -using Microsoft.JSInterop; -using System; -using System.Text.Json; -using System.Threading.Tasks; - -namespace Blazored.SessionStorage -{ - public class SessionStorageService : ISessionStorageService, ISyncSessionStorageService - { - private readonly IJSRuntime _jSRuntime; - private readonly IJSInProcessRuntime _jSInProcessRuntime; - private readonly JsonSerializerOptions _jsonOptions; - - public event EventHandler Changing; - public event EventHandler Changed; - - public SessionStorageService(IJSRuntime jSRuntime, IOptions options) - { +using Microsoft.JSInterop; +using System; +using System.Text.Json; +using System.Threading.Tasks; + +namespace Blazored.SessionStorage +{ + public class SessionStorageService : ISessionStorageService, ISyncSessionStorageService + { + private readonly IJSRuntime _jSRuntime; + private readonly IJSInProcessRuntime _jSInProcessRuntime; + private readonly JsonSerializerOptions _jsonOptions; + + public event EventHandler Changing; + + public event EventHandler Changed; + + public SessionStorageService(IJSRuntime jSRuntime, IOptions options) + { _jSRuntime = jSRuntime; _jsonOptions = options.Value.JsonSerializerOptions; _jSInProcessRuntime = jSRuntime as IJSInProcessRuntime; - } - - public async Task SetItemAsync(string key, T data) - { - if (string.IsNullOrEmpty(key)) - throw new ArgumentNullException(nameof(key)); - - var e = await RaiseOnChangingAsync(key, data); - - if (e.Cancel) + } + + public async Task SetItemAsync(string key, T data) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + var e = await RaiseOnChangingAsync(key, data); + + if (e.Cancel) return; var serialisedData = JsonSerializer.Serialize(data, _jsonOptions); await _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", key, serialisedData); - - RaiseOnChanged(key, e.OldValue, data); - } - - public async Task GetItemAsync(string key) - { - if (string.IsNullOrEmpty(key)) - throw new ArgumentNullException(nameof(key)); - - var serialisedData = await _jSRuntime.InvokeAsync("sessionStorage.getItem", key); - - if (serialisedData == null) + + RaiseOnChanged(key, e.OldValue, data); + } + + public async Task GetItemAsync(string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + var serialisedData = await _jSRuntime.InvokeAsync("sessionStorage.getItem", key); + + if (serialisedData == null) return default; return JsonSerializer.Deserialize(serialisedData, _jsonOptions); - } - - public async Task RemoveItemAsync(string key) - { - if (string.IsNullOrEmpty(key)) - throw new ArgumentNullException(nameof(key)); - - await _jSRuntime.InvokeAsync("sessionStorage.removeItem", key); - } - - public async Task ClearAsync() => await _jSRuntime.InvokeAsync("sessionStorage.clear"); - - public async Task LengthAsync() => await _jSRuntime.InvokeAsync("eval", "sessionStorage.length"); - - public async Task KeyAsync(int index) => await _jSRuntime.InvokeAsync("sessionStorage.key", index); - - public void SetItem(string key, T data) - { - if (string.IsNullOrEmpty(key)) - throw new ArgumentNullException(nameof(key)); - - if (_jSInProcessRuntime == null) - throw new InvalidOperationException("IJSInProcessRuntime not available"); - - var e = RaiseOnChangingSync(key, data); - - if (e.Cancel) - return; - + } + + public async Task RemoveItemAsync(string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + await _jSRuntime.InvokeAsync("sessionStorage.removeItem", key); + } + + public async Task ClearAsync() => await _jSRuntime.InvokeAsync("sessionStorage.clear"); + + public async Task LengthAsync() => await _jSRuntime.InvokeAsync("eval", "sessionStorage.length"); + + public async Task KeyAsync(int index) => await _jSRuntime.InvokeAsync("sessionStorage.key", index); + + public async Task ContainKeyAsync(string key) => await _jSRuntime.InvokeAsync("sessionStorage.hasOwnProperty", key); + + public void SetItem(string key, T data) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + var e = RaiseOnChangingSync(key, data); + + if (e.Cancel) + return; + var serialisedData = JsonSerializer.Serialize(data, _jsonOptions); _jSInProcessRuntime.InvokeVoid("sessionStorage.setItem", key, serialisedData); - - RaiseOnChanged(key, e.OldValue, data); - } - - public T GetItem(string key) - { - if (string.IsNullOrEmpty(key)) - throw new ArgumentNullException(nameof(key)); - - if (_jSInProcessRuntime == null) - throw new InvalidOperationException("IJSInProcessRuntime not available"); - - var serialisedData = _jSInProcessRuntime.Invoke("sessionStorage.getItem", key); - - if (serialisedData == null) + + RaiseOnChanged(key, e.OldValue, data); + } + + public T GetItem(string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + var serialisedData = _jSInProcessRuntime.Invoke("sessionStorage.getItem", key); + + if (serialisedData == null) return default; return JsonSerializer.Deserialize(serialisedData, _jsonOptions); - } - - public void RemoveItem(string key) - { - if (string.IsNullOrEmpty(key)) - throw new ArgumentNullException(nameof(key)); - - if (_jSInProcessRuntime == null) - throw new InvalidOperationException("IJSInProcessRuntime not available"); - - _jSInProcessRuntime.InvokeVoid("sessionStorage.removeItem", key); - } - - public void Clear() - { - if (_jSInProcessRuntime == null) - throw new InvalidOperationException("IJSInProcessRuntime not available"); - - _jSInProcessRuntime.InvokeVoid("sessionStorage.clear"); - } - - public int Length() - { - if (_jSInProcessRuntime == null) - throw new InvalidOperationException("IJSInProcessRuntime not available"); - - return _jSInProcessRuntime.Invoke("eval", "sessionStorage.length"); - } - - public string Key(int index) - { - if (_jSInProcessRuntime == null) - throw new InvalidOperationException("IJSInProcessRuntime not available"); - - return _jSInProcessRuntime.Invoke("sessionStorage.key", index); - } - - private async Task RaiseOnChangingAsync(string key, object data) - { - var e = new ChangingEventArgs - { - Key = key, - OldValue = await GetItemAsync(key), - NewValue = data - }; - - Changing?.Invoke(this, e); - - return e; - } - - private ChangingEventArgs RaiseOnChangingSync(string key, object data) - { - var e = new ChangingEventArgs - { - Key = key, - OldValue = ((ISyncSessionStorageService)this).GetItem(key), - NewValue = data - }; - - Changing?.Invoke(this, e); - - return e; - } - - private void RaiseOnChanged(string key, object oldValue, object data) - { - var e = new ChangedEventArgs - { - Key = key, - OldValue = oldValue, - NewValue = data - }; - - Changed?.Invoke(this, e); - } - } -} + } + + public void RemoveItem(string key) + { + if (string.IsNullOrEmpty(key)) + throw new ArgumentNullException(nameof(key)); + + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + _jSInProcessRuntime.InvokeVoid("sessionStorage.removeItem", key); + } + + public void Clear() + { + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + _jSInProcessRuntime.InvokeVoid("sessionStorage.clear"); + } + + public int Length() + { + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + return _jSInProcessRuntime.Invoke("eval", "sessionStorage.length"); + } + + public string Key(int index) + { + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + return _jSInProcessRuntime.Invoke("sessionStorage.key", index); + } + + public bool ContainKey(string key) + { + if (_jSInProcessRuntime == null) + throw new InvalidOperationException("IJSInProcessRuntime not available"); + + return _jSInProcessRuntime.Invoke("sessionStorage.hasOwnProperty", key); + } + + private async Task RaiseOnChangingAsync(string key, object data) + { + var e = new ChangingEventArgs + { + Key = key, + OldValue = await GetItemAsync(key), + NewValue = data + }; + + Changing?.Invoke(this, e); + + return e; + } + + private ChangingEventArgs RaiseOnChangingSync(string key, object data) + { + var e = new ChangingEventArgs + { + Key = key, + OldValue = ((ISyncSessionStorageService)this).GetItem(key), + NewValue = data + }; + + Changing?.Invoke(this, e); + + return e; + } + + private void RaiseOnChanged(string key, object oldValue, object data) + { + var e = new ChangedEventArgs + { + Key = key, + OldValue = oldValue, + NewValue = data + }; + + Changed?.Invoke(this, e); + } + } +} \ No newline at end of file