2020-12-28 19:59:12 +00: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;
|
|
|
|
using osu.Framework.Bindables;
|
2021-01-09 20:38:20 +00:00
|
|
|
using osu.Framework.Graphics;
|
2020-12-28 19:59:12 +00: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-09 20:38:20 +00:00
|
|
|
public class OngoingOperationTracker : Component
|
2020-12-28 19:59:12 +00: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-09 20:38:20 +00:00
|
|
|
public OngoingOperationTracker()
|
|
|
|
{
|
|
|
|
AlwaysPresent = true;
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:59:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Begins tracking a new online operation.
|
|
|
|
/// </summary>
|
2020-12-29 06:54:27 +00:00
|
|
|
/// <returns>
|
|
|
|
/// An <see cref="IDisposable"/> that will automatically mark the operation as ended on disposal.
|
|
|
|
/// </returns>
|
2020-12-28 19:59:12 +00:00
|
|
|
/// <exception cref="InvalidOperationException">An operation has already been started.</exception>
|
2020-12-29 06:54:27 +00:00
|
|
|
public IDisposable BeginOperation()
|
2020-12-28 19:59:12 +00: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 06:54:27 +00:00
|
|
|
|
2021-01-25 07:53:58 +00:00
|
|
|
return new OngoingOperation(this, leasedInProgress);
|
2020-12-28 19:59:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 07:53:58 +00:00
|
|
|
private void endOperationWithKnownLease(LeasedBindable<bool> lease)
|
2020-12-28 19:59:12 +00:00
|
|
|
{
|
2021-01-25 07:53:58 +00:00
|
|
|
// for extra safety, marshal the end of operation back to the update thread if necessary.
|
|
|
|
Scheduler.Add(() =>
|
|
|
|
{
|
2021-01-30 20:03:09 +00:00
|
|
|
if (lease != leasedInProgress)
|
|
|
|
return;
|
|
|
|
|
2021-01-30 20:00:13 +00: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 07:53:58 +00:00
|
|
|
leasedInProgress = null;
|
|
|
|
}, false);
|
2020-12-28 19:59:12 +00:00
|
|
|
}
|
2021-01-23 15:04:12 +00:00
|
|
|
|
2021-01-25 07:53:58 +00:00
|
|
|
private class OngoingOperation : IDisposable
|
2021-01-23 15:14:58 +00:00
|
|
|
{
|
2021-01-25 07:53:58 +00:00
|
|
|
private readonly OngoingOperationTracker tracker;
|
|
|
|
private readonly LeasedBindable<bool> lease;
|
2021-01-23 15:14:58 +00:00
|
|
|
|
2021-01-25 07:53:58 +00:00
|
|
|
public OngoingOperation(OngoingOperationTracker tracker, LeasedBindable<bool> lease)
|
2021-01-23 15:14:58 +00:00
|
|
|
{
|
2021-01-25 07:53:58 +00:00
|
|
|
this.tracker = tracker;
|
|
|
|
this.lease = lease;
|
2021-01-23 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 07:53:58 +00:00
|
|
|
public void Dispose()
|
2021-01-23 15:14:58 +00:00
|
|
|
{
|
2021-01-25 07:53:58 +00:00
|
|
|
tracker.endOperationWithKnownLease(lease);
|
2021-01-23 15:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 19:59:12 +00:00
|
|
|
}
|
|
|
|
}
|