From 1855f48997aad66735b6a90c94ddf49fc8e2d0c8 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 15 Mar 2017 18:07:26 +0900 Subject: [PATCH] Add cancellation support for API requests. --- osu.Game/Online/API/APIRequest.cs | 11 ++++++++++- osu.Game/Screens/Select/PlaySongSelect.cs | 10 +++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/osu.Game/Online/API/APIRequest.cs b/osu.Game/Online/API/APIRequest.cs index f871e5e20a..43c039eb1e 100644 --- a/osu.Game/Online/API/APIRequest.cs +++ b/osu.Game/Online/API/APIRequest.cs @@ -58,8 +58,12 @@ namespace osu.Game.Online.API public event APISuccessHandler Success; public event APIFailureHandler Failure; + private bool cancelled; + public void Perform(APIAccess api) { + if (cancelled) return; + if (startTime == null) startTime = DateTime.Now.TotalMilliseconds(); @@ -74,11 +78,16 @@ namespace osu.Game.Online.API WebRequest.BlockingPerform(); - api.Scheduler.Add(delegate { Success?.Invoke(); }); + if (WebRequest.Completed) + api.Scheduler.Add(delegate { Success?.Invoke(); }); } + public void Cancel() => Fail(new OperationCanceledException(@"Request cancelled")); + public void Fail(Exception e) { + cancelled = true; + WebRequest?.Abort(); api.Scheduler.Add(delegate { diff --git a/osu.Game/Screens/Select/PlaySongSelect.cs b/osu.Game/Screens/Select/PlaySongSelect.cs index 97e64f44e2..841f8eda7f 100644 --- a/osu.Game/Screens/Select/PlaySongSelect.cs +++ b/osu.Game/Screens/Select/PlaySongSelect.cs @@ -52,19 +52,23 @@ namespace osu.Game.Screens.Select }, Key.Number3); } + private GetScoresRequest getScoresRequest; + protected override void OnBeatmapChanged(WorkingBeatmap beatmap) { beatmap?.Mods.BindTo(modSelect.SelectedMods); leaderboard.Scores = null; + getScoresRequest?.Cancel(); + if (beatmap != null) { - var getScores = new GetScoresRequest(beatmap.BeatmapInfo); - getScores.Success += res => + getScoresRequest = new GetScoresRequest(beatmap.BeatmapInfo); + getScoresRequest.Success += res => { leaderboard.Scores = res.Scores; }; - Game.API.Queue(getScores); + Game.API.Queue(getScoresRequest); } base.OnBeatmapChanged(beatmap);