forked from Blazored/SessionStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionStorageService.cs
More file actions
289 lines (220 loc) · 8.88 KB
/
Copy pathSessionStorageService.cs
File metadata and controls
289 lines (220 loc) · 8.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Blazored.SessionStorage.Serialization;
namespace Blazored.SessionStorage;
internal class SessionStorageService : ISessionStorageService, ISyncSessionStorageService
{
private readonly IStorageProvider _storageProvider;
private readonly IJsonSerializer _serializer;
public SessionStorageService(IStorageProvider storageProvider, IJsonSerializer serializer)
{
_storageProvider = storageProvider;
_serializer = serializer;
}
public ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)
=> _storageProvider.RemoveItemsAsync(keys, cancellationToken);
public async ValueTask SetItemAsync<T>(string key, T data, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
var e = await RaiseOnChangingAsync(key, data).ConfigureAwait(false);
if (e.Cancel)
return;
var serialisedData = _serializer.Serialize(data);
await _storageProvider.SetItemAsync(key, serialisedData, cancellationToken).ConfigureAwait(false);
RaiseOnChanged(key, e.OldValue, data);
}
public async ValueTask SetItemAsStringAsync(string key, string data, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (data is null)
throw new ArgumentNullException(nameof(data));
var e = await RaiseOnChangingAsync(key, data).ConfigureAwait(false);
if (e.Cancel)
return;
await _storageProvider.SetItemAsync(key, data, cancellationToken).ConfigureAwait(false);
RaiseOnChanged(key, e.OldValue, data);
}
public async ValueTask<T> GetItemAsync<T>(string key, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
var serialisedData = await _storageProvider.GetItemAsync(key, cancellationToken).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(serialisedData))
return default;
try
{
return _serializer.Deserialize<T>(serialisedData);
}
catch (JsonException e) when (e.Path == "$" && typeof(T) == typeof(string))
{
// For backward compatibility return the plain string.
// On the next save a correct value will be stored and this Exception will not happen again, for this 'key'
return (T)(object)serialisedData;
}
}
public ValueTask<string> GetItemAsStringAsync(string key, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
return _storageProvider.GetItemAsync(key, cancellationToken);
}
public ValueTask RemoveItemAsync(string key, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
return _storageProvider.RemoveItemAsync(key, cancellationToken);
}
public ValueTask ClearAsync(CancellationToken cancellationToken = default)
=> _storageProvider.ClearAsync(cancellationToken);
public ValueTask<int> LengthAsync(CancellationToken cancellationToken = default)
=> _storageProvider.LengthAsync(cancellationToken);
public ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
=> _storageProvider.KeyAsync(index, cancellationToken);
public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
=> _storageProvider.KeysAsync(cancellationToken);
public ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default)
=> _storageProvider.ContainKeyAsync(key, cancellationToken);
public void SetItem<T>(string key, T data)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
var e = RaiseOnChangingSync(key, data);
if (e.Cancel)
return;
var serialisedData = _serializer.Serialize(data);
_storageProvider.SetItem(key, serialisedData);
RaiseOnChanged(key, e.OldValue, data);
}
public void SetItemAsString(string key, string data)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (data is null)
throw new ArgumentNullException(nameof(data));
var e = RaiseOnChangingSync(key, data);
if (e.Cancel)
return;
_storageProvider.SetItem(key, data);
RaiseOnChanged(key, e.OldValue, data);
}
public T GetItem<T>(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
var serialisedData = _storageProvider.GetItem(key);
if (string.IsNullOrWhiteSpace(serialisedData))
return default;
try
{
return _serializer.Deserialize<T>(serialisedData);
}
catch (JsonException e) when (e.Path == "$" && typeof(T) == typeof(string))
{
// For backward compatibility return the plain string.
// On the next save a correct value will be stored and this Exception will not happen again, for this 'key'
return (T)(object)serialisedData;
}
}
public string GetItemAsString(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
return _storageProvider.GetItem(key);
}
public void RemoveItem(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
_storageProvider.RemoveItem(key);
}
public void RemoveItems(IEnumerable<string> keys)
{
if (keys == null)
throw new ArgumentNullException(nameof(keys));
foreach (var key in keys)
{
_storageProvider.RemoveItem(key);
}
}
public void Clear()
=> _storageProvider.Clear();
public int Length()
=> _storageProvider.Length();
public string Key(int index)
=> _storageProvider.Key(index);
public IEnumerable<string> Keys()
=> _storageProvider.Keys();
public bool ContainKey(string key)
=> _storageProvider.ContainKey(key);
public event EventHandler<ChangingEventArgs> Changing;
private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object data)
{
var e = new ChangingEventArgs
{
Key = key,
OldValue = await GetItemInternalAsync<object>(key).ConfigureAwait(false),
NewValue = data
};
Changing?.Invoke(this, e);
return e;
}
private ChangingEventArgs RaiseOnChangingSync(string key, object data)
{
var e = new ChangingEventArgs
{
Key = key,
OldValue = GetItemInternal(key),
NewValue = data
};
Changing?.Invoke(this, e);
return e;
}
private async Task<T> GetItemInternalAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var serialisedData = await _storageProvider.GetItemAsync(key).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(serialisedData))
return default;
try
{
return _serializer.Deserialize<T>(serialisedData);
}
catch (JsonException)
{
return (T)(object)serialisedData;
}
}
private object GetItemInternal(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var serialisedData = _storageProvider.GetItem(key);
if (string.IsNullOrWhiteSpace(serialisedData))
return default;
try
{
return _serializer.Deserialize<object>(serialisedData);
}
catch (JsonException)
{
return serialisedData;
}
}
public event EventHandler<ChangedEventArgs> Changed;
private void RaiseOnChanged(string key, object oldValue, object data)
{
var e = new ChangedEventArgs
{
Key = key,
OldValue = oldValue,
NewValue = data
};
Changed?.Invoke(this, e);
}
}