Repository navigation
Expand file tree
/
Copy pathSessionStorageService.cs
More file actions
172 lines (128 loc) · 5.43 KB
/
Copy pathSessionStorageService.cs
File metadata and controls
172 lines (128 loc) · 5.43 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
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;
public SessionStorageService(IJSRuntime jSRuntime)
{
_jSRuntime = jSRuntime;
_jSInProcessRuntime = jSRuntime as IJSInProcessRuntime;
}
public async Task SetItemAsync(string key, object data)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var e = await RaiseOnChangingAsync(key, data);
if (e.Cancel)
return;
await _jSRuntime.InvokeAsync<object>("sessionStorage.setItem", key, JsonSerializer.Serialize(data));
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(T);
return JsonSerializer.Deserialize<T>(serialisedData);
}
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 void SetItem(string key, object 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;
_jSInProcessRuntime.Invoke<object>("sessionStorage.setItem", key, JsonSerializer.Serialize(data));
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(T);
return JsonSerializer.Deserialize<T>(serialisedData);
}
public void RemoveItem(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
_jSInProcessRuntime.Invoke<object>("sessionStorage.removeItem", key);
}
public void Clear()
{
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
_jSInProcessRuntime.Invoke<object>("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 event EventHandler<ChangingEventArgs> Changing;
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;
}
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);
}
}
}