-
Notifications
You must be signed in to change notification settings - Fork 36
/
IndexedDb.razor
132 lines (110 loc) · 4.5 KB
/
IndexedDb.razor
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
@using BlazorWorker.Demo.IoCExample
@using BlazorWorker.Extensions.JSRuntime
@using TG.Blazor.IndexedDB
@inject IWorkerFactory workerFactory
@inject IndexedDBManager indexedDBManager
<div class="row">
<div class="col-5 col-xs-12">
<h1>.NET Worker Thread Service with IndexedDB</h1>
This page demonstrates IndexedDB.
In this example, the class @(nameof(MyIndexDBServiceStartup)) is a factory class for @(nameof(MyIndexDBService)).
The factory is implemented using ServiceCollection as an IoC Container.
The example is using the library <a href="https://github.com/wtulloch/Blazor.IndexedDB">TG.Blazor.IndexedDB</a>.
<br /><br />
<button disabled=@RunDisabled @onclick=OnClick class="btn btn-primary">Run test</button><br /><br />
<br />
<br />
<strong>Output:</strong>
<hr />
<pre>
@output
</pre>
</div>
<div class="col-7 col-xs-12">
<GithubSource RelativePath="Pages/IndexedDb.razor" />
</div>
</div>
@code {
string output = "";
IWorker worker;
IWorkerBackgroundService<MyIndexDBServiceStartup> startupService;
string canDisposeWorker => worker == null ? null : "disabled";
string RunDisabled => Running ? "disabled" : null;
bool Running = false;
public async Task OnClick(EventArgs _)
{
Running = true;
//output = "";
var rn = Environment.NewLine;
try
{
if (worker == null)
{
worker = await workerFactory.CreateAsync();
}
var sw = new System.Diagnostics.Stopwatch();
IWorkerBackgroundService<MyIndexDBService> myIocIndexedDbService;
if (startupService == null)
{
output = $"{rn}{LogDate()} Creating background service Startup class...";
StateHasChanged();
sw.Start();
startupService = await worker.CreateBackgroundServiceAsync<MyIndexDBServiceStartup>();
output += $"{rn}{LogDate()} Background service created in {sw.ElapsedMilliseconds}ms";
StateHasChanged();
}
output += $"{rn}{LogDate()} Resolving instance...";
myIocIndexedDbService = await startupService.CreateBackgroundServiceAsync(startup => startup.Resolve<MyIndexDBService>());
await using (myIocIndexedDbService)
{
long id;
var storeName = indexedDBManager.Stores[0].Name;
output += $"{rn}{LogDate()} Retrieving test person from store '{storeName}'...";
var testPerson = (await this.indexedDBManager.GetRecords<Person>(storeName)).FirstOrDefault();
if (testPerson == null)
{
output += $"{rn}{LogDate()} No test person present, creating a test person...";
await InvokeAsync(StateHasChanged);
await this.indexedDBManager.AddRecord<Person>(
new StoreRecord<Person>()
{
Storename = storeName,
Data = new Person() { Name = "Morgoff" }
});
output += $"{rn}{LogDate()} Test person record added. Verifying that it can be retrieved on the main thread...";
testPerson = (await this.indexedDBManager.GetRecords<Person>(storeName)).FirstOrDefault();
output += $"{rn}{LogDate()} Retrieved = {testPerson?.Name ??"N/A" }";
}
else
{
output += $"{rn}{LogDate()} Test person present.";
await InvokeAsync(StateHasChanged);
}
id = testPerson.Id.Value;
output += $"{rn}{LogDate()} Trying to to get the name of the test person using the worker...";
await InvokeAsync(StateHasChanged);
output += $"{rn}{LogDate()} PersonName() = {await myIocIndexedDbService.RunAsync(s => s.GetPersonName(id))}";
}
StateHasChanged();
}
catch (Exception e)
{
output += $"{rn}Error = {e}";
InvokeAsync(StateHasChanged);
}
finally
{
Running = false;
}
}
public async Task OnDisposeWorker()
{
await worker.DisposeAsync();
worker = null;
startupService = null;
}
private string LogDate()
{
return DateTime.Now.ToString("HH:mm:ss:fff");
}
}