diff --git a/osu.Game/Screens/Multi/Match/Components/MatchBeatmapPanel.cs b/osu.Game/Screens/Multi/Match/Components/MatchBeatmapPanel.cs index 7939b18e97..7c1fe91393 100644 --- a/osu.Game/Screens/Multi/Match/Components/MatchBeatmapPanel.cs +++ b/osu.Game/Screens/Multi/Match/Components/MatchBeatmapPanel.cs @@ -1,8 +1,10 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Threading; using osu.Framework.Allocation; using osu.Framework.Graphics; +using osu.Game.Beatmaps; using osu.Game.Online.API; using osu.Game.Online.API.Requests; using osu.Game.Overlays.Direct; @@ -18,6 +20,7 @@ namespace osu.Game.Screens.Multi.Match.Components [Resolved] private RulesetStore rulesets { get; set; } + private CancellationTokenSource loadCancellation; private GetBeatmapSetRequest request; private DirectGridPanel panel; @@ -29,30 +32,31 @@ namespace osu.Game.Screens.Multi.Match.Components [BackgroundDependencyLoader] private void load() { - CurrentItem.BindValueChanged(item => + CurrentItem.BindValueChanged(item => loadNewPanel(item.NewValue?.Beatmap), true); + } + + private void loadNewPanel(BeatmapInfo beatmap) + { + loadCancellation?.Cancel(); + request?.Cancel(); + + panel?.FadeOut(200); + panel?.Expire(); + panel = null; + + if (beatmap?.OnlineBeatmapID == null) + return; + + loadCancellation = new CancellationTokenSource(); + + request = new GetBeatmapSetRequest(beatmap.OnlineBeatmapID.Value, BeatmapSetLookupType.BeatmapId); + request.Success += res => Schedule(() => { - request?.Cancel(); + panel = new DirectGridPanel(res.ToBeatmapSet(rulesets)); + LoadComponentAsync(panel, AddInternal, loadCancellation.Token); + }); - if (panel != null) - { - panel.FadeOut(200); - panel.Expire(); - panel = null; - } - - var onlineId = item.NewValue?.Beatmap.OnlineBeatmapID; - - if (onlineId.HasValue) - { - request = new GetBeatmapSetRequest(onlineId.Value, BeatmapSetLookupType.BeatmapId); - request.Success += beatmap => - { - panel = new DirectGridPanel(beatmap.ToBeatmapSet(rulesets)); - LoadComponentAsync(panel, AddInternal); - }; - api.Queue(request); - } - }, true); + api.Queue(request); } } }