Skip to content

Commit d808607

Browse files
committed
perf: 更改为服务器端与客户端结合减少服务器压力
1 parent 5f0c397 commit d808607

File tree

3 files changed

+79
-21
lines changed

3 files changed

+79
-21
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" style="width: 110px;">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+
}

0 commit comments

Comments
 (0)