From 80e4fc5f194e0465d42c7722f8def66281696e3e Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Tue, 29 Oct 2024 13:00:54 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20AjaxServic?= =?UTF-8?q?e=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Ajax/Ajax.cs | 47 ---------- .../Components/Ajax/AjaxService.cs | 86 ------------------- src/BootstrapBlazor/Services/AjaxService.cs | 39 +++++++++ 3 files changed, 39 insertions(+), 133 deletions(-) delete mode 100644 src/BootstrapBlazor/Components/Ajax/Ajax.cs delete mode 100644 src/BootstrapBlazor/Components/Ajax/AjaxService.cs create mode 100644 src/BootstrapBlazor/Services/AjaxService.cs diff --git a/src/BootstrapBlazor/Components/Ajax/Ajax.cs b/src/BootstrapBlazor/Components/Ajax/Ajax.cs deleted file mode 100644 index a91131bcf88..00000000000 --- a/src/BootstrapBlazor/Components/Ajax/Ajax.cs +++ /dev/null @@ -1,47 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License -// See the LICENSE file in the project root for more information. -// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone - -using System.Text.Json; - -namespace BootstrapBlazor.Components; - -/// -/// Ajax 组件 -/// -[BootstrapModuleAutoLoader(ModuleName = "ajax", AutoInvokeInit = false, AutoInvokeDispose = false)] -public class Ajax : BootstrapModuleComponentBase -{ - [Inject] - [NotNull] - private AjaxService? AjaxService { get; set; } - - /// - /// OnInitialized 方法 - /// - protected override void OnInitialized() - { - base.OnInitialized(); - AjaxService.Register(this, InvokeAsync); - AjaxService.RegisterGoto(this, Goto); - } - - private Task InvokeAsync(AjaxOption option) => InvokeAsync("execute", option); - - private Task Goto(string url) => InvokeVoidAsync("goto", url); - - /// - /// - /// - protected override async ValueTask DisposeAsync(bool disposing) - { - if (disposing) - { - AjaxService.UnRegister(this); - AjaxService.UnRegisterGoto(this); - } - - await base.DisposeAsync(disposing); - } -} diff --git a/src/BootstrapBlazor/Components/Ajax/AjaxService.cs b/src/BootstrapBlazor/Components/Ajax/AjaxService.cs deleted file mode 100644 index 6a0f670e3c2..00000000000 --- a/src/BootstrapBlazor/Components/Ajax/AjaxService.cs +++ /dev/null @@ -1,86 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License -// See the LICENSE file in the project root for more information. -// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone - -using System.Text.Json; - -namespace BootstrapBlazor.Components; - -/// -/// Ajax服务类 -/// -public class AjaxService -{ - /// - /// 获得 回调委托缓存集合 - /// - private List<(IComponent Key, Func> Callback)> Cache { get; } = []; - - /// - /// 获得 跳转其他页面的回调委托缓存集合 - /// - private List<(IComponent Key, Func Callback)> GotoCache { get; } = []; - - /// - /// 注册服务 - /// - /// - /// - internal void Register(IComponent key, Func> callback) => Cache.Add((key, callback)); - - /// - /// 注销事件 - /// - internal void UnRegister(IComponent key) - { - var item = Cache.FirstOrDefault(i => i.Key == key); - if (item.Key != null) - { - Cache.Remove(item); - } - } - - /// - /// 注册服务 - /// - /// - /// - internal void RegisterGoto(IComponent key, Func callback) => GotoCache.Add((key, callback)); - - /// - /// 注销事件 - /// - internal void UnRegisterGoto(IComponent key) - { - var item = GotoCache.FirstOrDefault(i => i.Key == key); - if (item.Key != null) - { - GotoCache.Remove(item); - } - } - - /// - /// 调用Ajax方法发送请求 - /// - /// - /// - public async Task InvokeAsync(AjaxOption option) - { - var cb = Cache.FirstOrDefault().Callback; - return cb == null ? default : await cb.Invoke(option); - } - - /// - /// 调用 Goto 方法跳转其他页面 - /// - /// - public async Task Goto(string url) - { - var cb = GotoCache.FirstOrDefault().Callback; - if (cb != null) - { - await cb.Invoke(url); - } - } -} diff --git a/src/BootstrapBlazor/Services/AjaxService.cs b/src/BootstrapBlazor/Services/AjaxService.cs new file mode 100644 index 00000000000..a1898b9cc27 --- /dev/null +++ b/src/BootstrapBlazor/Services/AjaxService.cs @@ -0,0 +1,39 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using System.Text.Json; + +namespace BootstrapBlazor.Components; + +/// +/// Ajax 服务类 +/// +public class AjaxService(IJSRuntime jSRuntime) +{ + [NotNull] + private JSModule? _module = null; + + private Task LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/ajax.js"); + + /// + /// 调用Ajax方法发送请求 + /// + public async Task InvokeAsync(AjaxOption option, CancellationToken token = default) + { + _module ??= await LoadModule(); + return await _module.InvokeAsync("execute", token, option); + } + + /// + /// 调用 Goto 方法跳转其他页面 + /// + /// + /// + public async Task Goto(string url, CancellationToken token = default) + { + _module ??= await LoadModule(); + await _module.InvokeVoidAsync("goto", token, url); + } +} From ee226dd13ef582503eafdd53fc29029174cdf07c Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Tue, 29 Oct 2024 13:01:06 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20Ajax=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/BaseComponents/BootstrapBlazorRoot.razor.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs index 00b4d75ac5b..37be8e96c71 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs @@ -104,9 +104,6 @@ private static RenderFragment RenderComponents() => builder => builder.OpenComponent(0); builder.CloseComponent(); - builder.OpenComponent(1); - builder.CloseComponent(); - builder.OpenComponent(2); builder.CloseComponent(); From 186f40913e52c59d5a8710f19c73b3e42e827c5d Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Tue, 29 Oct 2024 13:01:14 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=E5=AE=8C=E5=96=84=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AjaxTest.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/UnitTest/Components/AjaxTest.cs b/test/UnitTest/Components/AjaxTest.cs index 7e30ad088a1..98222ad4c14 100644 --- a/test/UnitTest/Components/AjaxTest.cs +++ b/test/UnitTest/Components/AjaxTest.cs @@ -22,9 +22,12 @@ public async Task Ajax_Test() var service = Context.Services.GetRequiredService(); await service.InvokeAsync(option); + } - Context.RenderComponent(); - await service.InvokeAsync(option); + [Fact] + public async Task Goto_Test() + { + var service = Context.Services.GetRequiredService(); await service.Goto("http://www.blazor.zone"); } }