2020-01-11 17:21:31 +01:00

54 lines
1.2 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
#nullable enable
namespace Capnp.Rpc
{
class LocalAnswer : IPromisedAnswer
{
readonly CancellationTokenSource _cts;
public LocalAnswer(CancellationTokenSource cts, Task<DeserializerState> call)
{
_cts = cts ?? throw new ArgumentNullException(nameof(cts));
WhenReturned = call ?? throw new ArgumentNullException(nameof(call));
CleanupAfterReturn();
}
async void CleanupAfterReturn()
{
try
{
await WhenReturned;
}
catch
{
}
finally
{
_cts.Dispose();
}
}
public Task<DeserializerState> WhenReturned { get; }
public ConsumedCapability Access(MemberAccessPath access)
{
return new LocalAnswerCapability(WhenReturned, access);
}
public void Dispose()
{
try
{
_cts.Cancel();
}
catch (ObjectDisposedException)
{
}
}
}
}
#nullable restore