Skip to content

Commit

Permalink
Merge pull request #95 from dragonfruitnetwork/async-await
Browse files Browse the repository at this point in the history
replace taskcompletionsource with await
  • Loading branch information
aspriddell authored Nov 30, 2021
2 parents b03d8dc + 537d0ec commit 5f6167d
Showing 1 changed file with 19 additions and 37 deletions.
56 changes: 19 additions & 37 deletions DragonFruit.Common.Data/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,55 +204,35 @@ protected virtual void SetupRequest(HttpRequestMessage request)
/// <param name="processResult"><see cref="Func{T,TResult}"/> to process the <see cref="HttpResponseMessage"/></param>
/// <param name="disposeResponse">Whether to dispose of the <see cref="HttpResponseMessage"/> produced after <see cref="processResult"/> has been invoked.</param>
/// <param name="token">(optional) <see cref="CancellationToken"/></param>
protected Task<T> InternalPerform<T>(HttpRequestMessage request, Func<HttpResponseMessage, Task<T>> processResult, bool disposeResponse, CancellationToken token = default)
protected async Task<T> InternalPerform<T>(HttpRequestMessage request, Func<HttpResponseMessage, Task<T>> processResult, bool disposeResponse, CancellationToken token = default)
{
var (client, clientLock) = GetClient();
var monitor = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);

// post-modification
SetupRequest(request);
HttpResponseMessage response = null;

// send request
// ReSharper disable once MethodSupportsCancellation (we need to run regardless of cancellation to release lock)
client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).ContinueWith(async t =>
try
{
try
{
// evaluate task status and update monitor
switch (t.Status)
{
case TaskStatus.RanToCompletion:
monitor.SetResult(await processResult.Invoke(t.Result).ConfigureAwait(false));
break;
// send request
// ReSharper disable once MethodSupportsCancellation (we need to run regardless of cancellation to release lock)
response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).ConfigureAwait(false);

case TaskStatus.Faulted:
monitor.SetException(t.Exception?.Flatten().InnerException ?? t.Exception);
break;
// evaluate task status and update monitor
return await processResult.Invoke(response).ConfigureAwait(false);
}
finally
{
request.Dispose();

case TaskStatus.Canceled:
monitor.SetCanceled();
break;
}
}
catch (Exception e)
{
monitor.SetException(e);
}
finally
if (disposeResponse)
{
request.Dispose();

if (disposeResponse)
{
t.Result.Dispose();
}

// exit the read lock after fully processing
clientLock.Dispose();
response?.Dispose();
}
});

return monitor.Task;
// exit the read lock after fully processing
clientLock.Dispose();
}
}

/// <summary>
Expand All @@ -278,6 +258,8 @@ protected virtual async Task<T> ValidateAndProcess<T>(HttpResponseMessage respon
}

await stream.CopyToAsync(result).ConfigureAwait(false);
await stream.FlushAsync().ConfigureAwait(false);

result.Seek(0, SeekOrigin.Begin);
return result as T;
}
Expand Down

0 comments on commit 5f6167d

Please sign in to comment.