<commit_before>﻿// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.Direct;
using osu.Game.Rulesets;

namespace osu.Game.Screens.Multi.Match.Components
{
    public class MatchBeatmapPanel : MultiplayerComposite
    {
        [Resolved]
        private IAPIProvider api { get; set; }

        [Resolved]
        private RulesetStore rulesets { get; set; }

        private GetBeatmapSetRequest request;
        private DirectGridPanel panel;

        public MatchBeatmapPanel()
        {
            AutoSizeAxes = Axes.Both;
        }

        [BackgroundDependencyLoader]
        private void load()
        {
            CurrentItem.BindValueChanged(item =>
            {
                request?.Cancel();

                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);
        }
    }
}
<commit_msg>Refactor MatchBeatmapPanel for thread safety<commit_after>﻿// Copyright (c) p<NAME>ty Ltd <<EMAIL>>. 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;
using osu.Game.Rulesets;

namespace osu.Game.Screens.Multi.Match.Components
{
    public class MatchBeatmapPanel : MultiplayerComposite
    {
        [Resolved]
        private IAPIProvider api { get; set; }

        [Resolved]
        private RulesetStore rulesets { get; set; }

        private CancellationTokenSource loadCancellation;
        private GetBeatmapSetRequest request;
        private DirectGridPanel panel;

        public MatchBeatmapPanel()
        {
            AutoSizeAxes = Axes.Both;
        }

        [BackgroundDependencyLoader]
        private void load()
        {
            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(() =>
            {
                panel = new DirectGridPanel(res.ToBeatmapSet(rulesets));
                LoadComponentAsync(panel, AddInternal, loadCancellation.Token);
            });

            api.Queue(request);
        }
    }
}

{'fraction_non_alphanumeric': 0.08883319409635199, 'fraction_numerical': 0.001670843776106934, 'mean_word_length': 1.8944399677679291, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '826475', 'n_tokens_mistral': 1011, 'n_tokens_neox': 940, 'n_words': 207}