@@ -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}
0 commit comments