Skip to content

Commit abb15d2

Browse files
lots of updates
1 parent 333fdad commit abb15d2

28 files changed

+698
-404
lines changed

Magic.IndexedDb/Cache.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ namespace Magic.IndexedDb
88
{
99
internal static class Cache
1010
{
11+
public static long JsMessageSizeBytes { get; set; } = 31000;
1112
/// <summary>
1213
/// this is the wwwroot path of "./magicDB.js" for importing the script
1314
/// </summary>
14-
public const string MagicDbJsImportPath = "./magicDB.js";
15+
public const string MagicDbJsImportPath = "/magicDB.js";
1516
}
1617
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Magic.IndexedDb
9+
{
10+
public static class MagicCompoundExtension
11+
{
12+
public static IMagicCompoundIndex Create<T>(params Expression<Func<T, object>>[] keySelectors)
13+
{
14+
return InternalMagicCompoundIndex<T>.Create(keySelectors);
15+
}
16+
}
17+
}

Magic.IndexedDb/Extensions/MagicJsInvoke.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ internal async Task CallJsAsync(string modulePath, string functionName,
3434
return await MagicStreamJsAsync<T>(modulePath, functionName, token, args) ?? default;
3535
}
3636

37+
///
38+
internal async Task CallInvokeDefaultJsAsync(string modulePath, string functionName,
39+
params object[] args)
40+
{
41+
42+
var throwAway = await CallInvokeDefaultJsAsync<bool>(modulePath, functionName, args);
43+
44+
return;
45+
}
46+
47+
/// <summary>
48+
/// Utilizes InvokeAsync normally with no special serialization or streaming.
49+
/// </summary>
50+
/// <typeparam name="T"></typeparam>
51+
/// <param name="modulePath"></param>
52+
/// <param name="functionName"></param>
53+
/// <param name="token"></param>
54+
/// <param name="args"></param>
55+
/// <returns></returns>
56+
internal async Task<T?> CallInvokeDefaultJsAsync<T>(string modulePath, string functionName,
57+
params object[] args)
58+
{
59+
var response = await _jsModule.InvokeAsync<T>("JsHandler", modulePath, functionName, args);
60+
return response;
61+
}
62+
3763
internal async IAsyncEnumerable<T?> CallYieldJsAsync<T>(
3864
string modulePath,
3965
string functionName,
@@ -90,7 +116,7 @@ internal async Task CallJsAsync(string modulePath, string functionName,
90116

91117
// Send to JS
92118
var responseStreamRef = await _jsModule.InvokeAsync<IJSStreamReference>("streamedJsHandler", token,
93-
streamRef, instanceId, DotNetObjectReference.Create(this));
119+
streamRef, instanceId, DotNetObjectReference.Create(this), Cache.JsMessageSizeBytes);
94120

95121
// 🚀 Convert the stream reference back to JSON in C#
96122
await using var responseStream = await responseStreamRef.OpenReadStreamAsync(long.MaxValue, token);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Magic.IndexedDb.Interfaces;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Magic.IndexedDb
10+
{
11+
public class MagicTableTool<T> where T : class, IMagicTableBase, new()
12+
{
13+
protected CreateCompoundIndex CreateCompoundIndex(params Expression<Func<T, object>>[] keySelectors)
14+
{
15+
return MagicCompoundExtension.Create<T>(keySelectors);
16+
}
17+
}
18+
19+
}

Magic.IndexedDb/Extensions/ServiceCollectionExtensions.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,33 @@
99
using System.Text;
1010
using System.Threading.Tasks;
1111

12-
namespace Magic.IndexedDb.Extensions
12+
namespace Magic.IndexedDb
1313
{
14+
public enum BlazorInteropMode : long
15+
{
16+
/// <summary>
17+
/// SignalR default interop send/receive is 32 KB.
18+
/// This will default to 31 KB for safety.
19+
/// </summary>
20+
SignalR = 31 * 1024, // 31 KB in bytes
21+
22+
/// <summary>
23+
/// WASM default interop send/receive is 16 MB.
24+
/// This will default to 15 MB for safety.
25+
/// </summary>
26+
WASM = 15 * 1024 * 1024 // 15 MB in bytes
27+
}
28+
1429
public static class ServiceCollectionExtensions
1530
{
16-
public static IServiceCollection AddBlazorDB(this IServiceCollection services, Action<DbStore> options)
31+
public static IServiceCollection AddBlazorDB(this IServiceCollection services, BlazorInteropMode interoptMode)
1732
{
18-
services.TryAddScoped<IMagicManager, IndexedDbManager>();
33+
return services.AddBlazorDB((long)interoptMode);
34+
}
1935

20-
var dbStore = new DbStore();
21-
options(dbStore);
22-
_ = services.AddSingleton(dbStore);
36+
public static IServiceCollection AddBlazorDB(this IServiceCollection services, long jsMessageSizeBytes)
37+
{
38+
services.AddSingleton<IMagicDbFactory>(sp => new MagicDbFactory(jsMessageSizeBytes));
2339

2440
MagicValidator.ValidateTables();
2541

Magic.IndexedDb/Factories/EncryptionFactory.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

Magic.IndexedDb/Factories/IEncryptionFactory.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

Magic.IndexedDb/Factories/IMagicDbFactory.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ namespace Magic.IndexedDb
66
public interface IMagicDbFactory
77
{
88

9+
/// <summary>
10+
/// Allows manually inserting database names and schema names via strings.
11+
/// Please be careful, Magic IndexDB can't protect you from potential issues
12+
/// if you use this.
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
/// <param name="databaseNameOverride"></param>
16+
/// <param name="schemaNameOverride"></param>
17+
/// <returns></returns>
18+
ValueTask<IMagicQuery<T>> QueryOverride<T>(string? databaseNameOverride = null,
19+
string? schemaNameOverride = null) where T : class, IMagicTableBase, new();
20+
921
/// <summary>
1022
/// Opens a ready query to utilize IndexDB database and capabilities utilizing LINQ to IndexDB.
1123
/// Use example: IMagicQuery<Person> query = await _MagicDb.Query<Person>();
@@ -14,7 +26,27 @@ public interface IMagicDbFactory
1426
/// <param name="databaseNameOverride"></param>
1527
/// <param name="schemaNameOverride"></param>
1628
/// <returns></returns>
17-
ValueTask<IMagicQuery<T>> Query<T>(string? databaseNameOverride = null, string? schemaNameOverride = null) where T : class;
29+
ValueTask<IMagicQuery<T>> Query<T>()
30+
where T : class, IMagicTableBase, new();
31+
32+
/// <summary>
33+
/// Opens a query for a table to a specified database.
34+
/// </summary>
35+
/// <typeparam name="T"></typeparam>
36+
/// <param name="dbSetSelector"></param>
37+
/// <returns></returns>
38+
ValueTask<IMagicQuery<T>> Query<T>(Func<T, IndexedDbSet> dbSetSelector) where T : class, IMagicTableBase, new();
39+
40+
/// <summary>
41+
/// Utilize any Database you want, but be careful that it's assigned!
42+
/// Highly suggested you utilize `Query<T>(Func<T, IndexedDbSet> dbSetSelector)`
43+
/// </summary>
44+
/// <typeparam name="T"></typeparam>
45+
/// <param name="indexedDbSet"></param>
46+
/// <returns></returns>
47+
ValueTask<IMagicQuery<T>> Query<T>(IndexedDbSet indexedDbSet)
48+
where T : class, IMagicTableBase, new();
49+
1850
Task<QuotaUsage> GetStorageEstimateAsync(CancellationToken cancellationToken = default);
1951
}
2052
}

0 commit comments

Comments
 (0)