Skip to content

Commit 948a9b9

Browse files
authored
doc(FooterCounter): improve performance for ssr mode (#6607)
* style: 调整计时器宽度 * perf: 更改为服务器端与客户端结合减少服务器压力 * refactor: 更改样式 * style: 更新圆角 * style: 调整滚动条位置 * refactor: 精简进度面板代码
1 parent 6086ac3 commit 948a9b9

File tree

7 files changed

+96
-39
lines changed

7 files changed

+96
-39
lines changed

src/BootstrapBlazor.Server/Components/Components/FooterCounter.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
@inject ICacheManager Cache
22
@inject IConnectionService ConnectionService
3+
@inherits WebSiteModuleComponentBase
4+
@attribute [JSModuleAutoLoader("Components/FooterCounter.razor.js")]
35

4-
<div class="d-none d-sm-block ms-2">Run @Runtime</div>
6+
<div id="@Id" class="d-none d-sm-block ms-2" style="width: 110px;">Run @Runtime</div>
57
@if (_options.Enable)
68
{
79
<div class="footer-online ms-2">

src/BootstrapBlazor.Server/Components/Components/FooterCounter.razor.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace BootstrapBlazor.Server.Components.Components;
1010
/// <summary>
1111
/// FooterCounter 组件
1212
/// </summary>
13-
public partial class FooterCounter : IDisposable
13+
public partial class FooterCounter
1414
{
1515
private string? Runtime { get; set; }
1616

17-
private CancellationTokenSource _disposeTokenSource = new();
17+
private readonly CancellationTokenSource _disposeTokenSource = new();
1818

1919
private ConnectionHubOptions _options = default!;
2020

@@ -30,7 +30,9 @@ protected override void OnInitialized()
3030
base.OnInitialized();
3131

3232
_options = BootstrapBlazorOptions.Value.ConnectionHubOptions;
33-
UpdateRuntime();
33+
34+
var ts = DateTimeOffset.Now - Cache.GetStartTime();
35+
Runtime = ts.ToString("dd\\.hh\\:mm\\:ss");
3436
}
3537

3638
/// <summary>
@@ -41,36 +43,44 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4143
{
4244
await base.OnAfterRenderAsync(firstRender);
4345

44-
try
46+
if (firstRender)
4547
{
46-
await Task.Delay(1000, _disposeTokenSource.Token);
47-
UpdateRuntime();
48-
StateHasChanged();
48+
try
49+
{
50+
while (_disposeTokenSource is { IsCancellationRequested: false })
51+
{
52+
await Task.Delay(30000, _disposeTokenSource.Token);
53+
var ts = DateTimeOffset.Now - Cache.GetStartTime();
54+
await InvokeVoidAsync("updateFooterCounter", Id, ts.TotalSeconds);
55+
}
56+
}
57+
catch { }
4958
}
50-
catch { }
5159
}
5260

53-
private void UpdateRuntime()
61+
/// <summary>
62+
/// <inheritdoc/>
63+
/// </summary>
64+
/// <returns></returns>
65+
protected override Task InvokeInitAsync()
5466
{
5567
var ts = DateTimeOffset.Now - Cache.GetStartTime();
56-
Runtime = ts.ToString("dd\\.hh\\:mm\\:ss");
68+
return InvokeVoidAsync("init", Id, ts.TotalSeconds);
5769
}
5870

59-
private void Dispose(bool disposing)
71+
/// <summary>
72+
/// <inheritdoc/>
73+
/// </summary>
74+
/// <param name="disposing"></param>
75+
/// <returns></returns>
76+
protected override async ValueTask DisposeAsync(bool disposing)
6077
{
78+
await base.DisposeAsync(true);
79+
6180
if (disposing)
6281
{
6382
_disposeTokenSource.Cancel();
6483
_disposeTokenSource.Dispose();
6584
}
6685
}
67-
68-
/// <summary>
69-
/// <inheritdoc/>
70-
/// </summary>
71-
public void Dispose()
72-
{
73-
Dispose(true);
74-
GC.SuppressFinalize(this);
75-
}
7686
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Data from "../../_content/BootstrapBlazor/modules/data.js"
2+
3+
export function init(id, totalSeconds) {
4+
const el = document.getElementById(id);
5+
if (el === null) {
6+
return;
7+
}
8+
9+
const tick = () => {
10+
const counter = Data.get(id);
11+
12+
counter.totalSeconds = (counter.totalSeconds || 0) + 1;
13+
const now = Math.round(counter.totalSeconds, 0);
14+
15+
const days = Math.floor(now / (24 * 3600));
16+
const hours = Math.floor((now % (24 * 3600)) / 3600);
17+
const minutes = Math.floor((now % 3600) / 60);
18+
const seconds = now % 60;
19+
20+
const pad = num => num.toString().padStart(2, '0');
21+
el.innerHTML = `Run ${pad(days)}.${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
22+
}
23+
24+
const handler = setInterval(tick, 1000);
25+
Data.set(id, {
26+
el,
27+
handler,
28+
totalSeconds
29+
});
30+
}
31+
32+
export function updateFooterCounter(id, totalSeconds) {
33+
const counter = Data.get(id);
34+
if (counter) {
35+
counter.totalSeconds = totalSeconds;
36+
console.log(`FooterCounter updated: ${id}, totalSeconds: ${totalSeconds}`);
37+
}
38+
}
39+
40+
export function dispose(id) {
41+
const counter = Data.get(id);
42+
if (counter) {
43+
clearInterval(counter.handler);
44+
Data.remove(id);
45+
}
46+
}

src/BootstrapBlazor.Server/Components/Components/Video.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
<p><b>@Localizer["H1"]</b></p>
55

6-
@if (VideoUrl.Any())
6+
@if (VideoUrl.Count > 0)
77
{
88
foreach (var url in VideoUrl)
99
{
1010
<div class="mb-3">
11-
<a class="fa-solid fa-video" href="@url" target="_blank"><span class="ms-2">@Localizer["L1"]</span></a>
11+
<a href="@url" target="_blank"><i class="fa-solid fa-video"></i><span class="ms-2">@Localizer["L1"]</span></a>
1212
</div>
1313
}
1414
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
position: absolute;
4545
left: var(--bb-btn-space);
4646
top: var(--bb-btn-space);
47+
border-radius: inherit;
4748
}
4849

4950
.bb-logo:before {

src/BootstrapBlazor.Server/Components/Samples/Tutorials/Dashboard.razor

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,16 @@
129129
<div class="col-md-6">
130130
<Card IsShadow="true" HeaderText="分类进度">
131131
<BodyTemplate>
132-
<Scroll Height="320px" style="overflow:scroll;">
133-
<div style="margin-right:10px;">
134-
@{
135-
foreach (var item in Data.TestKKSGroupList)
136-
{
137-
<h4 class="small fw-bold">
138-
@item.KKS @item.NAM<span class="float-end">
139-
@(
140-
item.Percent
141-
)%
142-
</span>
143-
</h4>
144-
<BootstrapBlazor.Components.Progress Value="@item.Percent" IsAnimated="true" Color="@GetColor(item.Percent)" IsShowValue="true" Round="1"></BootstrapBlazor.Components.Progress>
145-
}
146-
}
147-
</div>
132+
<Scroll Height="320px" style="margin: -1rem; padding: 1rem;">
133+
@foreach (var item in Data.TestKKSGroupList)
134+
{
135+
<div class="progress-item">
136+
<div class="small fw-bold mb-2">
137+
@item.KKS @item.NAM<span class="float-end">@item.Percent%</span>
138+
</div>
139+
<BootstrapBlazor.Components.Progress Value="@item.Percent" IsAnimated="true" Color="@GetColor(item.Percent)" IsShowValue="true" Round="1"></BootstrapBlazor.Components.Progress>
140+
</div>
141+
}
148142
</Scroll>
149143
</BodyTemplate>
150144
</Card>

src/BootstrapBlazor.Server/Components/Samples/Tutorials/Dashboard.razor.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
.db-progress ::deep .progress {
2121
flex: 1;
2222
}
23+
24+
.progress-item:not(:last-child) {
25+
margin-block-end: 1rem;
26+
}

0 commit comments

Comments
 (0)