2020-12-29 03:59:12 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2020-12-29 03:59:12 +08:00
|
|
|
using System;
|
|
|
|
using osu.Framework.Bindables;
|
2021-01-10 04:38:20 +08:00
|
|
|
using osu.Framework.Graphics;
|
2020-12-29 03:59:12 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.OnlinePlay
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Utility class to track ongoing online operations' progress.
|
|
|
|
/// Can be used to disable interactivity while waiting for a response from online sources.
|
|
|
|
/// </summary>
|
2021-01-10 04:38:20 +08:00
|
|
|
public class OngoingOperationTracker : Component
|
2020-12-29 03:59:12 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Whether there is an online operation in progress.
|
|
|
|
/// </summary>
|
|
|
|
public IBindable<bool> InProgress => inProgress;
|
|
|
|
|
|
|
|
private readonly Bindable<bool> inProgress = new BindableBool();
|
|
|
|
|
|
|
|
private LeasedBindable<bool> leasedInProgress;
|
|
|
|
|
2021-01-10 04:38:20 +08:00
|
|
|
public OngoingOperationTracker()
|
|
|
|
{
|
|
|
|
AlwaysPresent = true;
|
|
|
|
}
|
|
|
|
|
2020-12-29 03:59:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Begins tracking a new online operation.
|
|
|
|
/// </summary>
|
2020-12-29 14:54:27 +08:00
|
|
|
/// <returns>
|
|
|
|
/// An <see cref="IDisposable"/> that will automatically mark the operation as ended on disposal.
|
|
|
|
/// </returns>
|
2020-12-29 03:59:12 +08:00
|
|
|
/// <exception cref="InvalidOperationException">An operation has already been started.</exception>
|
2020-12-29 14:54:27 +08:00
|
|
|
public IDisposable BeginOperation()
|
2020-12-29 03:59:12 +08:00
|
|
|
{
|
|
|
|
if (leasedInProgress != null)
|
|
|
|
throw new InvalidOperationException("Cannot begin operation while another is in progress.");
|
|
|
|
|
|
|
|
leasedInProgress = inProgress.BeginLease(true);
|
|
|
|
leasedInProgress.Value = true;
|
2020-12-29 14:54:27 +08:00
|
|
|
|
2021-01-25 15:53:58 +08:00
|
|
|
return new OngoingOperation(this, leasedInProgress);
|
2020-12-29 03:59:12 +08:00
|
|
|
}
|
|
|
|
|
2021-01-25 15:53:58 +08:00
|
|
|
private void endOperationWithKnownLease(LeasedBindable<bool> lease)
|
2020-12-29 03:59:12 +08:00
|
|
|
{
|
2021-01-25 15:53:58 +08:00
|
|
|
// for extra safety, marshal the end of operation back to the update thread if necessary.
|
|
|
|
Scheduler.Add(() =>
|
|
|
|
{
|
2021-01-31 04:03:09 +08:00
|
|
|
if (lease != leasedInProgress)
|
|
|
|
return;
|
|
|
|
|
2021-01-31 04:00:13 +08:00
|
|
|
// UnbindAll() is purposefully used instead of Return() - the two do roughly the same thing, with one difference:
|
|
|
|
// the former won't throw if the lease has already been returned before.
|
|
|
|
// this matters because framework can unbind the lease via the internal UnbindAllBindables(), which is not always detectable
|
|
|
|
// (it is in the case of disposal, but not in the case of screen exit - at least not cleanly).
|
|
|
|
leasedInProgress?.UnbindAll();
|
2021-01-25 15:53:58 +08:00
|
|
|
leasedInProgress = null;
|
|
|
|
}, false);
|
2020-12-29 03:59:12 +08:00
|
|
|
}
|
2021-01-23 23:04:12 +08:00
|
|
|
|
2021-01-25 15:53:58 +08:00
|
|
|
private class OngoingOperation : IDisposable
|
2021-01-23 23:14:58 +08:00
|
|
|
{
|
2021-01-25 15:53:58 +08:00
|
|
|
private readonly OngoingOperationTracker tracker;
|
|
|
|
private readonly LeasedBindable<bool> lease;
|
2021-01-23 23:14:58 +08:00
|
|
|
|
2021-01-25 15:53:58 +08:00
|
|
|
public OngoingOperation(OngoingOperationTracker tracker, LeasedBindable<bool> lease)
|
2021-01-23 23:14:58 +08:00
|
|
|
{
|
2021-01-25 15:53:58 +08:00
|
|
|
this.tracker = tracker;
|
|
|
|
this.lease = lease;
|
2021-01-23 23:14:58 +08:00
|
|
|
}
|
|
|
|
|
2021-01-25 15:53:58 +08:00
|
|
|
public void Dispose()
|
2021-01-23 23:14:58 +08:00
|
|
|
{
|
2021-01-25 15:53:58 +08:00
|
|
|
tracker.endOperationWithKnownLease(lease);
|
2021-01-23 23:14:58 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 03:59:12 +08:00
|
|
|
}
|
|
|
|
}
|