Skip to content

Commit

Permalink
chore: clean up of auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Jan 1, 2024
1 parent 7607761 commit ee0ec2c
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/Mangarr.Frontend/Pages/Activity/Content.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Mangarr.Frontend.Pages.Activity;
public partial class Content : IDisposable
{
private readonly List<ChapterProgressModel> _items = new();
private bool _isAutoRefreshing;

private bool _isRefreshing;

Expand All @@ -26,26 +27,44 @@ public void Dispose()
protected override void OnInitialized()
{
_timer = new Timer(2500);
_timer.Elapsed += (_, _) => RefreshAsync(false);
_timer.Elapsed += (_, _) => AutoRefreshAsync();
_timer.Start();

RefreshAsync(true);
RefreshAsync();
}

private async void RefreshAsync(bool updateInitialStateChange)
private async void RefreshAsync()
{
if (_isRefreshing)
{
return;
}

_isRefreshing = true;
await InvokeAsync(StateHasChanged);

await RefreshChapterProgress();

_isRefreshing = false;
await InvokeAsync(StateHasChanged);
}

if (updateInitialStateChange)
private async void AutoRefreshAsync()
{
if (_isRefreshing || _isAutoRefreshing)
{
await InvokeAsync(StateHasChanged);
return;
}

_isAutoRefreshing = true;
await RefreshChapterProgress();
_isAutoRefreshing = false;

await InvokeAsync(StateHasChanged);
}

private async Task RefreshChapterProgress()
{
Result<ChapterProgressResponse> result = await BackendApi.GetChapterProgress();

if (result.IsFailed)
Expand Down Expand Up @@ -73,8 +92,5 @@ private async void RefreshAsync(bool updateInitialStateChange)
// return lhs.ChapterNumber.CompareTo(rhs.ChapterNumber);
// });
}

_isRefreshing = false;
await InvokeAsync(StateHasChanged);
}
}

0 comments on commit ee0ec2c

Please sign in to comment.