Refactor reload logic with semaphore for concurrency
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release Linux / rpm (push) Blocked by required conditions
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run

This commit is contained in:
2dust 2025-11-08 20:48:55 +08:00
parent a6af95e083
commit e20c11c1a7

View file

@ -64,8 +64,6 @@ public class MainWindowViewModel : MyReactiveObject
#endregion Menu #endregion Menu
private bool _hasNextReloadJob = false;
#region Init #region Init
public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView) public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
@ -268,7 +266,6 @@ public class MainWindowViewModel : MyReactiveObject
} }
await RefreshServers(); await RefreshServers();
BlReloadEnabled = true;
await Reload(); await Reload();
} }
@ -525,15 +522,20 @@ public class MainWindowViewModel : MyReactiveObject
#region core job #region core job
private bool _hasNextReloadJob = false;
private readonly SemaphoreSlim _reloadSemaphore = new(1, 1);
public async Task Reload() public async Task Reload()
{ {
//If there are unfinished reload job, marked with next job. //If there are unfinished reload job, marked with next job.
if (!BlReloadEnabled) if (!await _reloadSemaphore.WaitAsync(0))
{ {
_hasNextReloadJob = true; _hasNextReloadJob = true;
return; return;
} }
try
{
SetReloadEnabled(false); SetReloadEnabled(false);
var msgs = await ActionPrecheckManager.Instance.Check(_config.IndexId); var msgs = await ActionPrecheckManager.Instance.Check(_config.IndexId);
@ -544,7 +546,6 @@ public class MainWindowViewModel : MyReactiveObject
NoticeManager.Instance.SendMessage(msg); NoticeManager.Instance.SendMessage(msg);
} }
NoticeManager.Instance.Enqueue(Utils.List2String(msgs.Take(10).ToList(), true)); NoticeManager.Instance.Enqueue(Utils.List2String(msgs.Take(10).ToList(), true));
SetReloadEnabled(true);
return; return;
} }
@ -563,13 +564,19 @@ public class MainWindowViewModel : MyReactiveObject
} }
ReloadResult(showClashUI); ReloadResult(showClashUI);
}
finally
{
SetReloadEnabled(true); SetReloadEnabled(true);
_reloadSemaphore.Release();
//If there is a next reload job, execute it.
if (_hasNextReloadJob) if (_hasNextReloadJob)
{ {
_hasNextReloadJob = false; _hasNextReloadJob = false;
await Reload(); await Reload();
} }
} }
}
private void ReloadResult(bool showClashUI) private void ReloadResult(bool showClashUI)
{ {