Skip to content

Commit 554b715

Browse files
doc(FooterCounter): add FooterCounter componenet improve performance (#5150)
* doc: 增加 FooterCounter 组件提高性能 * doc: 增加图片高度样式防止抖动 Co-Authored-By: Alex chow <[email protected]> Co-Authored-By: Argo Zhang <[email protected]>
1 parent 2145ff0 commit 554b715

File tree

7 files changed

+111
-82
lines changed

7 files changed

+111
-82
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@inject ICacheManager Cache
2+
@inject IConnectionService ConnectionService
3+
4+
<div class="d-none d-sm-block ms-2">Run @Runtime</div>
5+
@if (_options.Enable)
6+
{
7+
<div class="footer-online ms-2">
8+
<i class="fa-solid fa-people-group"></i>
9+
<a href="./online" target="_blank">@ConnectionService.Count</a>
10+
</div>
11+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.Extensions.Options;
7+
8+
namespace BootstrapBlazor.Server.Components.Components;
9+
10+
/// <summary>
11+
/// FooterCounter 组件
12+
/// </summary>
13+
public partial class FooterCounter : IDisposable
14+
{
15+
private string? Runtime { get; set; }
16+
17+
private CancellationTokenSource DisposeTokenSource { get; } = new();
18+
19+
private ConnectionHubOptions _options = default!;
20+
21+
[Inject]
22+
[NotNull]
23+
private IOptions<BootstrapBlazorOptions>? BootstrapBlazorOptions { get; set; }
24+
25+
/// <summary>
26+
/// <inheritdoc/>
27+
/// </summary>
28+
protected override void OnInitialized()
29+
{
30+
base.OnInitialized();
31+
32+
_options = BootstrapBlazorOptions.Value.ConnectionHubOptions;
33+
UpdateRuntime();
34+
}
35+
36+
/// <summary>
37+
/// <inheritdoc />
38+
/// </summary>
39+
/// <param name="firstRender"></param>
40+
protected override void OnAfterRender(bool firstRender)
41+
{
42+
if (firstRender)
43+
{
44+
_ = Task.Run(async () =>
45+
{
46+
while (!DisposeTokenSource.IsCancellationRequested)
47+
{
48+
try
49+
{
50+
await Task.Delay(1000, DisposeTokenSource.Token);
51+
}
52+
catch (TaskCanceledException)
53+
{
54+
55+
}
56+
if (!DisposeTokenSource.IsCancellationRequested)
57+
{
58+
UpdateRuntime();
59+
await InvokeAsync(StateHasChanged);
60+
}
61+
}
62+
});
63+
}
64+
}
65+
66+
private void UpdateRuntime()
67+
{
68+
var ts = DateTimeOffset.Now - Cache.GetStartTime();
69+
Runtime = ts.ToString("dd\\.hh\\:mm\\:ss");
70+
}
71+
72+
private void Dispose(bool disposing)
73+
{
74+
if (disposing)
75+
{
76+
DisposeTokenSource.Cancel();
77+
DisposeTokenSource.Dispose();
78+
}
79+
}
80+
81+
/// <summary>
82+
/// <inheritdoc/>
83+
/// </summary>
84+
public void Dispose()
85+
{
86+
Dispose(true);
87+
GC.SuppressFinalize(this);
88+
}
89+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.footer-online i {
2+
color: var(--bs-success)
3+
}
4+
5+
.footer-online a {
6+
color: #ddd;
7+
}

src/BootstrapBlazor.Server/Components/Layout/HomeLayout.razor

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
@inherits LayoutComponentBase
2-
@implements IDisposable
32
@layout BaseLayout
43
@inject NavigationManager NavigationManager
54
@inject IOptionsMonitor<WebsiteOptions> WebsiteOption
65
@inject IStringLocalizer<HomeLayout> Localizer
76
@inject PackageVersionService VersionService
8-
@inject ICacheManager Cache
9-
@inject IConnectionService ConnectionService
107

118
@Body
129

@@ -65,14 +62,7 @@
6562
<div class="d-flex">
6663
<div>Powered by .NET @Version on @OS</div>
6764
<div class="ms-1">BB @VersionService.Version</div>
68-
<div class="d-none d-sm-block ms-2">Run @Runtime</div>
69-
@if (_options.Enable)
70-
{
71-
<div class="footer-online ms-2">
72-
<i class="fa-solid fa-people-group"></i>
73-
<a href="./online" target="_blank">@ConnectionService.Count</a>
74-
</div>
75-
}
65+
<FooterCounter></FooterCounter>
7666
</div>
7767
<div class="d-flex flex-fill align-items-center justify-content-center">
7868
<a class="d-none d-md-block me-3" href="@WebsiteOption.CurrentValue.GiteeRepositoryUrl" target="_blank">@Localizer["Footer"]</a>

src/BootstrapBlazor.Server/Components/Layout/HomeLayout.razor.cs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,8 @@ public partial class HomeLayout
1818
[NotNull]
1919
private string? OS { get; set; }
2020

21-
private string? Runtime { get; set; }
22-
2321
private string SelectedCulture { get; set; } = CultureInfo.CurrentUICulture.Name;
2422

25-
private CancellationTokenSource DisposeTokenSource { get; } = new();
26-
27-
[Inject]
28-
[NotNull]
29-
private IOptions<BootstrapBlazorOptions>? BootstrapBlazorOptions { get; set; }
30-
31-
private ConnectionHubOptions _options = default!;
32-
3323
/// <summary>
3424
/// <inheritdoc />
3525
/// </summary>
@@ -51,45 +41,6 @@ protected override void OnInitialized()
5141
{
5242
OS = "Unknown";
5343
}
54-
55-
_options = BootstrapBlazorOptions.Value.ConnectionHubOptions;
56-
UpdateRuntime();
57-
}
58-
59-
/// <summary>
60-
/// <inheritdoc />
61-
/// </summary>
62-
/// <param name="firstRender"></param>
63-
protected override void OnAfterRender(bool firstRender)
64-
{
65-
if (firstRender)
66-
{
67-
_ = Task.Run(async () =>
68-
{
69-
while (!DisposeTokenSource.IsCancellationRequested)
70-
{
71-
try
72-
{
73-
await Task.Delay(1000, DisposeTokenSource.Token);
74-
}
75-
catch (TaskCanceledException)
76-
{
77-
78-
}
79-
if (!DisposeTokenSource.IsCancellationRequested)
80-
{
81-
UpdateRuntime();
82-
await InvokeAsync(StateHasChanged);
83-
}
84-
}
85-
});
86-
}
87-
}
88-
89-
private void UpdateRuntime()
90-
{
91-
var ts = DateTimeOffset.Now - Cache.GetStartTime();
92-
Runtime = ts.ToString("dd\\.hh\\:mm\\:ss");
9344
}
9445

9546
private Task SetLang(string cultureName)
@@ -106,22 +57,4 @@ private Task SetLang(string cultureName)
10657

10758
return Task.CompletedTask;
10859
}
109-
110-
private void Dispose(bool disposing)
111-
{
112-
if (disposing)
113-
{
114-
DisposeTokenSource.Cancel();
115-
DisposeTokenSource.Dispose();
116-
}
117-
}
118-
119-
/// <summary>
120-
/// <inheritdoc/>
121-
/// </summary>
122-
public void Dispose()
123-
{
124-
Dispose(true);
125-
GC.SuppressFinalize(this);
126-
}
12760
}

src/BootstrapBlazor.Server/Components/Layout/HomeLayout.razor.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@
5959
margin-inline-end: 6px;
6060
}
6161

62-
.footer-info .footer-online i {
63-
color: var(--bs-success)
64-
}
65-
6662
.bb-foundation {
6763
display: flex;
6864
align-items: center;

src/BootstrapBlazor.Server/Components/Pages/Index.razor.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
.bd-masthead .bb-logo {
3232
width: 200px;
33+
height: 200px;
3334
}
3435

3536
.bd-gutter {
@@ -82,6 +83,7 @@
8283

8384
.donate .barcode {
8485
width: 280px;
86+
height: 178.84px;
8587
}
8688

8789
@media (min-width: 768px) {
@@ -96,5 +98,6 @@
9698

9799
.donate .barcode {
98100
width: 480px;
101+
height: 306.59px;
99102
}
100103
}

0 commit comments

Comments
 (0)