Skip to content

Commit 350b0e3

Browse files
authored
feat(LoadModeByName): add LoadModuleByName extension method (#5286)
* refactor: 过滤掉超时异常 * refactor: 更新代码 * test: 更新单元测试 * refactor: 增加 LoadModuleByName 扩展方法 * refactor: 使用新的扩展方法减少代码 * refactor: 重构代码 * refactor: 设置方法为公开
1 parent c001d4f commit 350b0e3

File tree

14 files changed

+30
-23
lines changed

14 files changed

+30
-23
lines changed

src/BootstrapBlazor/Components/Validate/ValidateBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public virtual void ToggleMessage(IEnumerable<ValidationResult> results)
485485

486486
private JSModule? ValidateModule { get; set; }
487487

488-
private Task<JSModule> LoadValidateModule() => JSRuntime.LoadModule("./_content/BootstrapBlazor/modules/validate.js");
488+
private Task<JSModule> LoadValidateModule() => JSRuntime.LoadModuleByName("validate");
489489

490490
/// <summary>
491491
/// 增加客户端 Tooltip 方法

src/BootstrapBlazor/Components/WebSpeech/WebSpeechService.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BootstrapBlazor.Components;
1010
/// <summary>
1111
/// Web Speech 服务
1212
/// </summary>
13-
public class WebSpeechService(IJSRuntime runtime, IComponentIdGenerator ComponentIdGenerator, ILogger<WebSpeechService> logger)
13+
public class WebSpeechService(IJSRuntime runtime, IComponentIdGenerator ComponentIdGenerator)
1414
{
1515
private JSModule? SynthesisModule { get; set; }
1616

@@ -24,9 +24,7 @@ public async Task<WebSpeechSynthesizer> CreateSynthesizerAsync()
2424
{
2525
if (SynthesisModule == null)
2626
{
27-
var moduleName = "./_content/BootstrapBlazor/modules/synthesis.js";
28-
logger.LogInformation("load module {moduleName}", moduleName);
29-
SynthesisModule = await runtime.LoadModule(moduleName);
27+
SynthesisModule = await runtime.LoadModuleByName("synthesis");
3028
}
3129
return new WebSpeechSynthesizer(SynthesisModule, ComponentIdGenerator);
3230
}
@@ -39,9 +37,7 @@ public async Task<WebSpeechRecognition> CreateRecognitionAsync()
3937
{
4038
if (RecognitionModule == null)
4139
{
42-
var moduleName = "./_content/BootstrapBlazor/modules/recognition.js";
43-
logger.LogInformation("load module {moduleName}", moduleName);
44-
RecognitionModule = await runtime.LoadModule(moduleName);
40+
RecognitionModule = await runtime.LoadModuleByName("recognition");
4541
}
4642
return new WebSpeechRecognition(RecognitionModule, ComponentIdGenerator);
4743
}

src/BootstrapBlazor/Extensions/JSModuleExtensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,23 @@ public static class JSModuleExtensions
1616
/// <param name="jsRuntime"></param>
1717
/// <param name="version"></param>
1818
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
19-
public static Task<JSModule> LoadUtility(this IJSRuntime jsRuntime, string? version = null) => LoadModule(jsRuntime, "./_content/BootstrapBlazor/modules/utility.js", version);
19+
public static Task<JSModule> LoadUtility(this IJSRuntime jsRuntime, string? version = null) => LoadModuleByName(jsRuntime, "utility", version);
2020

2121
/// <summary>
22-
/// IJSRuntime 扩展方法 动态加载脚本 脚本目录为 modules
22+
/// 通过名称导入内置脚本模块
23+
/// </summary>
24+
/// <param name="jsRuntime"></param>
25+
/// <param name="moduleName"></param>
26+
/// <param name="version"></param>
27+
/// <returns>A <see cref="Task"/><![CDATA[<]]><see cref="JSModule"/><![CDATA[>]]> 模块加载器</returns>
28+
public static Task<JSModule> LoadModuleByName(this IJSRuntime jsRuntime, string moduleName, string? version = null)
29+
{
30+
var fileName = $"./_content/BootstrapBlazor/modules/{moduleName}.js";
31+
return LoadModule(jsRuntime, fileName, version);
32+
}
33+
34+
/// <summary>
35+
/// IJSRuntime 扩展方法 动态加载脚本
2336
/// </summary>
2437
/// <param name="jsRuntime"></param>
2538
/// <param name="fileName"></param>

src/BootstrapBlazor/Services/AjaxService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AjaxService(IJSRuntime jSRuntime)
1515
[NotNull]
1616
private JSModule? _module = null;
1717

18-
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/ajax.js");
18+
private Task<JSModule> LoadModule() => jSRuntime.LoadModuleByName("ajax");
1919

2020
/// <summary>
2121
/// 调用Ajax方法发送请求

src/BootstrapBlazor/Services/Bluetooth/DefaultBluetooth.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public DefaultBluetooth(IJSRuntime jsRuntime)
4444

4545
private async Task<JSModule> LoadModule()
4646
{
47-
var module = await _runtime.LoadModule("./_content/BootstrapBlazor/modules/bt.js");
47+
var module = await _runtime.LoadModuleByName("bt");
4848

4949
IsSupport = await module.InvokeAsync<bool>("init");
5050
return module;

src/BootstrapBlazor/Services/ClipboardService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ClipboardService(IJSRuntime jSRuntime)
1313
[NotNull]
1414
private JSModule? _module = null;
1515

16-
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js");
16+
private Task<JSModule> LoadModule() => jSRuntime.LoadUtility();
1717

1818
/// <summary>
1919
/// 获取剪切板数据方法

src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ class DefaultBrowserFingerService(IJSRuntime jSRuntime) : IBrowserFingerService
1313
[NotNull]
1414
private JSModule? _module = null;
1515

16-
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js");
17-
1816
/// <summary>
1917
/// 获取剪切板数据方法
2018
/// </summary>
2119
public async Task<string?> GetFingerCodeAsync(CancellationToken token = default)
2220
{
23-
_module ??= await LoadModule();
21+
_module ??= await jSRuntime.LoadUtility();
2422
return await _module.InvokeAsync<string?>("getFingerCode", token);
2523
}
2624
}

src/BootstrapBlazor/Services/DefaultGeoLocationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public DefaultGeoLocationService(IJSRuntime jsRuntime)
2828
Interop = DotNetObjectReference.Create(this);
2929
}
3030

31-
private Task<JSModule> LoadModule() => JSRuntime.LoadModule("./_content/BootstrapBlazor/modules/geo.js");
31+
private Task<JSModule> LoadModule() => JSRuntime.LoadModuleByName("geo");
3232

3333
/// <summary>
3434
/// get the current position of the device

src/BootstrapBlazor/Services/EyeDropperService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class EyeDropperService(IJSRuntime jSRuntime)
1919
/// <returns></returns>
2020
public async Task<string?> PickAsync(CancellationToken token = default)
2121
{
22-
_module ??= await jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/eye-dropper.js");
22+
_module ??= await jSRuntime.LoadModuleByName("eye-dropper");
2323
return await _module.InvokeAsync<string?>("open", token);
2424
}
2525
}

src/BootstrapBlazor/Services/FullScreenService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class FullScreenService(IJSRuntime jSRuntime)
2121
/// <returns></returns>
2222
public async Task Toggle(FullScreenOption? option = null, CancellationToken token = default)
2323
{
24-
_module ??= await jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/fullscreen.js");
24+
_module ??= await jSRuntime.LoadModuleByName("fullscreen");
2525
await _module.InvokeVoidAsync("toggle", token, option);
2626
}
2727
}

0 commit comments

Comments
 (0)