From 10e8b7082e5affb83670333a6115af16c34ee9c8 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Jan 2021 16:53:58 +0900 Subject: [PATCH] Rework logic to avoid custom disposal early return handling --- .../OnlinePlay/OngoingOperationTracker.cs | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/osu.Game/Screens/OnlinePlay/OngoingOperationTracker.cs b/osu.Game/Screens/OnlinePlay/OngoingOperationTracker.cs index 060f1d7b91..b7ee84eb9e 100644 --- a/osu.Game/Screens/OnlinePlay/OngoingOperationTracker.cs +++ b/osu.Game/Screens/OnlinePlay/OngoingOperationTracker.cs @@ -2,7 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using System; -using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; @@ -43,42 +42,45 @@ namespace osu.Game.Screens.OnlinePlay leasedInProgress = inProgress.BeginLease(true); leasedInProgress.Value = true; - // for extra safety, marshal the end of operation back to the update thread if necessary. - return new OngoingOperation(() => Scheduler.Add(endOperation, false)); + return new OngoingOperation(this, leasedInProgress); } - private void endOperation() + private void endOperationWithKnownLease(LeasedBindable lease) { - leasedInProgress?.Return(); - leasedInProgress = null; + if (lease != leasedInProgress) + return; + + // for extra safety, marshal the end of operation back to the update thread if necessary. + Scheduler.Add(() => + { + leasedInProgress?.Return(); + leasedInProgress = null; + }, false); } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); + // base call does an UnbindAllBindables(). // clean up the leased reference here so that it doesn't get returned twice. leasedInProgress = null; } - private class OngoingOperation : InvokeOnDisposal + private class OngoingOperation : IDisposable { - private bool isDisposed; + private readonly OngoingOperationTracker tracker; + private readonly LeasedBindable lease; - public OngoingOperation(Action action) - : base(action) + public OngoingOperation(OngoingOperationTracker tracker, LeasedBindable lease) { + this.tracker = tracker; + this.lease = lease; } - public override void Dispose() + public void Dispose() { - // base class does not check disposal state for performance reasons which aren't relevant here. - // track locally, to avoid interfering with other operations in case of a potential double-disposal. - if (isDisposed) - return; - - base.Dispose(); - isDisposed = true; + tracker.endOperationWithKnownLease(lease); } } }