1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 18:12:56 +08:00

Add API methods to perform requests out-of-queue

This commit is contained in:
Dean Herbert 2019-11-29 20:03:14 +09:00
parent 4dc8e0ae20
commit c49aeb08c4
8 changed files with 46 additions and 35 deletions

View File

@ -228,7 +228,7 @@ namespace osu.Game.Tournament
if (b.BeatmapInfo == null && b.ID > 0) if (b.BeatmapInfo == null && b.ID > 0)
{ {
var req = new GetBeatmapRequest(new BeatmapInfo { OnlineBeatmapID = b.ID }); var req = new GetBeatmapRequest(new BeatmapInfo { OnlineBeatmapID = b.ID });
req.Perform(API); API.Perform(req);
b.BeatmapInfo = req.Result?.ToBeatmap(RulesetStore); b.BeatmapInfo = req.Result?.ToBeatmap(RulesetStore);
addedInfo = true; addedInfo = true;

View File

@ -398,7 +398,7 @@ namespace osu.Game.Beatmaps
try try
{ {
// intentionally blocking to limit web request concurrency // intentionally blocking to limit web request concurrency
req.Perform(api); api.Perform(req);
var res = req.Result; var res = req.Result;

View File

@ -99,17 +99,7 @@ namespace osu.Game.Database
currentDownloads.Add(request); currentDownloads.Add(request);
PostNotification?.Invoke(notification); PostNotification?.Invoke(notification);
Task.Factory.StartNew(() => api.PerformAsync(request);
{
try
{
request.Perform(api);
}
catch (Exception error)
{
triggerFailure(error);
}
}, TaskCreationOptions.LongRunning);
DownloadBegan?.Invoke(request); DownloadBegan?.Invoke(request);
return true; return true;

View File

@ -7,6 +7,7 @@ using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -198,6 +199,22 @@ namespace osu.Game.Online.API
} }
} }
public void Perform(APIRequest request)
{
try
{
request.Perform(this);
}
catch (Exception e)
{
// todo: fix exception handling
request.Fail(e);
}
}
public Task PerformAsync(APIRequest request) =>
Task.Factory.StartNew(() => Perform(request), TaskCreationOptions.LongRunning);
public void Login(string username, string password) public void Login(string username, string password)
{ {
Debug.Assert(State == APIState.Offline); Debug.Assert(State == APIState.Offline);

View File

@ -3,6 +3,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Users; using osu.Game.Users;
@ -56,6 +57,10 @@ namespace osu.Game.Online.API
{ {
} }
public void Perform(APIRequest request) { }
public Task PerformAsync(APIRequest request) => Task.CompletedTask;
public void Register(IOnlineComponent component) public void Register(IOnlineComponent component)
{ {
Scheduler.Add(delegate { components.Add(component); }); Scheduler.Add(delegate { components.Add(component); });

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Threading.Tasks;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.Users; using osu.Game.Users;
@ -42,6 +43,24 @@ namespace osu.Game.Online.API
/// <param name="request">The request to perform.</param> /// <param name="request">The request to perform.</param>
void Queue(APIRequest request); void Queue(APIRequest request);
/// <summary>
/// Perform a request immediately, bypassing any API state checks.
/// </summary>
/// <remarks>
/// Can be used to run requests as a guest user.
/// </remarks>
/// <param name="request">The request to perform.</param>
void Perform(APIRequest request);
/// <summary>
/// Perform a request immediately, bypassing any API state checks.
/// </summary>
/// <remarks>
/// Can be used to run requests as a guest user.
/// </remarks>
/// <param name="request">The request to perform.</param>
Task PerformAsync(APIRequest request);
/// <summary> /// <summary>
/// Register a component to receive state changes. /// Register a component to receive state changes.
/// </summary> /// </summary>

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -43,18 +42,7 @@ namespace osu.Game.Overlays.Changelog
}; };
req.Failure += _ => complete = true; req.Failure += _ => complete = true;
// This is done on a separate thread to support cancellation below api.PerformAsync(req);
Task.Run(() =>
{
try
{
req.Perform(api);
}
catch
{
complete = true;
}
});
while (!complete) while (!complete)
{ {

View File

@ -191,15 +191,7 @@ namespace osu.Game.Overlays
tcs.SetResult(false); tcs.SetResult(false);
}; };
try await API.PerformAsync(req);
{
req.Perform(API);
}
catch
{
initialFetchTask = null;
tcs.SetResult(false);
}
await tcs.Task; await tcs.Task;
}); });