From 02a8024fcdd0deedaf4a8376d9ade959b7b64c46 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 25 Oct 2024 12:12:17 +0800 Subject: [PATCH] feat(BrowserFinger): redesign IBrowserFinger service (#4543) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 移除 BorwerFinger 组件 * refactor: 重构服务 * test: 更新单元测试 --- .../BaseComponents/BootstrapBlazorRoot.razor | 1 - .../Components/BrowserFinger/BrowserFinger.cs | 77 ------------------- .../Services/DefaultBrowserFingerService.cs | 34 ++------ .../Services/IBrowserFingerService.cs | 15 +--- .../Services/BrowserFingerServiceTest.cs | 5 +- 5 files changed, 11 insertions(+), 121 deletions(-) delete mode 100644 src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.cs diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index 2d8c04bd8af..7037643218e 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -11,7 +11,6 @@ - diff --git a/src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.cs b/src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.cs deleted file mode 100644 index 292b7e9d9cb..00000000000 --- a/src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.cs +++ /dev/null @@ -1,77 +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 - -namespace BootstrapBlazor.Components; - -/// -/// 浏览器指纹组件 -/// -[BootstrapModuleAutoLoader(ModuleName = "utility", AutoInvokeInit = false, AutoInvokeDispose = false)] -public partial class BrowserFinger : BootstrapModuleComponentBase, IDisposable -{ - [Inject] - [NotNull] - private IBrowserFingerService? BrowserFingerService { get; set; } - - private string? _fingerCode; - - /// - /// - /// - protected override void OnInitialized() - { - base.OnInitialized(); - - BrowserFingerService.Subscribe(this, Callback); - } - - private readonly TaskCompletionSource _tcs = new(); - - /// - /// - /// - /// - /// - protected override async Task OnAfterRenderAsync(bool firstRender) - { - await base.OnAfterRenderAsync(firstRender); - - if (firstRender) - { - _tcs.TrySetResult(); - } - } - - private async Task Callback() - { - if (string.IsNullOrEmpty(_fingerCode)) - { - await _tcs.Task; - _fingerCode = await InvokeAsync("getFingerCode"); - } - return _fingerCode; - } - - /// - /// Dispose 方法 - /// - /// - protected virtual void Dispose(bool disposing) - { - if (disposing) - { - BrowserFingerService.Unsubscribe(this); - } - } - - /// - /// - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } -} diff --git a/src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs b/src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs index 4649328f7aa..45a18d7229e 100644 --- a/src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs +++ b/src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs @@ -3,42 +3,24 @@ // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone -using System.Collections.Concurrent; - namespace BootstrapBlazor.Components; /// /// 浏览器指纹服务 /// -class DefaultBrowserFingerService : IBrowserFingerService +class DefaultBrowserFingerService(IJSRuntime jSRuntime) : IBrowserFingerService { - private ConcurrentDictionary>> Cache { get; } = new(); - - /// - /// - /// - /// - /// - public void Subscribe(object target, Func> callback) => Cache.GetOrAdd(target, k => callback); + [NotNull] + private JSModule? _module = null; - /// - /// - /// - /// - public void Unsubscribe(object target) => Cache.TryRemove(target, out _); + private Task LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js"); /// - /// + /// 获取剪切板数据方法 /// - /// - public async Task GetFingerCodeAsync() + public async Task GetFingerCodeAsync(CancellationToken token = default) { - string? code = null; - var cb = Cache.LastOrDefault(); - if (cb.Value != null) - { - code = await cb.Value(); - } - return code; + _module ??= await LoadModule(); + return await _module.InvokeAsync("getFingerCode", token); } } diff --git a/src/BootstrapBlazor/Services/IBrowserFingerService.cs b/src/BootstrapBlazor/Services/IBrowserFingerService.cs index 1b36b6ce624..6c461220b08 100644 --- a/src/BootstrapBlazor/Services/IBrowserFingerService.cs +++ b/src/BootstrapBlazor/Services/IBrowserFingerService.cs @@ -10,22 +10,9 @@ namespace BootstrapBlazor.Components; /// public interface IBrowserFingerService { - /// - /// 订阅指纹方法回调 - /// - /// - /// - void Subscribe(object target, Func> callback); - - /// - /// 取消指纹方法回调 - /// - /// - void Unsubscribe(object target); - /// /// 获得当前浏览器指纹方法 /// /// - Task GetFingerCodeAsync(); + Task GetFingerCodeAsync(CancellationToken token = default); } diff --git a/test/UnitTest/Services/BrowserFingerServiceTest.cs b/test/UnitTest/Services/BrowserFingerServiceTest.cs index 40cec74720e..7d28958c8ac 100644 --- a/test/UnitTest/Services/BrowserFingerServiceTest.cs +++ b/test/UnitTest/Services/BrowserFingerServiceTest.cs @@ -10,10 +10,9 @@ public class BrowserFingerServiceTest : BootstrapBlazorTestBase [Fact] public async Task GetFingerCodeAsync_Ok() { + Context.JSInterop.Setup("getFingerCode").SetResult("9527"); var service = Context.Services.GetRequiredService(); - var cut = Context.RenderComponent(); var code = await service.GetFingerCodeAsync(); - cut.Instance.Dispose(); - Assert.Null(code); + Assert.Equal("9527", code); } }