Skip to content

Commit

Permalink
refactor(LoadModule): add logger information for load module failed (#…
Browse files Browse the repository at this point in the history
…5148)

* refactor: 复用 LoadModule 方法

* refactor: 增加日志输出

* refactor: 精简代码提高可读性

Co-Authored-By: Alex chow <[email protected]>
Co-Authored-By: Argo Zhang <[email protected]>
  • Loading branch information
ArgoZhang and densen2014 authored Jan 18, 2025
1 parent 3279623 commit 84ace2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/BootstrapBlazor/Extensions/JSModuleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public static class JSModuleExtensions
/// 导入 utility js 模块
/// </summary>
/// <param name="jsRuntime"></param>
/// <param name="version"></param>
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
public static Task<JSModule> LoadUtility(this IJSRuntime jsRuntime) => jsRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js");
public static Task<JSModule> LoadUtility(this IJSRuntime jsRuntime, string? version = null) => LoadModule(jsRuntime, "./_content/BootstrapBlazor/modules/utility.js", version);

/// <summary>
/// IJSRuntime 扩展方法 动态加载脚本 脚本目录为 modules
Expand All @@ -39,6 +40,7 @@ public static async Task<JSModule> LoadModule(this IJSRuntime jsRuntime, string
catch (JSException)
{
#if DEBUG
System.Console.WriteLine($"{nameof(LoadModule)} throw {nameof(JSException)}. import {fileName} failed");
throw;
#endif
}
Expand Down
59 changes: 25 additions & 34 deletions src/BootstrapBlazor/Utils/JSModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,23 @@ public virtual async ValueTask InvokeVoidAsync(string identifier, CancellationTo
{
paras.AddRange(args);
}
await InvokeVoidAsync();

async ValueTask InvokeVoidAsync()
try
{
try
if (jSObjectReference != null)
{
if (jSObjectReference != null)
{
await jSObjectReference.InvokeVoidAsync(identifier, cancellationToken, [.. paras]);
}
await jSObjectReference.InvokeVoidAsync(identifier, cancellationToken, [.. paras]);
}
catch (JSException)
{
}
catch (JSException)
{
#if DEBUG
System.Console.WriteLine($"identifier: {identifier} args: {string.Join(" ", args!)}");
throw;
System.Console.WriteLine($"identifier: {identifier} args: {string.Join(" ", args!)}");
throw;
#endif
}
catch (JSDisconnectedException) { }
catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }
}
catch (JSDisconnectedException) { }
catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }
}

/// <summary>
Expand Down Expand Up @@ -107,31 +102,27 @@ async ValueTask InvokeVoidAsync()
{
paras.AddRange(args!);
}
return await InvokeAsync();

async ValueTask<TValue?> InvokeAsync()
TValue? ret = default;
try
{
TValue? ret = default;
try
if (jSObjectReference != null)
{
if (jSObjectReference != null)
{
ret = await jSObjectReference.InvokeAsync<TValue?>(identifier, cancellationToken, [.. paras]);
}
ret = await jSObjectReference.InvokeAsync<TValue?>(identifier, cancellationToken, [.. paras]);
}
catch (JSException)
{
}
catch (JSException)
{
#if DEBUG
System.Console.WriteLine($"identifier: {identifier} args: {string.Join(" ", args!)}");
throw;
System.Console.WriteLine($"identifier: {identifier} args: {string.Join(" ", args!)}");
throw;
#endif
}
catch (JSDisconnectedException) { }
catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }

return ret;
}
catch (JSDisconnectedException) { }
catch (OperationCanceledException) { }
catch (ObjectDisposedException) { }

return ret;
}

/// <summary>
Expand Down

0 comments on commit 84ace2e

Please sign in to comment.