-
-
Notifications
You must be signed in to change notification settings - Fork 363
perf(Message): improve performance #7094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
30ad21c
doc: 更新示例
ArgoZhang d8d926e
chore: 更新字典
ArgoZhang 5551e77
refactor: 精简代码
ArgoZhang e270070
refactor: 移除 Clear 方法参数传递
ArgoZhang 178fac8
chore: 增加字典
ArgoZhang d87ea23
feat: 支持全局配置自动隐藏时长设置
ArgoZhang 1068ab6
doc: 更新注释
ArgoZhang 6ace02d
feat: 精简反转逻辑
ArgoZhang 13c83f6
refactor: 更新 Clear 方法
ArgoZhang 52b901b
chore: 增加命名空间
ArgoZhang 52e7f31
refactor: 更新 init 接口参数
ArgoZhang acb9295
refactor: 重构 Dismiss 逻辑提高性能
ArgoZhang 0c6cb44
fix: 修复异步消息未清除 dom 问题
ArgoZhang 964663d
refactor: 更新关闭逻辑
ArgoZhang aeb989e
refactor: 移除 click 事件委托
ArgoZhang a25b6dc
refactor: 精简代码
ArgoZhang 898d8f1
test: 更新单元测试
ArgoZhang 8143f1f
refactor: 精简代码
ArgoZhang d894f8f
perf: 提高性能
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,3 +121,5 @@ dotx | |
| Modbus | ||
| Protocol | ||
| vditor | ||
| alertdialog | ||
| blazorbootstrap | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // 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([email protected]) Website: https://www.blazor.zone | ||
|
|
@@ -18,13 +18,20 @@ public sealed partial class Messages | |
|
|
||
| private readonly MessageOption _option = new(); | ||
|
|
||
| private long _count = 0; | ||
|
|
||
| private string _placement = "Top"; | ||
|
|
||
| private readonly List<SelectedItem> _items = [new SelectedItem("Top", "Top"), new SelectedItem("Bottom", "Bottom")]; | ||
|
|
||
| private Placement Placement => _placement == "Top" ? Placement.Top : Placement.Bottom; | ||
|
|
||
| private async Task ShowMessage() | ||
| { | ||
| Message.SetPlacement(Placement.Top); | ||
| await MessageService.Show(new MessageOption() | ||
| { | ||
| Content = "This is a reminder message" | ||
| }); | ||
| Content = $"This is a reminder message {_count++}" | ||
| }, Message); | ||
| } | ||
|
|
||
| private async Task ShowAsyncMessage() | ||
|
|
@@ -97,7 +104,7 @@ private async Task ShowBottomMessage() | |
| { | ||
| await MessageService.Show(new MessageOption() | ||
| { | ||
| Content = $"This is a reminder message - {DateTime.Now:mm:ss}", | ||
| Content = $"This is a reminder message - {_count++}", | ||
| Icon = "fa-solid fa-circle-info", | ||
| }, Message1); | ||
| } | ||
|
|
@@ -111,13 +118,11 @@ await MessageService.Show(new MessageOption() | |
| }); | ||
| } | ||
|
|
||
| private int lastCount = 0; | ||
|
|
||
| private Task ShowLastOnlyMessage() => MessageService.Show(new MessageOption() | ||
| { | ||
| ShowShadow = true, | ||
| ShowMode = MessageShowMode.Single, | ||
| Content = lastCount++.ToString() | ||
| Content = $"This is a reminder message - {_count++}" | ||
| }); | ||
|
|
||
| private static AttributeItem[] GetAttributes() => | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,29 @@ | ||
| @namespace BootstrapBlazor.Components | ||
| @namespace BootstrapBlazor.Components | ||
| @inherits BootstrapModuleComponentBase | ||
| @attribute [BootstrapModuleAutoLoader(JSObjectReference = true)] | ||
|
|
||
| <div id="@Id" class="@ClassString" style="@StyleName" role="alert"> | ||
| @if (Placement == Placement.Top) | ||
| @foreach (var item in GetMessages()) | ||
| { | ||
| foreach (var item in _messages) | ||
| { | ||
| <div @key="item" id="@GetItemId(item)" role="alertdialog" class="@GetItemClassString(item)" data-bb-autohide="@GetAutoHideString(item)" data-bb-delay="@item.Delay"> | ||
| @if (!string.IsNullOrEmpty(item.Icon)) | ||
| <div @key="item" id="@GetItemId(item)" role="alertdialog" class="@GetItemClassString(item)" data-bb-autohide="@GetAutoHideString(item)" data-bb-delay="@item.Delay"> | ||
| @if (!string.IsNullOrEmpty(item.Icon)) | ||
| { | ||
| <i class="@item.Icon"></i> | ||
| } | ||
| <div> | ||
| @if (item.ChildContent != null) | ||
| { | ||
| <i class="@item.Icon"></i> | ||
| @item.ChildContent | ||
| } | ||
| <div> | ||
| @if (item.ChildContent != null) | ||
| { | ||
| @item.ChildContent | ||
| } | ||
| else | ||
| { | ||
| @item.Content | ||
| } | ||
| </div> | ||
| @if (item.ShowDismiss) | ||
| else | ||
| { | ||
| <button type="button" class="btn-close" aria-label="close"></button> | ||
| @item.Content | ||
| } | ||
| </div> | ||
| } | ||
| } | ||
| else | ||
| { | ||
| for (var index = _messages.Count; index > 0; index--) | ||
| { | ||
| var item = _messages[index - 1]; | ||
| <div @key="item" id="@GetItemId(item)" role="alertdialog" class="@GetItemClassString(item)" data-bb-autohide="@GetAutoHideString(item)" data-bb-delay="@item.Delay"> | ||
| @if (!string.IsNullOrEmpty(item.Icon)) | ||
| { | ||
| <i class="@item.Icon"></i> | ||
| } | ||
| <div> | ||
| @if (item.ChildContent != null) | ||
| { | ||
| @item.ChildContent | ||
| } | ||
| else | ||
| { | ||
| @item.Content | ||
| } | ||
| </div> | ||
| @if (item.ShowDismiss) | ||
| { | ||
| <button type="button" class="btn-close" aria-label="close"></button> | ||
| } | ||
| </div> | ||
| } | ||
| @if (item.ShowDismiss) | ||
| { | ||
| <button type="button" class="btn-close" aria-label="close"></button> | ||
| } | ||
| </div> | ||
| } | ||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // 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([email protected]) Website: https://www.blazor.zone | ||
|
|
@@ -55,7 +55,7 @@ protected override void OnInitialized() | |
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, nameof(Clear)); | ||
| protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop); | ||
|
|
||
| private static string? GetAutoHideString(MessageOption option) => option.IsAutoHide ? "true" : null; | ||
|
|
||
|
|
@@ -85,8 +85,19 @@ protected override async Task OnAfterRenderAsync(bool firstRender) | |
| } | ||
| } | ||
|
|
||
| private List<MessageOption> GetMessages() | ||
ArgoZhang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| var messages = new List<MessageOption>(_messages); | ||
| if (Placement == Placement.Bottom) | ||
| { | ||
| messages.Reverse(); | ||
| } | ||
|
|
||
| return messages; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 设置 Toast 容器位置方法 | ||
| /// 设置 容器位置方法 | ||
| /// </summary> | ||
| /// <param name="placement"></param> | ||
| public void SetPlacement(Placement placement) | ||
|
|
@@ -105,33 +116,39 @@ private async Task Show(MessageOption option) | |
| if (!_messages.Contains(option)) | ||
| { | ||
| _messages.Add(option); | ||
| _msgId = GetItemId(option); | ||
| } | ||
| _msgId = GetItemId(option); | ||
| await InvokeAsync(StateHasChanged); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 清除 Message 方法 由 JSInvoke 触发 | ||
| /// </summary> | ||
| [JSInvokable] | ||
| public Task Clear() | ||
| public void Clear(string id) | ||
| { | ||
| _messages.Clear(); | ||
| var option = _messages.Find(i => GetItemId(i) == id); | ||
| if (option != null) | ||
| { | ||
| _messages.Remove(option); | ||
| } | ||
|
|
||
| StateHasChanged(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// OnDismiss 回调方法 由 JSInvoke 触发 | ||
| /// </summary> | ||
| /// <param name="id"></param> | ||
| [JSInvokable] | ||
| public async Task Dismiss(string id) | ||
| public async ValueTask Dismiss(string id) | ||
| { | ||
| var option = _messages.Find(i => GetItemId(i) == id); | ||
| if (option is { OnDismiss: not null }) | ||
| { | ||
| await option.OnDismiss(); | ||
| _messages.Remove(option); | ||
| StateHasChanged(); | ||
| } | ||
| } | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // 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([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using Microsoft.AspNetCore.Components.Web; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace UnitTest.Components; | ||
|
|
||
|
|
@@ -57,10 +58,8 @@ await cut.InvokeAsync(() => | |
| Assert.NotNull(alert.Id); | ||
|
|
||
| var message = cut.FindComponent<Message>(); | ||
| await message.Instance.Dismiss(alert.Id); | ||
| await cut.InvokeAsync(() => message.Instance.Dismiss(alert.Id)); | ||
| Assert.True(dismiss); | ||
|
|
||
| await cut.InvokeAsync(() => message.Instance.Clear()); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
@@ -98,7 +97,7 @@ await cut.InvokeAsync(() => service.Show(new MessageOption() | |
|
|
||
| await cut.Instance.Dismiss(alert.Id); | ||
| await cut.Instance.Dismiss("test_id"); | ||
| await cut.InvokeAsync(() => cut.Instance.Clear()); | ||
| await cut.InvokeAsync(() => cut.Instance.Clear(alert.Id)); | ||
|
|
||
| await cut.InvokeAsync(() => service.Show(new MessageOption() | ||
| { | ||
|
|
@@ -133,4 +132,44 @@ await cut.InvokeAsync(() => service.Show(new MessageOption() | |
| ShowMode = MessageShowMode.Single | ||
| }, cut.Instance)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ForceDelay_Ok() | ||
| { | ||
| var service = Context.Services.GetRequiredService<MessageService>(); | ||
| var cut = Context.RenderComponent<Message>(); | ||
| var option = new MessageOption() | ||
| { | ||
| Content = "Test Content", | ||
| IsAutoHide = false, | ||
| ShowDismiss = true, | ||
| Icon = "fa-solid fa-font-awesome", | ||
| ForceDelay = true, | ||
| Delay = 2000 | ||
| }; | ||
| await cut.InvokeAsync(() => service.Show(option, cut.Instance)); | ||
| Assert.Contains("data-bb-delay=\"2000\"", cut.Markup); | ||
|
|
||
| var alert = cut.Find(".alert"); | ||
| Assert.NotNull(alert); | ||
| Assert.NotNull(alert.Id); | ||
| await cut.InvokeAsync(() => cut.Instance.Clear(alert.Id)); | ||
|
|
||
| option.ForceDelay = false; | ||
| await cut.InvokeAsync(() => service.Show(option, cut.Instance)); | ||
| Assert.Contains("data-bb-delay=\"4000\"", cut.Markup); | ||
| await cut.InvokeAsync(() => cut.Instance.Clear(alert.Id)); | ||
|
|
||
| // 更新 Options 值 | ||
| var options = Context.Services.GetRequiredService<IOptionsMonitor<BootstrapBlazorOptions>>(); | ||
| options.CurrentValue.MessageDelay = 1000; | ||
| await cut.InvokeAsync(() => service.Show(option, cut.Instance)); | ||
| Assert.Contains("data-bb-delay=\"1000\"", cut.Markup); | ||
| await cut.InvokeAsync(() => cut.Instance.Clear(alert.Id)); | ||
|
|
||
| options.CurrentValue.MessageDelay = 0; | ||
| await cut.InvokeAsync(() => service.Show(option, cut.Instance)); | ||
| Assert.Contains("data-bb-delay=\"1000\"", cut.Markup); | ||
| await cut.InvokeAsync(() => cut.Instance.Clear(alert.Id)); | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.