1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 17:32:54 +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 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
{

View File

@ -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);