forked from Blazored/SessionStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserStorageProvider.cs
More file actions
87 lines (70 loc) · 2.81 KB
/
Copy pathBrowserStorageProvider.cs
File metadata and controls
87 lines (70 loc) · 2.81 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
using Microsoft.JSInterop;
using System;
using System.Threading.Tasks;
namespace Blazored.SessionStorage
{
internal class BrowserStorageProvider : IStorageProvider
{
private readonly IJSRuntime _jSRuntime;
private readonly IJSInProcessRuntime _jSInProcessRuntime;
public BrowserStorageProvider(IJSRuntime jSRuntime)
{
_jSRuntime = jSRuntime;
_jSInProcessRuntime = jSRuntime as IJSInProcessRuntime;
}
public ValueTask ClearAsync()
=> _jSRuntime.InvokeVoidAsync("sessionStorage.clear");
public ValueTask<string> GetItemAsync(string key)
=> _jSRuntime.InvokeAsync<string>("sessionStorage.getItem", key);
public ValueTask<string> KeyAsync(int index)
=> _jSRuntime.InvokeAsync<string>("sessionStorage.key", index);
public ValueTask<bool> ContainKeyAsync(string key)
=> _jSRuntime.InvokeAsync<bool>("sessionStorage.hasOwnProperty", key);
public ValueTask<int> LengthAsync()
=> _jSRuntime.InvokeAsync<int>("eval", "sessionStorage.length");
public ValueTask RemoveItemAsync(string key)
=> _jSRuntime.InvokeVoidAsync("sessionStorage.removeItem", key);
public ValueTask SetItemAsync(string key, string data)
=> _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", key, data);
public void Clear()
{
CheckForInProcessRuntime();
_jSInProcessRuntime.InvokeVoid("sessionStorage.clear");
}
public string GetItem(string key)
{
CheckForInProcessRuntime();
return _jSInProcessRuntime.Invoke<string>("sessionStorage.getItem", key);
}
public string Key(int index)
{
CheckForInProcessRuntime();
return _jSInProcessRuntime.Invoke<string>("sessionStorage.key", index);
}
public bool ContainKey(string key)
{
CheckForInProcessRuntime();
return _jSInProcessRuntime.Invoke<bool>("sessionStorage.hasOwnProperty", key);
}
public int Length()
{
CheckForInProcessRuntime();
return _jSInProcessRuntime.Invoke<int>("eval", "sessionStorage.length");
}
public void RemoveItem(string key)
{
CheckForInProcessRuntime();
_jSInProcessRuntime.InvokeVoidAsync("sessionStorage.removeItem", key);
}
public void SetItem(string key, string data)
{
CheckForInProcessRuntime();
_jSInProcessRuntime.InvokeVoid("sessionStorage.setItem", key, data);
}
private void CheckForInProcessRuntime()
{
if (_jSInProcessRuntime == null)
throw new InvalidOperationException("IJSInProcessRuntime not available");
}
}
}