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
190 lines (141 loc) · 6.24 KB
/
Copy pathSessionStorageService.cs
File metadata and controls
190 lines (141 loc) · 6.24 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
using Blazored.SessionStorage.StorageOptions;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace Blazored.SessionStorage
{
public class SessionStorageService : ISessionStorageService, ISyncSessionStorageService
{
private readonly IJSRuntime _jSRuntime;
private readonly IJSInProcessRuntime _jSInProcessRuntime;
private readonly JsonSerializerOptions _jsonOptions;
public event EventHandler<ChangingEventArgs> Changing;
public event EventHandler<ChangedEventArgs> Changed;
public SessionStorageService(IJSRuntime jSRuntime, IOptions<SessionStorageOptions> options)
{
_jSRuntime = jSRuntime;
_jsonOptions = options.Value.JsonSerializerOptions;
_jSInProcessRuntime = jSRuntime as IJSInProcessRuntime;
}
public async Task SetItemAsync<T>(string key, T data)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var e = await RaiseOnChangingAsync(key, data);
if (e.Cancel)
return;
var serialisedData = JsonSerializer.Serialize(data, _jsonOptions);
await _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", key, serialisedData);
RaiseOnChanged(key, e.OldValue, data);
}
public async Task<T> GetItemAsync<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var serialisedData = await _jSRuntime.InvokeAsync<string>("sessionStorage.getItem", key);
if (serialisedData == null)
return default;
return JsonSerializer.Deserialize<T>(serialisedData, _jsonOptions);
}
public async Task RemoveItemAsync(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
await _jSRuntime.InvokeAsync<object>("sessionStorage.removeItem", key);
}
public async Task ClearAsync() => await _jSRuntime.InvokeAsync<object>("sessionStorage.clear");
public async Task<int> LengthAsync() => await _jSRuntime.InvokeAsync<int>("eval", "sessionStorage.length");
public async Task<string> KeyAsync(int index) => await _jSRuntime.InvokeAsync<string>("sessionStorage.key", index);
public async Task<bool> ContainKeyAsync(string key) => await _jSRuntime.InvokeAsync<bool>("sessionStorage.hasOwnProperty", key);
public void SetItem<T>(string key, T data)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
var e = RaiseOnChangingSync(key, data);
if (e.Cancel)
return;
var serialisedData = JsonSerializer.Serialize(data, _jsonOptions);
_jSInProcessRuntime.InvokeVoid("sessionStorage.setItem", key, serialisedData);
RaiseOnChanged(key, e.OldValue, data);
}
public T GetItem<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
var serialisedData = _jSInProcessRuntime.Invoke<string>("sessionStorage.getItem", key);
if (serialisedData == null)
return default;
return JsonSerializer.Deserialize<T>(serialisedData, _jsonOptions);
}
public void RemoveItem(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
_jSInProcessRuntime.InvokeVoid("sessionStorage.removeItem", key);
}
public void Clear()
{
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
_jSInProcessRuntime.InvokeVoid("sessionStorage.clear");
}
public int Length()
{
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
return _jSInProcessRuntime.Invoke<int>("eval", "sessionStorage.length");
}
public string Key(int index)
{
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
return _jSInProcessRuntime.Invoke<string>("sessionStorage.key", index);
}
public bool ContainKey(string key)
{
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
return _jSInProcessRuntime.Invoke<bool>("sessionStorage.hasOwnProperty", key);
}
private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object data)
{
var e = new ChangingEventArgs
{
Key = key,
OldValue = await GetItemAsync<object>(key),
NewValue = data
};
Changing?.Invoke(this, e);
return e;
}
private ChangingEventArgs RaiseOnChangingSync(string key, object data)
{
var e = new ChangingEventArgs
{
Key = key,
OldValue = ((ISyncSessionStorageService)this).GetItem<object>(key),
NewValue = data
};
Changing?.Invoke(this, e);
return e;
}
private void RaiseOnChanged(string key, object oldValue, object data)
{
var e = new ChangedEventArgs
{
Key = key,
OldValue = oldValue,
NewValue = data
};
Changed?.Invoke(this, e);
}
}
}