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.
|
|
|
|
|
|
|
|
using System;
|
2020-12-29 14:54:27 +08:00
|
|
|
using osu.Framework.Allocation;
|
2020-12-29 03:59:12 +08:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
|
|
|
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>
|
|
|
|
public class OngoingOperationTracker
|
|
|
|
{
|
|
|
|
/// <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;
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
|
|
return new InvokeOnDisposal(endOperation);
|
2020-12-29 03:59:12 +08:00
|
|
|
}
|
|
|
|
|
2020-12-29 14:54:27 +08:00
|
|
|
private void endOperation()
|
2020-12-29 03:59:12 +08:00
|
|
|
{
|
2021-01-09 05:17:37 +08:00
|
|
|
if (leasedInProgress == null)
|
|
|
|
throw new InvalidOperationException("Cannot end operation multiple times.");
|
|
|
|
|
|
|
|
leasedInProgress.Return();
|
2020-12-29 03:59:12 +08:00
|
|
|
leasedInProgress = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|