Skip to content

Commit 788e6b5

Browse files
Merge pull request #33 from yueyinqiu/re
scoped
2 parents 3209084 + 510f105 commit 788e6b5

File tree

6 files changed

+51
-57
lines changed

6 files changed

+51
-57
lines changed

Magic.IndexedDb/Extensions/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public static class ServiceCollectionExtensions
1313
{
1414
public static IServiceCollection AddBlazorDB(this IServiceCollection services, Action<DbStore> options)
1515
{
16+
services.TryAddScoped<IMagicDbFactory, MagicDbFactory>();
17+
1618
var dbStore = new DbStore();
1719
options(dbStore);
18-
19-
services.AddTransient<DbStore>((_) => dbStore);
20-
services.TryAddSingleton<IMagicDbFactory, MagicDbFactory>();
20+
_ = services.AddSingleton(dbStore);
2121

2222
return services;
2323
}

Magic.IndexedDb/Factories/MagicDbFactory.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,32 @@
44

55
namespace Magic.IndexedDb.Factories
66
{
7-
public class MagicDbFactory : IMagicDbFactory
7+
public class MagicDbFactory : IMagicDbFactory, IAsyncDisposable
88
{
9-
readonly IJSRuntime _jsRuntime;
9+
readonly Task<IJSObjectReference> _jsRuntime;
1010
readonly IServiceProvider _serviceProvider;
1111
readonly IDictionary<string, IndexedDbManager> _databases = new Dictionary<string, IndexedDbManager>();
1212

1313
public MagicDbFactory(IServiceProvider serviceProvider, IJSRuntime jSRuntime)
1414
{
1515
_serviceProvider = serviceProvider;
16-
_jsRuntime = jSRuntime;
16+
this._jsRuntime = jSRuntime.InvokeAsync<IJSObjectReference>(
17+
"import",
18+
"./_content/Magic.IndexedDb/magicDB.js").AsTask();
19+
}
20+
public async ValueTask DisposeAsync()
21+
{
22+
var js = await _jsRuntime;
23+
try
24+
{
25+
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
26+
await js.InvokeVoidAsync(IndexedDbFunctions.CLOSE_ALL, timeout.Token);
27+
}
28+
catch
29+
{
30+
// do nothing here
31+
}
32+
await js.DisposeAsync();
1733
}
1834

1935
public async ValueTask<IndexedDbManager> OpenAsync(
@@ -23,7 +39,7 @@ public async ValueTask<IndexedDbManager> OpenAsync(
2339
if (force || !_databases.ContainsKey(dbStore.Name))
2440
{
2541
var db = await IndexedDbManager.CreateAndOpenAsync(
26-
dbStore, _jsRuntime, cancellationToken);
42+
dbStore, await _jsRuntime, cancellationToken);
2743
_databases[dbStore.Name] = db;
2844
}
2945
return _databases[dbStore.Name];

Magic.IndexedDb/IndexDbManager.cs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Linq.Expressions;
33
using System.Reflection;
44
using System.Text.Json;
5-
using System.Threading;
65
using Magic.IndexedDb.Factories;
76
using Magic.IndexedDb.Helpers;
87
using Magic.IndexedDb.Models;
@@ -17,46 +16,29 @@ namespace Magic.IndexedDb
1716
/// <summary>
1817
/// Provides functionality for accessing IndexedDB from Blazor application
1918
/// </summary>
20-
public sealed class IndexedDbManager : IAsyncDisposable
19+
public sealed class IndexedDbManager
2120
{
2221
internal static async ValueTask<IndexedDbManager> CreateAndOpenAsync(
23-
DbStore dbStore, IJSRuntime jsRuntime,
22+
DbStore dbStore, IJSObjectReference jsRuntime,
2423
CancellationToken cancellationToken = default)
2524
{
2625
var result = new IndexedDbManager(dbStore, jsRuntime);
27-
try
28-
{
29-
await result.CallJsAsync(IndexedDbFunctions.CREATE_DB, cancellationToken, [dbStore]);
30-
return result;
31-
}
32-
catch
33-
{
34-
await result.DisposeAsync();
35-
throw;
36-
}
26+
await result.CallJsAsync(IndexedDbFunctions.CREATE_DB, cancellationToken, [dbStore]);
27+
return result;
3728
}
3829

39-
4030
readonly DbStore _dbStore;
41-
readonly Task<IJSObjectReference> _jsModule;
42-
43-
public async ValueTask DisposeAsync()
44-
{
45-
var module = await _jsModule;
46-
await module.DisposeAsync();
47-
}
31+
readonly IJSObjectReference _jsModule;
4832

4933
/// <summary>
5034
/// Ctor
5135
/// </summary>
5236
/// <param name="dbStore"></param>
5337
/// <param name="jsRuntime"></param>
54-
private IndexedDbManager(DbStore dbStore, IJSRuntime jsRuntime)
38+
private IndexedDbManager(DbStore dbStore, IJSObjectReference jsRuntime)
5539
{
5640
this._dbStore = dbStore;
57-
this._jsModule = jsRuntime.InvokeAsync<IJSObjectReference>(
58-
"import",
59-
"./_content/Magic.IndexedDb/magicDB.js").AsTask();
41+
this._jsModule = jsRuntime;
6042
}
6143

6244
// TODO: make it readonly
@@ -806,14 +788,12 @@ public Task ClearTableAsync<T>(CancellationToken cancellationToken = default) wh
806788

807789
internal async Task CallJsAsync(string functionName, CancellationToken token, object[] args)
808790
{
809-
var mod = await this._jsModule;
810-
await mod.InvokeVoidAsync(functionName, token, args);
791+
await this._jsModule.InvokeVoidAsync(functionName, token, args);
811792
}
812793

813794
internal async Task<T> CallJsAsync<T>(string functionName, CancellationToken token, object[] args)
814795
{
815-
var mod = await this._jsModule;
816-
return await mod.InvokeAsync<T>(functionName, token, args);
796+
return await this._jsModule.InvokeAsync<T>(functionName, token, args);
817797
}
818798
}
819799
}

Magic.IndexedDb/IndexedDbFunctions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Magic.IndexedDb
33
internal struct IndexedDbFunctions
44
{
55
public const string CREATE_DB = "createDb";
6+
public const string CLOSE_ALL = "closeAll";
67
public const string DELETE_DB = "deleteDb";
78
public const string ADD_ITEM = "addItem";
89
public const string BULKADD_ITEM = "bulkAddItem";

Magic.IndexedDb/wwwroot/magicDB.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ export function createDb(dbStore)
6666
db.open();
6767
}
6868

69+
export function closeAll()
70+
{
71+
const dbs = databases;
72+
databases = [];
73+
for (db of dbs)
74+
db.db.close();
75+
}
76+
6977
export async function deleteDb(dbName)
7078
{
7179
const db = await getDb(dbName);

README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,16 @@ This open source library provides an IndexedDb wrapper for C# and Blazor WebAsse
88
This code is still very young. I will be making updates for this code as I come across it. I will try my best to not depreciate or break syntax already in use. But please take note of any version updates before you use my code if you're updating it in the future. As I will not guarentee right now that I won't break your stuff if you download this version and then come back in a year and get the latest version.
99

1010
## Future Planned Features
11-
[X] Updating soon to NET 8
12-
13-
[] Reset Primary Key starting Id
14-
15-
[] Compound Key Indexing
16-
17-
[] Easy table syncing functionality with API
18-
19-
[X] Allow Any and/or All Linq statements inside the WHERE query
20-
21-
[] Handling of Nested OR conditions in where query
22-
23-
[] API like response protocal from JS. This will allow better debugging and make it easier to expand the code. I left the original weak reference system build by nwestfall, but I will be removing that code for a system I believe is easier to tame and more appropriate for the project goals.
24-
25-
[] Superior & easy version handling and table altering. Current form handles changes easily but it'd take some work to do serious data migrations. I want data migrations to be built in c#, translated to clients, and automatically handled with extreme ease. I have some really interesting protocals I've brainstormed and would like to experiment with.
26-
27-
[] Deferred execution of table joins. I want c# join abilities to properly join tables together.
28-
29-
[] Superior reflections. The current version of reflections is fine, but I've not spent much time optimizing. I need to convert more into hash sets, dictionaries, and more. This wrapper is meant to be a very expandable version that will engulf indexedDb with a great deal of capabilities. Fine tuning and perfecting the protocal I'm working on will allow some very cool features in the future.
30-
31-
[] **Long Term** - NET 8 provides AOT features that I think could be extremely abusive in a good way for this project. I believe with future NET 8 possibilites and AOT, I can move IndexedDB into being a multi thread monster and various other performance improvements. Obviously I'd make this optional. But I believe there's some very interesting possibilities I'd like to experiment with in the future.
11+
- [ ] Reset Primary Key starting Id
12+
- [ ] Compound Key Indexing
13+
- [ ] Easy table syncing functionality with API
14+
- [X] Allow Any and/or All Linq statements inside the WHERE query
15+
- [ ] Handling of Nested OR conditions in where query
16+
- [ ] API like response protocal from JS. This will allow better debugging and make it easier to expand the code. I left the original weak reference system build by nwestfall, but I will be removing that code for a system I believe is easier to tame and more appropriate for the project goals.
17+
- [ ] Superior & easy version handling and table altering. Current form handles changes easily but it'd take some work to do serious data migrations. I want data migrations to be built in c#, translated to clients, and automatically handled with extreme ease. I have some really interesting protocals I've brainstormed and would like to experiment with.
18+
- [ ] Deferred execution of table joins. I want c# join abilities to properly join tables together.
19+
- [ ] Superior reflections. The current version of reflections is fine, but I've not spent much time optimizing. I need to convert more into hash sets, dictionaries, and more. This wrapper is meant to be a very expandable version that will engulf indexedDb with a great deal of capabilities. Fine tuning and perfecting the protocal I'm working on will allow some very cool features in the future.
20+
- [ ] **Long Term** - NET 8 provides AOT features that I think could be extremely abusive in a good way for this project. I believe with future NET 8 possibilites and AOT, I can move IndexedDB into being a multi thread monster and various other performance improvements. Obviously I'd make this optional. But I believe there's some very interesting possibilities I'd like to experiment with in the future.
3221

3322
## Table of Contents
3423

0 commit comments

Comments
 (0)