Merge pull request #8810 from peppy/fix-web-request-result

Fix inline executions of APIRequest.Perform not getting result populated early enough
This commit is contained in:
Dan Balasescu 2020-04-20 20:23:08 +09:00 committed by GitHub
commit ea77fc222d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 7 deletions

View File

@ -18,24 +18,32 @@ public abstract class APIRequest<T> : APIRequest where T : class
public T Result { get; private set; }
protected APIRequest()
{
base.Success += () => TriggerSuccess(((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject);
}
/// <summary>
/// Invoked on successful completion of an API request.
/// This will be scheduled to the API's internal scheduler (run on update thread automatically).
/// </summary>
public new event APISuccessHandler<T> Success;
protected override void PostProcess()
{
base.PostProcess();
Result = ((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject;
}
internal void TriggerSuccess(T result)
{
if (Result != null)
throw new InvalidOperationException("Attempted to trigger success more than once");
Result = result;
Success?.Invoke(result);
TriggerSuccess();
}
internal override void TriggerSuccess()
{
base.TriggerSuccess();
Success?.Invoke(Result);
}
}
@ -99,6 +107,8 @@ public void Perform(IAPIProvider api)
if (checkAndScheduleFailure())
return;
PostProcess();
API.Schedule(delegate
{
if (cancelled) return;
@ -107,7 +117,14 @@ public void Perform(IAPIProvider api)
});
}
internal void TriggerSuccess()
/// <summary>
/// Perform any post-processing actions after a successful request.
/// </summary>
protected virtual void PostProcess()
{
}
internal virtual void TriggerSuccess()
{
Success?.Invoke();
}