Skip to content

Commit

Permalink
Merge pull request #601 from hadashiA/ku/awaitable-support
Browse files Browse the repository at this point in the history
Awaitable support
  • Loading branch information
hadashiA authored Dec 23, 2023
2 parents 579efb9 + e8a2823 commit a057311
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,38 @@ public interface IAsyncStartable
UniTask StartAsync(CancellationToken cancellation);
}
}
#elif UNITY_2023_1_OR_NEWER
using System;
using System.Threading;
using UnityEngine;

namespace VContainer.Unity
{
public interface IAsyncStartable
{
Awaitable StartAsync(CancellationToken cancellation);
}

static class AwaitableHelper
{
public static async Awaitable Forget(Awaitable awaitable, EntryPointExceptionHandler exceptionHandler)
{
try
{
await awaitable;
}
catch (Exception ex)
{
if (exceptionHandler != null)
{
exceptionHandler.Publish(ex);
}
else
{
throw;
}
}
}
}
}
#endif
8 changes: 6 additions & 2 deletions VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
#if VCONTAINER_UNITASK_INTEGRATION
using System.Threading;
#if VCONTAINER_UNITASK_INTEGRATION
using Cysharp.Threading.Tasks;
#endif

Expand Down Expand Up @@ -293,7 +293,7 @@ public bool MoveNext()
public void Dispose() => disposed = true;
}

#if VCONTAINER_UNITASK_INTEGRATION
#if VCONTAINER_UNITASK_INTEGRATION || UNITY_2023_1_OR_NEWER
sealed class AsyncStartableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IAsyncStartable> entries;
Expand All @@ -315,10 +315,14 @@ public bool MoveNext()
foreach (var x in entries)
{
var task = x.StartAsync(cts.Token);
#if VCONTAINER_UNITASK_INTEGRATION
if (exceptionHandler != null)
task.Forget(ex => exceptionHandler.Publish(ex));
else
task.Forget();
#else
var _ = AwaitableHelper.Forget(task, exceptionHandler);
#endif
}
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/integrations/entrypoint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ And
[Unity - Manual: Order of Execution for Event Functions](https://docs.unity3d.com/Manual/ExecutionOrder.html)
:::

### async

`IAsyncStartable` is available as a variant of IStartable.
It has the same timing as IStartable, but `async Awaitable StartAsync()` is available.

If you are a UniTask user, you can also choose the UniTask version of StartAsync.
[UniTask integration](../integrations/unitask)

## Handle of the exception that was not caught

On the application side, exceptions thrown in processes such as Start() and Tick() cannot be caught outside.
Expand Down

1 comment on commit a057311

@vercel
Copy link

@vercel vercel bot commented on a057311 Dec 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.