-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from Tewr/feature/IndexedDB-II
Consistent versions, and Another fix to IndexedDb tests and demo
- Loading branch information
Showing
13 changed files
with
301 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.JSInterop; | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
using TG.Blazor.IndexedDB; | ||
|
||
namespace BlazorWorker.Demo.IoCExample | ||
{ | ||
public class MyIndexDBService | ||
{ | ||
private readonly IJSRuntime jsRuntime; | ||
private readonly IndexedDBManager indexedDBManager; | ||
private bool isIndexDBManagerInitialized; | ||
|
||
public MyIndexDBService( | ||
IJSRuntime jsRuntime, | ||
IndexedDBManager indexDBManager) | ||
{ | ||
//Console.WriteLine($"{nameof(MyIndexDBService)} instance created"); | ||
this.jsRuntime = jsRuntime; | ||
this.indexedDBManager = indexDBManager; | ||
} | ||
|
||
public async Task<string> GetPersonName(long id) | ||
{ | ||
try | ||
{ | ||
//Console.WriteLine($"{nameof(GetPersonName)}({id}) called."); | ||
await EnsureInitializedAsync(); | ||
//Console.WriteLine($"{nameof(GetPersonName)}({id}): Get Store name..."); | ||
var storeName = indexedDBManager.Stores[0].Name; | ||
var testPersons = (await this.indexedDBManager.GetRecords<Person>(storeName)); | ||
foreach (var item in testPersons) | ||
{ | ||
if (item.Id == id) | ||
{ | ||
return item.Name ?? "[empty name]"; | ||
} | ||
} | ||
|
||
return "N/A"; | ||
|
||
} | ||
catch (System.Exception e) | ||
{ | ||
Console.Error.WriteLine($"{nameof(GetPersonName)} :{e}"); | ||
throw; | ||
} | ||
} | ||
|
||
private async Task EnsureInitializedAsync() | ||
{ | ||
if (!isIndexDBManagerInitialized) | ||
{ | ||
// The following is a workaround as indexedDb.Blazor.js explicitly references "window" | ||
await this.jsRuntime.InvokeVoidAsync("eval", "(() => { self.window = self; return null; })()"); | ||
await this.jsRuntime.InvokeVoidAsync("importLocalScripts", "_content/TG.Blazor.IndexedDB/indexedDb.Blazor.js"); | ||
|
||
isIndexDBManagerInitialized = true; | ||
} | ||
} | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/BlazorWorker.Demo.IoCExample/MyIndexDBServiceStartup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using BlazorWorker.Extensions.JSRuntime; | ||
using BlazorWorker.WorkerCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace BlazorWorker.Demo.IoCExample | ||
{ | ||
public class MyIndexDBServiceStartup | ||
{ | ||
private readonly IServiceProvider serviceProvider; | ||
private readonly IWorkerMessageService workerMessageService; | ||
|
||
/// <summary> | ||
/// The constructor uses the built-in injection for library-native services such as the <see cref="IWorkerMessageService"/>. | ||
/// </summary> | ||
/// <param name="workerMessageService"></param> | ||
public MyIndexDBServiceStartup(IWorkerMessageService workerMessageService) | ||
{ | ||
this.workerMessageService = workerMessageService; | ||
serviceProvider = ServiceCollectionHelper.BuildServiceProviderFromMethod(Configure); | ||
} | ||
|
||
public T Resolve<T>()=> serviceProvider.GetService<T>(); | ||
|
||
public void Configure(IServiceCollection services) | ||
{ | ||
services.AddTransient<IMyServiceDependency, MyServiceDependency>() | ||
.AddBlazorWorkerJsRuntime() | ||
.AddSingleton<MyIndexDBService>() | ||
.AddSingleton(workerMessageService) | ||
.AddIndexedDbDemoPersonConfig(); | ||
} | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/BlazorWorker.Demo.IoCExample/SetupIndexedDBExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Collections.Generic; | ||
using TG.Blazor.IndexedDB; | ||
|
||
namespace BlazorWorker.Demo.IoCExample | ||
{ | ||
public static class SetupIndexedDBExtensions | ||
{ | ||
public static IServiceCollection AddIndexedDbDemoPersonConfig(this IServiceCollection services) | ||
{ | ||
services.AddIndexedDB(dbStore => | ||
{ | ||
dbStore.DbName = "TheFactory"; //example name | ||
dbStore.Version = 1; | ||
|
||
dbStore.Stores.Add(new StoreSchema | ||
{ | ||
Name = "TestPersons", | ||
PrimaryKey = new IndexSpec { Name = "id", KeyPath = "id", Auto = true }, | ||
Indexes = new List<IndexSpec> | ||
{ | ||
new IndexSpec{ Name = "name", KeyPath = "name", Auto = false }, | ||
} | ||
}); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
|
||
public class Person | ||
{ | ||
public long? Id { get; set; } | ||
public string Name { get; set; } | ||
|
||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/BlazorWorker.Demo/Net8/Client/BlazorWorker.Demo.Client/Pages/IndexedDb.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page "/IndexedDb" | ||
<BlazorWorker.Demo.SharedPages.Pages.IndexedDb /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/BlazorWorker.Demo/SharedPages/Pages/IndexedDb.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
@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; | ||
|
||
|
||
var serviceCollectionDependencies = new string[] { | ||
"Microsoft.Extensions.DependencyInjection.Abstractions.dll" | ||
#if NET5_0_OR_GREATER | ||
,"System.Diagnostics.Tracing.dll" | ||
#endif | ||
#if NET6_0_OR_GREATER | ||
,"Microsoft.Extensions.DependencyInjection.dll" | ||
#endif | ||
}; | ||
|
||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters