33
44namespace Tevling . Pages ;
55
6- public partial class Statistics : ComponentBase
6+ public partial class Statistics : ComponentBase , IAsyncDisposable
77{
88 [ Inject ] private IJSRuntime Js { get ; set ; } = null ! ;
99 [ Inject ] private IAuthenticationService AuthenticationService { get ; set ; } = null ! ;
1010 [ Inject ] private IActivityService ActivityService { get ; set ; } = null ! ;
1111
1212 private Athlete _athlete = null ! ;
1313 private Activity [ ] _activities = [ ] ;
14-
14+ private IJSObjectReference ? _module ;
1515
1616 protected override async Task OnInitializedAsync ( )
1717 {
@@ -55,6 +55,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5555 {
5656 if ( firstRender )
5757 {
58+ _module = await Js . InvokeAsync < IJSObjectReference > ( "import" , "./Pages/Statistics.razor.js" ) ;
59+
5860 List < int > lastThreeMonths =
5961 [
6062 DateTimeOffset . Now . AddMonths ( - 2 ) . Month ,
@@ -70,24 +72,40 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
7072 GetAggregatedMeasurementData ( a => ( float ) a . Details . MovingTimeInSeconds / 3600 ) ;
7173
7274
73- await Js . InvokeVoidAsync (
75+ await _module . InvokeVoidAsync (
7476 "drawChart" ,
7577 distancesLastThreeMonths ,
7678 lastThreeMonths . Select ( m => CultureInfo . CurrentCulture . DateTimeFormat . GetMonthName ( m ) ) . ToList ( ) ,
7779 "totalDistanceChart" ,
7880 "Total Distance [m]" ) ;
79- await Js . InvokeVoidAsync (
81+ await _module . InvokeVoidAsync (
8082 "drawChart" ,
8183 elevationLastThreeMonths ,
8284 lastThreeMonths . Select ( m => CultureInfo . CurrentCulture . DateTimeFormat . GetMonthName ( m ) ) . ToList ( ) ,
8385 "totalElevationChart" ,
8486 "Total Elevation [m]" ) ;
85- await Js . InvokeVoidAsync (
87+ await _module . InvokeVoidAsync (
8688 "drawChart" ,
8789 timeLastThreeMonths ,
8890 lastThreeMonths . Select ( m => CultureInfo . CurrentCulture . DateTimeFormat . GetMonthName ( m ) ) . ToList ( ) ,
8991 "totalTimeChart" ,
9092 "Total Time [h]" ) ;
9193 }
9294 }
95+
96+ public async ValueTask DisposeAsync ( )
97+ {
98+ try
99+ {
100+ if ( _module != null )
101+ {
102+ await _module . DisposeAsync ( ) ;
103+ _module = null ;
104+ }
105+ }
106+ catch ( JSDisconnectedException )
107+ {
108+ // Ignore, happens during page reload.
109+ }
110+ }
93111}
0 commit comments