2020-03-10 21:55:34 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Capnp.Rpc
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
static class ResolvingCapabilityExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void ExportAsSenderPromise<T>(this T cap, IRpcEndpoint endpoint, CapDescriptor.WRITER writer)
|
|
|
|
|
where T: ConsumedCapability, IResolvingCapability
|
|
|
|
|
{
|
|
|
|
|
var vine = Vine.Create(cap);
|
|
|
|
|
uint preliminaryId = endpoint.AllocateExport(vine, out bool first);
|
|
|
|
|
|
|
|
|
|
writer.which = CapDescriptor.WHICH.SenderPromise;
|
|
|
|
|
writer.SenderPromise = preliminaryId;
|
|
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
endpoint.RequestPostAction(async () => {
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var resolvedCap = await cap.WhenResolved;
|
2020-03-10 21:55:34 +01:00
|
|
|
|
endpoint.Resolve(preliminaryId, vine, () => resolvedCap!);
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
catch (System.Exception exception)
|
|
|
|
|
{
|
|
|
|
|
endpoint.Resolve(preliminaryId, vine, () => throw exception);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:55:34 +01:00
|
|
|
|
public static async Task<Proxy> AsProxyTask(this Task<IDisposable> task)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
2020-03-10 21:55:34 +01:00
|
|
|
|
var obj = await task;
|
|
|
|
|
return obj is Proxy proxy ? proxy : BareProxy.FromImpl(obj);
|
2019-06-12 21:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-11 17:56:12 +01:00
|
|
|
|
}
|