using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Blazored.SessionStorage.TestExtensions { internal class InMemoryStorageProvider : IStorageProvider { private readonly Dictionary _dataStore = new(); public void Clear() => _dataStore.Clear(); public ValueTask ClearAsync(CancellationToken cancellationToken = default) { _dataStore.Clear(); return new ValueTask(Task.CompletedTask); } public bool ContainKey(string key) => _dataStore.ContainsKey(key); public ValueTask ContainKeyAsync(string key, CancellationToken cancellationToken = default) => new(ContainKey(key)); public string GetItem(string key) => _dataStore.TryGetValue(key, out var value) ? value : default; public ValueTask GetItemAsync(string key, CancellationToken cancellationToken = default) => new(GetItem(key)); public string Key(int index) => index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key; public IEnumerable Keys() => _dataStore.Keys.ToList(); public ValueTask KeyAsync(int index, CancellationToken cancellationToken = default) => new(Key(index)); public ValueTask> KeysAsync(CancellationToken cancellationToken = default) => new(_dataStore.Keys.ToList()); public int Length() => _dataStore.Count; public ValueTask LengthAsync(CancellationToken cancellationToken = default) => new(Length()); public void RemoveItem(string key) => _dataStore.Remove(key); public ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default) { RemoveItem(key); return new ValueTask(Task.CompletedTask); } public void RemoveItems(IEnumerable keys) { foreach (var key in keys) { _dataStore.Remove(key); } } public ValueTask RemoveItemsAsync(IEnumerable keys, CancellationToken cancellationToken = default) { RemoveItems(keys); return new ValueTask(Task.CompletedTask); } public void SetItem(string key, string data) { if (_dataStore.ContainsKey(key)) { _dataStore[key] = data; } else { _dataStore.Add(key, data); } } public ValueTask SetItemAsync(string key, string data, CancellationToken cancellationToken = default) { SetItem(key, data); return new ValueTask(Task.CompletedTask); } } }