1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-06 08:22:56 +08:00

Store all linked cancellation tokens

This commit is contained in:
smoogipoo 2020-07-28 16:52:07 +09:00
parent 1ff3b20a5e
commit ff3cb6487d

View File

@ -109,25 +109,42 @@ namespace osu.Game.Beatmaps
} }
private CancellationTokenSource trackedUpdateCancellationSource; private CancellationTokenSource trackedUpdateCancellationSource;
private readonly List<CancellationTokenSource> linkedCancellationSources = new List<CancellationTokenSource>();
/// <summary> /// <summary>
/// Updates all tracked <see cref="BindableStarDifficulty"/> using the current ruleset and mods. /// Updates all tracked <see cref="BindableStarDifficulty"/> using the current ruleset and mods.
/// </summary> /// </summary>
private void updateTrackedBindables() private void updateTrackedBindables()
{ {
trackedUpdateCancellationSource?.Cancel(); cancelTrackedBindableUpdate();
trackedUpdateCancellationSource = new CancellationTokenSource(); trackedUpdateCancellationSource = new CancellationTokenSource();
foreach (var b in trackedBindables) foreach (var b in trackedBindables)
{ {
if (trackedUpdateCancellationSource.IsCancellationRequested) var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(trackedUpdateCancellationSource.Token, b.CancellationToken);
break; linkedCancellationSources.Add(linkedSource);
using (var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(trackedUpdateCancellationSource.Token, b.CancellationToken)) updateBindable(b, currentRuleset.Value, currentMods.Value, linkedSource.Token);
updateBindable(b, currentRuleset.Value, currentMods.Value, linkedSource.Token);
} }
} }
/// <summary>
/// Cancels the existing update of all tracked <see cref="BindableStarDifficulty"/> via <see cref="updateTrackedBindables"/>.
/// </summary>
private void cancelTrackedBindableUpdate()
{
trackedUpdateCancellationSource?.Cancel();
trackedUpdateCancellationSource = null;
foreach (var c in linkedCancellationSources)
{
c.Cancel();
c.Dispose();
}
linkedCancellationSources.Clear();
}
/// <summary> /// <summary>
/// Updates the value of a <see cref="BindableStarDifficulty"/> with a given ruleset + mods. /// Updates the value of a <see cref="BindableStarDifficulty"/> with a given ruleset + mods.
/// </summary> /// </summary>
@ -220,6 +237,12 @@ namespace osu.Game.Beatmaps
return difficultyCache.TryGetValue(key, out existingDifficulty); return difficultyCache.TryGetValue(key, out existingDifficulty);
} }
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
cancelTrackedBindableUpdate();
}
private readonly struct DifficultyCacheLookup : IEquatable<DifficultyCacheLookup> private readonly struct DifficultyCacheLookup : IEquatable<DifficultyCacheLookup>
{ {
public readonly int BeatmapId; public readonly int BeatmapId;