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

Add cancellation support for API requests.

This commit is contained in:
Dean Herbert 2017-03-15 18:07:26 +09:00
parent 08e93275fa
commit 1855f48997
No known key found for this signature in database
GPG Key ID: 46D71BF4958ABB49
2 changed files with 17 additions and 4 deletions

View File

@ -58,8 +58,12 @@ namespace osu.Game.Online.API
public event APISuccessHandler Success; public event APISuccessHandler Success;
public event APIFailureHandler Failure; public event APIFailureHandler Failure;
private bool cancelled;
public void Perform(APIAccess api) public void Perform(APIAccess api)
{ {
if (cancelled) return;
if (startTime == null) if (startTime == null)
startTime = DateTime.Now.TotalMilliseconds(); startTime = DateTime.Now.TotalMilliseconds();
@ -74,11 +78,16 @@ namespace osu.Game.Online.API
WebRequest.BlockingPerform(); 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) public void Fail(Exception e)
{ {
cancelled = true;
WebRequest?.Abort(); WebRequest?.Abort();
api.Scheduler.Add(delegate api.Scheduler.Add(delegate
{ {

View File

@ -52,19 +52,23 @@ namespace osu.Game.Screens.Select
}, Key.Number3); }, Key.Number3);
} }
private GetScoresRequest getScoresRequest;
protected override void OnBeatmapChanged(WorkingBeatmap beatmap) protected override void OnBeatmapChanged(WorkingBeatmap beatmap)
{ {
beatmap?.Mods.BindTo(modSelect.SelectedMods); beatmap?.Mods.BindTo(modSelect.SelectedMods);
leaderboard.Scores = null; leaderboard.Scores = null;
getScoresRequest?.Cancel();
if (beatmap != null) if (beatmap != null)
{ {
var getScores = new GetScoresRequest(beatmap.BeatmapInfo); getScoresRequest = new GetScoresRequest(beatmap.BeatmapInfo);
getScores.Success += res => getScoresRequest.Success += res =>
{ {
leaderboard.Scores = res.Scores; leaderboard.Scores = res.Scores;
}; };
Game.API.Queue(getScores); Game.API.Queue(getScoresRequest);
} }
base.OnBeatmapChanged(beatmap); base.OnBeatmapChanged(beatmap);