From eb3a529b79024fb41c658e2ce87d4dba6429bf3c Mon Sep 17 00:00:00 2001 From: hadashiA Date: Wed, 20 Dec 2023 16:46:04 +0900 Subject: [PATCH] Awaitable support --- .../Runtime/Annotations/IAsyncStartable.cs | 34 +++++++++++++++++++ .../Runtime/Unity/PlayerLoopItem.cs | 8 +++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs b/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs index bd9fdbac..ef2a2b0a 100644 --- a/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs +++ b/VContainer/Assets/VContainer/Runtime/Annotations/IAsyncStartable.cs @@ -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 diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index d8dab74d..f9b6ecdd 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -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 @@ -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 entries; @@ -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; }