Skip to content

Commit

Permalink
feat(BrowserFinger): redesign IBrowserFinger service (#4543)
Browse files Browse the repository at this point in the history
* refactor: 移除 BorwerFinger 组件

* refactor: 重构服务

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Oct 25, 2024
1 parent 696292e commit 02a8024
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<DrawerContainer></DrawerContainer>

<ResizeNotification></ResizeNotification>
<BrowserFinger></BrowserFinger>
<ConnectionHub></ConnectionHub>
<Mask></Mask>

Expand Down
77 changes: 0 additions & 77 deletions src/BootstrapBlazor/Components/BrowserFinger/BrowserFinger.cs

This file was deleted.

34 changes: 8 additions & 26 deletions src/BootstrapBlazor/Services/DefaultBrowserFingerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// 浏览器指纹服务
/// </summary>
class DefaultBrowserFingerService : IBrowserFingerService
class DefaultBrowserFingerService(IJSRuntime jSRuntime) : IBrowserFingerService
{
private ConcurrentDictionary<object, Func<Task<string?>>> Cache { get; } = new();

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="target"></param>
/// <param name="callback"></param>
public void Subscribe(object target, Func<Task<string?>> callback) => Cache.GetOrAdd(target, k => callback);
[NotNull]
private JSModule? _module = null;

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="target"></param>
public void Unsubscribe(object target) => Cache.TryRemove(target, out _);
private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js");

/// <summary>
/// <inheritdoc/>
/// 获取剪切板数据方法
/// </summary>
/// <returns></returns>
public async Task<string?> GetFingerCodeAsync()
public async Task<string?> 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<string?>("getFingerCode", token);
}
}
15 changes: 1 addition & 14 deletions src/BootstrapBlazor/Services/IBrowserFingerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,9 @@ namespace BootstrapBlazor.Components;
/// </summary>
public interface IBrowserFingerService
{
/// <summary>
/// 订阅指纹方法回调
/// </summary>
/// <param name="target"></param>
/// <param name="callback"></param>
void Subscribe(object target, Func<Task<string?>> callback);

/// <summary>
/// 取消指纹方法回调
/// </summary>
/// <param name="target"></param>
void Unsubscribe(object target);

/// <summary>
/// 获得当前浏览器指纹方法
/// </summary>
/// <returns></returns>
Task<string?> GetFingerCodeAsync();
Task<string?> GetFingerCodeAsync(CancellationToken token = default);
}
5 changes: 2 additions & 3 deletions test/UnitTest/Services/BrowserFingerServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ public class BrowserFingerServiceTest : BootstrapBlazorTestBase
[Fact]
public async Task GetFingerCodeAsync_Ok()
{
Context.JSInterop.Setup<string?>("getFingerCode").SetResult("9527");
var service = Context.Services.GetRequiredService<IBrowserFingerService>();
var cut = Context.RenderComponent<BrowserFinger>();
var code = await service.GetFingerCodeAsync();
cut.Instance.Dispose();
Assert.Null(code);
Assert.Equal("9527", code);
}
}

0 comments on commit 02a8024

Please sign in to comment.