forked from Blazored/SessionStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryStorageProvider.cs
More file actions
72 lines (57 loc) · 1.98 KB
/
Copy pathInMemoryStorageProvider.cs
File metadata and controls
72 lines (57 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Blazored.SessionStorage.Testing
{
internal class InMemoryStorageProvider : IStorageProvider
{
private readonly Dictionary<string, string> _dataStore = new Dictionary<string, string>();
public void Clear()
=> _dataStore.Clear();
public ValueTask ClearAsync()
{
_dataStore.Clear();
return new ValueTask(Task.CompletedTask);
}
public bool ContainKey(string key)
=> _dataStore.ContainsKey(key);
public ValueTask<bool> ContainKeyAsync(string key)
=> new ValueTask<bool>(ContainKey(key));
public string GetItem(string key)
=> _dataStore.ContainsKey(key) ? _dataStore[key] : default;
public ValueTask<string> GetItemAsync(string key)
=> new ValueTask<string>(GetItem(key));
public string Key(int index)
=> index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key;
public ValueTask<string> KeyAsync(int index)
=> new ValueTask<string>(Key(index));
public int Length()
=> _dataStore.Count;
public ValueTask<int> LengthAsync()
=> new ValueTask<int>(Length());
public void RemoveItem(string key)
=> _dataStore.Remove(key);
public ValueTask RemoveItemAsync(string key)
{
RemoveItem(key);
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)
{
SetItem(key, data);
return new ValueTask(Task.CompletedTask);
}
}
}