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
93 lines (73 loc) · 2.88 KB
/
Copy pathInMemoryStorageProvider.cs
File metadata and controls
93 lines (73 loc) · 2.88 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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<string, string> _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<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default)
=> new(ContainKey(key));
public string GetItem(string key)
=> _dataStore.TryGetValue(key, out var value) ? value : default;
public ValueTask<string> 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<string> Keys()
=> _dataStore.Keys.ToList();
public ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
=> new(Key(index));
public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
=> new(_dataStore.Keys.ToList());
public int Length()
=> _dataStore.Count;
public ValueTask<int> 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<string> keys)
{
foreach (var key in keys)
{
_dataStore.Remove(key);
}
}
public ValueTask RemoveItemsAsync(IEnumerable<string> 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);
}
}
}