mirror of
https://github.com/ppy/osu.git
synced 2026-05-13 20:33:35 +08:00
Allow cancelling from initial notification
This commit is contained in:
@@ -37,7 +37,7 @@ namespace osu.Desktop.Updater
|
||||
scheduledBackgroundCheck = Scheduler.AddDelayed(() =>
|
||||
{
|
||||
Logger.Log("Running scheduled background update check...");
|
||||
Task.Run(CheckForUpdateAsync);
|
||||
CheckForUpdate();
|
||||
}, 60000 * 30);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,13 @@ namespace osu.Desktop.Updater
|
||||
|
||||
UpdateInfo? update = await updateManager.CheckForUpdatesAsync().ConfigureAwait(false);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
Logger.Log("Update check cancelled");
|
||||
scheduleNextUpdateCheck();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (update == null)
|
||||
{
|
||||
// No update is available.
|
||||
@@ -68,9 +75,8 @@ namespace osu.Desktop.Updater
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger.Log($"New update available: {update.TargetFullRelease.Version}");
|
||||
|
||||
// Download update in the background while notifying awaiters of the update being available.
|
||||
Logger.Log($"New update available: {update.TargetFullRelease.Version}");
|
||||
downloadUpdate(updateManager, update, cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
|
||||
|
||||
try
|
||||
{
|
||||
bool foundUpdate = await updateManager.CheckForUpdateAsync().ConfigureAwait(true);
|
||||
bool foundUpdate = await updateManager.CheckForUpdateAsync(checkingNotification.CancellationToken).ConfigureAwait(true);
|
||||
|
||||
if (!foundUpdate)
|
||||
{
|
||||
@@ -142,8 +142,9 @@ namespace osu.Game.Overlays.Settings.Sections.General
|
||||
}
|
||||
finally
|
||||
{
|
||||
// This sequence allows the notification to be immediately dismissed.
|
||||
checkingNotification.State = ProgressNotificationState.Cancelled;
|
||||
// This sequence allows the notification to be immediately dismissed without posting a continuation message.
|
||||
checkingNotification.CompletionTarget = null;
|
||||
checkingNotification.State = ProgressNotificationState.Completed;
|
||||
checkingNotification.Close(false);
|
||||
checkForUpdatesButton.Enabled.Value = true;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace osu.Game.Updater
|
||||
protected IBindable<ReleaseStream> ReleaseStream => releaseStream;
|
||||
|
||||
private readonly Bindable<ReleaseStream> releaseStream = new Bindable<ReleaseStream>();
|
||||
private CancellationTokenSource updateCancellation = new CancellationTokenSource();
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
@@ -67,22 +68,35 @@ namespace osu.Game.Updater
|
||||
config.SetValue(OsuSetting.Version, version);
|
||||
|
||||
config.BindWith(OsuSetting.ReleaseStream, releaseStream);
|
||||
releaseStream.BindValueChanged(_ => CheckForUpdateAsync());
|
||||
releaseStream.BindValueChanged(_ => CheckForUpdate());
|
||||
|
||||
CheckForUpdateAsync();
|
||||
CheckForUpdate();
|
||||
}
|
||||
|
||||
private CancellationTokenSource? updateCheckCancellation;
|
||||
/// <summary>
|
||||
/// Immediately checks for any available update.
|
||||
/// </summary>
|
||||
public void CheckForUpdate()
|
||||
{
|
||||
_ = CheckForUpdateAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Immediately checks for any available updates, or returns the existing update task.
|
||||
/// Immediately checks for any available update.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if any updates are available, <c>false</c> otherwise.</returns>
|
||||
public Task<bool> CheckForUpdateAsync()
|
||||
public async Task<bool> CheckForUpdateAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
updateCheckCancellation?.Cancel();
|
||||
updateCheckCancellation = new CancellationTokenSource();
|
||||
return PerformUpdateCheck(updateCheckCancellation.Token);
|
||||
var cancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
var lastCancellation = Interlocked.Exchange(ref updateCancellation, cancellation);
|
||||
|
||||
using (lastCancellation)
|
||||
{
|
||||
// This serves a dual purpose of nullifying the last update, closing any existing notifications as stale.
|
||||
await lastCancellation.CancelAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return await PerformUpdateCheck(cancellation.Token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,12 +105,6 @@ namespace osu.Game.Updater
|
||||
/// <returns>Whether any update is waiting. May return true if an error occured (there is potentially an update available).</returns>
|
||||
protected virtual Task<bool> PerformUpdateCheck(CancellationToken cancellationToken) => Task.FromResult(false);
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
base.Dispose(isDisposing);
|
||||
updateCheckCancellation?.Cancel();
|
||||
}
|
||||
|
||||
private partial class UpdateCompleteNotification : SimpleNotification
|
||||
{
|
||||
private readonly string version;
|
||||
|
||||
Reference in New Issue
Block a user