Created
November 24, 2020 23:01
-
-
Save RobertBouillon/8bf41f0def62d8f035a40d36d0d3a3dc to your computer and use it in GitHub Desktop.
Blazor dictionary implementation of Local Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.JSInterop; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Collections; | |
namespace Spin.Blazor | |
{ | |
public class BrowserStorage : IDictionary<string, string> | |
{ | |
private static IJSInProcessRuntime _js; | |
public BrowserStorageType Type { get; } | |
private string _prefix; | |
public BrowserStorage(BrowserStorageType type, IJSInProcessRuntime js) | |
{ | |
Type = type; | |
_js = js; | |
_prefix = Type.ToString().ToLower() + "Storage"; | |
} | |
public string this[string key] | |
{ | |
get => _js.Invoke<string>($"{_prefix}.getItem", key); | |
set => _js.Invoke<string>($"{_prefix}.setItem", key, value); | |
} | |
public string this[int index] | |
{ | |
get => this[GetKey(index)]; | |
set => this[GetKey(index)] = value; | |
} | |
private string GetKey(int index) => _js.Invoke<string>($"{_prefix}.key", index); | |
public ICollection<string> Keys => Enumerable.Range(0, Count).Select(x => GetKey(x)).ToList(); | |
public ICollection<string> Values => Enumerable.Range(0, Count).Select(x => this[GetKey(x)]).ToList(); | |
public int Count => _js.Invoke<int>("eval", $"{_prefix}.length"); | |
public bool IsReadOnly => false; | |
public void Add(string key, string value) | |
{ | |
if (ContainsKey(key)) | |
throw new ArgumentException("An element with the same key already exists"); | |
this[key] = value; | |
} | |
public void Add(KeyValuePair<string, string> item) => Add(item.Key, item.Value); | |
public void Clear() => _js.InvokeVoid($"{_prefix}.clear"); | |
public bool Contains(KeyValuePair<string, string> item) => TryGetValue(item.Key, out var value) && item.Value == value; | |
public bool ContainsKey(string key) => Keys.Contains(key); | |
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) => Array.Copy(this.ToArray(), array, Count); | |
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() | |
{ | |
for (int i = 0; i < Count; i++) | |
{ | |
var key = GetKey(i); | |
yield return new KeyValuePair<string, string>(key, this[key]); | |
} | |
} | |
public bool Remove(string key) | |
{ | |
if (!ContainsKey(key)) | |
return false; | |
_js.InvokeVoid($"{_prefix}.removeItem", key); | |
return true; | |
} | |
public bool Remove(KeyValuePair<string, string> item) | |
{ | |
if (!Contains(item)) | |
return false; | |
Remove(item.Key); | |
return true; | |
} | |
public bool TryGetValue(string key, out string value) | |
{ | |
value = this[key]; | |
return ContainsKey(key); | |
} | |
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment