Skip to content

Commit

Permalink
Merge pull request #102 from AnnulusGames/optimize-awaiter
Browse files Browse the repository at this point in the history
Add: Lightweight Awaiter without UniTask
  • Loading branch information
AnnulusGames authored Mar 16, 2024
2 parents 78aec23 + 157d2ff commit 92b96ec
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ namespace LitMotion
/// </summary>
public static class LitMotionUniTaskExtensions
{
public static UniTask.Awaiter GetAwaiter(this MotionHandle handle)
{
return ToUniTask(handle).GetAwaiter();
}

/// <summary>
/// Convert motion handle to UniTask.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionAwaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#if LITMOTION_SUPPORT_UNITASK
using System;
using System.Runtime.CompilerServices;

namespace LitMotion
{
public readonly struct MotionAwaiter : ICriticalNotifyCompletion
{
readonly MotionHandle handle;
public bool IsCompleted => !handle.IsActive();

public MotionAwaiter(MotionHandle handle)
{
this.handle = handle;
}

public MotionAwaiter GetAwaiter()
{
return this;
}

public void GetResult()
{
}

public void OnCompleted(Action continuation)
{
UnsafeOnCompleted(continuation);
}

public void UnsafeOnCompleted(Action continuation)
{
if (continuation == null) return;

var callbackData = MotionStorageManager.GetMotionCallbacks(handle);
callbackData.OnCompleteAction += continuation;
MotionStorageManager.SetMotionCallbacks(handle, callbackData);
}
}
}
#endif
2 changes: 2 additions & 0 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionAwaiter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public static IEnumerator ToYieldInteraction(this MotionHandle handle)
yield return null;
}
}

public static MotionAwaiter GetAwaiter(this MotionHandle handle)
{
return new MotionAwaiter(handle);
}

public static ValueTask ToValueTask(this MotionHandle handle, CancellationToken cancellationToken = default)
{
Expand Down
24 changes: 24 additions & 0 deletions src/LitMotion/Assets/LitMotion/Tests/Runtime/AwaitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading.Tasks;
using NUnit.Framework;
using UnityEngine.TestTools.Utils;

namespace LitMotion.Tests.Runtime
{
public class AwaitTest
{
[Test]
public async Task Test_AwaitManyTimes()
{
var value = 0f;
var startValue = 0f;
var endValue = 10f;

for (int i = 0; i < 50; i++)
{
await LMotion.Create(startValue, endValue, 0.1f)
.Bind(x => value = x);
Assert.That(value, Is.EqualTo(10f).Using(FloatEqualityComparer.Instance));
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/LitMotion/Assets/LitMotion/Tests/Runtime/UniTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public void OneTimeTearDown()
public IEnumerator Test_ToUniTask() => UniTask.ToCoroutine(async () =>
{
await LMotion.Create(0f, 10f, 1f)
.BindToUnityLogger();
.BindToUnityLogger()
.ToUniTask();
});

[UnityTest]
Expand All @@ -51,7 +52,8 @@ public IEnumerator Test_AwaitManyTimes() => UniTask.ToCoroutine(async () =>
for (int i = 0; i < 50; i++)
{
await LMotion.Create(startValue, endValue, 0.1f)
.Bind(x => value = x);
.Bind(x => value = x)
.ToUniTask();
Assert.That(value, Is.EqualTo(10f).Using(FloatEqualityComparer.Instance));
}
});
Expand All @@ -63,7 +65,7 @@ public IEnumerator Test_CancelWhileAwait() => UniTask.ToCoroutine(async () =>
DelayedCall(0.2f, () => handle.Cancel()).Forget();
try
{
await handle;
await handle.ToUniTask();
}
catch (OperationCanceledException)
{
Expand All @@ -86,7 +88,7 @@ public IEnumerator Test_CancelWhileAwait_WithCancelOnError() => UniTask.ToCorout

try
{
await handle;
await handle.ToUniTask();
}
catch (OperationCanceledException)
{
Expand Down

0 comments on commit 92b96ec

Please sign in to comment.