Skip to content

Commit

Permalink
Merge pull request #476 from AndreyG/RdTask.GetAwaiter
Browse files Browse the repository at this point in the history
implement RdTask.GetAwaiter
  • Loading branch information
Iliya-usov authored Mar 15, 2024
2 parents ed4bba2 + 063066d commit 401a19e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
23 changes: 23 additions & 0 deletions rd-net/RdFramework/Tasks/RdTaskEx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -121,5 +122,27 @@ public static Task<T> AsTask<T>(this IRdTask<T> task)
});
return tcs.Task;
}

[PublicAPI]
public static RdTaskAwaiter<T> GetAwaiter<T>(this IRdTask<T> task) => new(task.Result);

public readonly struct RdTaskAwaiter<T> : INotifyCompletion
{
private readonly IReadonlyProperty<RdTaskResult<T>> myResult;

internal RdTaskAwaiter(IReadonlyProperty<RdTaskResult<T>> result)
{
myResult = result;
}

public bool IsCompleted => myResult.Maybe.HasValue;

public T GetResult() => myResult.Value.Unwrap();

public void OnCompleted(Action continuation)
{
myResult.Change.AdviseOnce(Lifetime.Eternal, _ => continuation());
}
}
}
}
26 changes: 24 additions & 2 deletions rd-net/Test.RdFramework/AsyncRdTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@ protected override IScheduler CreateScheduler(bool isServer)
}

[Test]
public void BindableRdCallListTest()
public void BindableRdCallListUseSystemTaskTest()
{
BindableRdCallListTest(TaskKind.System);
}

[Test]
public void BindableRdCallListUseRdTaskTest()
{
BindableRdCallListTest(TaskKind.Rd);
}

private enum TaskKind
{
System,
Rd,
}

private void BindableRdCallListTest(TaskKind taskKind)
{
ClientWire.AutoTransmitMode = true;
ServerWire.AutoTransmitMode = true;
Expand Down Expand Up @@ -52,7 +69,12 @@ public void BindableRdCallListTest()
{
BindToClient(lifetime, callsite, entity_id);
var list = await callsite.Start(lifetime, Unit.Instance).AsTask();
var list = taskKind switch
{
TaskKind.System => await callsite.Start(lifetime, Unit.Instance).AsTask(),
TaskKind.Rd => await callsite.Start(lifetime, Unit.Instance),
_ => throw new ArgumentOutOfRangeException(nameof(taskKind), taskKind, null)
};
var count = 0;
list.View(lifetime, (lt, index, value) =>
Expand Down

0 comments on commit 401a19e

Please sign in to comment.