mirror of
https://github.com/ppy/osu
synced 2025-01-04 13:22:08 +00:00
Add cancellation support for API requests.
This commit is contained in:
parent
08e93275fa
commit
1855f48997
@ -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
|
||||
{
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user