From 352122fc46bf4068fea03f5a9a3b64afb32da80a Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sun, 14 Jun 2020 23:01:08 +0430 Subject: [PATCH] Add ContainKeyAsync and Add ContainKey --- .../ISessionStorageService.cs | 7 +++++++ .../ISyncSessionStorageService.cs | 7 +++++++ .../SessionStorageService.cs | 13 +++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Blazored.SessionStorage/ISessionStorageService.cs b/src/Blazored.SessionStorage/ISessionStorageService.cs index 97724f7..ed6724c 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); diff --git a/src/Blazored.SessionStorage/ISyncSessionStorageService.cs b/src/Blazored.SessionStorage/ISyncSessionStorageService.cs index a994f6a..6271c9c 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); diff --git a/src/Blazored.SessionStorage/SessionStorageService.cs b/src/Blazored.SessionStorage/SessionStorageService.cs index efb0922..9247008 100644 --- a/src/Blazored.SessionStorage/SessionStorageService.cs +++ b/src/Blazored.SessionStorage/SessionStorageService.cs @@ -65,7 +65,9 @@ public async Task RemoveItemAsync(string key) 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 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) { @@ -136,7 +138,14 @@ public string Key(int index) 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) {