Skip to content

refactor: improve auto fetch timer implementation using PeriodicTimer #1139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/ViewModels/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,13 @@ public void Open()
_selectedView = _histories;
_selectedViewIndex = 0;

_autoFetchTimer = new Timer(AutoFetchImpl, null, 5000, 5000);
_ = StartAutoFetchAsync();
RefreshAll();
}

public void Close()
{
_isClosing = true;
SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect

var settingsSerialized = JsonSerializer.Serialize(_settings, JsonCodeGen.Default.RepositorySettings);
Expand All @@ -473,7 +474,7 @@ public void Close()
{
// Ignore
}
_autoFetchTimer.Dispose();
_autoFetchTimer?.Dispose();
_autoFetchTimer = null;

_settings = null;
Expand Down Expand Up @@ -2511,7 +2512,34 @@ private void CalcMatchedFilesForSearching()
MatchedFilesForSearching = matched;
}

private void AutoFetchImpl(object sender)
private async Task StartAutoFetchAsync()
{
_autoFetchTimer = new PeriodicTimer(TimeSpan.FromSeconds(5));

try
{
while (!_isClosing && await _autoFetchTimer.WaitForNextTickAsync())
{
if (_isClosing)
break;

try
{
await Task.Run(AutoFetchImpl);
}
catch (Exception)
{
// ignored
}
}
}
catch (OperationCanceledException)
{
// ignored
}
}

private void AutoFetchImpl()
{
if (!_settings.EnableAutoFetch || _isAutoFetching)
return;
Expand Down Expand Up @@ -2579,7 +2607,8 @@ private void AutoFetchImpl(object sender)
private List<Models.Submodule> _visibleSubmodules = new List<Models.Submodule>();

private bool _isAutoFetching = false;
private Timer _autoFetchTimer = null;
private PeriodicTimer _autoFetchTimer;
private volatile bool _isClosing = false;
private DateTime _lastFetchTime = DateTime.MinValue;
}
}