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;
|
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-10 04:38:20 +08:00
|
|
|
// for extra safety, marshal the end of operation back to the update thread if necessary.
|
|
|
|
return new InvokeOnDisposal(() => Scheduler.Add(endOperation, false));
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|