Skip to content

Commit

Permalink
Awaitable support
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 20, 2023
1 parent c2d19a7 commit eb3a529
Show file tree
Hide file tree
Showing 2 changed files with 40 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

0 comments on commit eb3a529

Please sign in to comment.