Skip to content

Commit

Permalink
Add async entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Jan 29, 2021
1 parent 834120c commit 11da734
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 189 deletions.
40 changes: 37 additions & 3 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ void ActivateEntryPoints()
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem);
}

var postStartable = Container.Resolve<IReadOnlyList<IPostStartable>>();
if (postInitializables.Count > 0)
var postStartables = Container.Resolve<IReadOnlyList<IPostStartable>>();
if (postStartables.Count > 0)
{
var loopItem = new PostStartableLoopItem(postStartable);
var loopItem = new PostStartableLoopItem(postStartables);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostStartup, loopItem);
}
Expand Down Expand Up @@ -368,6 +368,40 @@ void ActivateEntryPoints()
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostLateUpdate, loopItem);
}

#if VCONTAINER_UNITASK_INTEGRATION
var asyncInitializables = Container.Resolve<IReadOnlyList<IAsyncInitializable>>();
if (asyncInitializables.Count > 0)
{
var loopItem = new AsyncInitializationLoopItem(asyncInitializables);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Initialization, loopItem);
}

var asyncPostInitializables = Container.Resolve<IReadOnlyList<IAsyncPostInitializable>>();
if (asyncPostInitializables.Count > 0)
{
var loopItem = new AsyncPostInitializationLoopItem(asyncPostInitializables);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostInitialization, loopItem);
}

var asyncStartables = Container.Resolve<IReadOnlyList<IAsyncStartable>>();
if (asyncStartables.Count > 0)
{
var loopItem = new AsyncStartableLoopItem(asyncStartables);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem);
}

var asyncPostStartables = Container.Resolve<IReadOnlyList<IAsyncPostStartable>>();
if (asyncPostStartables.Count > 0)
{
var loopItem = new AsyncPostStartableLoopItem(asyncPostStartables);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostStartup, loopItem);
}
#endif

#if VCONTAINER_ECS_INTEGRATION
Container.Resolve<IEnumerable<ComponentSystemBase>>();

Expand Down
119 changes: 119 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
#if VCONTAINER_UNITASK_INTEGRATION
using Cysharp.Threading.Tasks;
#endif

namespace VContainer.Unity
{
Expand Down Expand Up @@ -53,6 +56,28 @@ public interface IPostLateTickable
void PostLateTick();
}

#if VCONTAINER_UNITASK_INTEGRATION
public interface IAsyncInitializable
{
UniTaskVoid InitializeAsync();
}

public interface IAsyncPostInitializable
{
UniTaskVoid PostInitializeAsync();
}

public interface IAsyncStartable
{
UniTaskVoid StartAsync();
}

public interface IAsyncPostStartable
{
UniTaskVoid PostStartAsync();
}
#endif

sealed class InitializationLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IInitializable> entries;
Expand Down Expand Up @@ -282,4 +307,98 @@ public bool MoveNext()

public void Dispose() => disposed = true;
}

#if VCONTAINER_UNITASK_INTEGRATION
sealed class AsyncInitializationLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IAsyncInitializable> entries;
bool disposed;

public AsyncInitializationLoopItem(IEnumerable<IAsyncInitializable> entries)
{
this.entries = entries;
}

public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
{
x.InitializeAsync().Forget();
}
return false;
}

public void Dispose() => disposed = true;
}

sealed class AsyncPostInitializationLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IAsyncPostInitializable> entries;
bool disposed;

public AsyncPostInitializationLoopItem(IEnumerable<IAsyncPostInitializable> entries)
{
this.entries = entries;
}

public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
{
x.PostInitializeAsync().Forget();
}
return false;
}

public void Dispose() => disposed = true;
}

sealed class AsyncStartableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IAsyncStartable> entries;
bool disposed;

public AsyncStartableLoopItem(IEnumerable<IAsyncStartable> entries)
{
this.entries = entries;
}

public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
{
x.StartAsync().Forget();
}
return false;
}

public void Dispose() => disposed = true;
}

sealed class AsyncPostStartableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IAsyncPostStartable> entries;
bool disposed;

public AsyncPostStartableLoopItem(IEnumerable<IAsyncPostStartable> entries)
{
this.entries = entries;
}

public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
{
x.PostStartAsync().Forget();
}
return false;
}

public void Dispose() => disposed = true;
}
#endif
}
9 changes: 8 additions & 1 deletion VContainer/Assets/VContainer/Runtime/VContainer.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "VContainer",
"rootNamespace": "",
"references": [
"GUID:734d92eba21c94caba915361bd5ac177"
"GUID:734d92eba21c94caba915361bd5ac177",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -15,6 +17,11 @@
"name": "com.unity.entities",
"expression": "",
"define": "VCONTAINER_ECS_INTEGRATION"
},
{
"name": "com.cysharp.unitask",
"expression": "",
"define": "VCONTAINER_UNITASK_INTEGRATION"
}
],
"noEngineReferences": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Cysharp.Threading.Tasks;
using VContainer.Unity;
using PlayerLoopTiming = Cysharp.Threading.Tasks.PlayerLoopTiming;

namespace VContainer.Tests.Unity
{
public sealed class SampleEntryPoint :
public class SampleEntryPoint :
IInitializable,
IPostInitializable,
IStartable,
Expand Down Expand Up @@ -36,4 +38,42 @@ public sealed class SampleEntryPoint :
void ILateTickable.LateTick() => LateTickCalls += 1;
void IPostLateTickable.PostLateTick() => PostLateTickCalls += 1;
}

public class SampleAsyncEntryPoint :
IAsyncInitializable,
IAsyncPostInitializable,
IAsyncStartable,
IAsyncPostStartable
{
public bool Initialized;
public bool PostInitialized;
public bool Started;
public bool PostStarted;

public async UniTaskVoid InitializeAsync()
{
UnityEngine.Debug.Log("@@@@@@ 1111");
await UniTask.Yield();
UnityEngine.Debug.Log("@@@@@@ 2222");
Initialized = true;
}

public async UniTaskVoid PostInitializeAsync()
{
await UniTask.Yield();
PostInitialized = true;
}

public async UniTaskVoid StartAsync()
{
await UniTask.Yield();
Started = true;
}

public async UniTaskVoid PostStartAsync()
{
await UniTask.Yield();
PostStarted = true;
}
}
}
36 changes: 36 additions & 0 deletions VContainer/Assets/VContainer/Tests/Unity/UniTaskTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using UnityEngine.TestTools;
using VContainer.Unity;

namespace VContainer.Tests.Unity
{
public class UniTaskTest
{
[UnityTest]
public IEnumerator AsyncStartup() => UniTask.ToCoroutine(async () =>
{
var parentLifetimeScope = LifetimeScope.Create(builder =>
{
builder.RegisterEntryPoint<SampleAsyncEntryPoint>(Lifetime.Scoped).AsSelf();
});

var entryPoint = parentLifetimeScope.Container.Resolve<SampleAsyncEntryPoint>();
UnityEngine.Debug.Log("00000");
Assert.That(entryPoint.Initialized, Is.False);
Assert.That(entryPoint.PostInitialized, Is.False);
Assert.That(entryPoint.Started, Is.False);
Assert.That(entryPoint.PostStarted, Is.False);

await UniTask.Yield();
UnityEngine.Debug.Log("222222");

Assert.That(entryPoint.Initialized, Is.True);
Assert.That(entryPoint.PostInitialized, Is.True);
Assert.That(entryPoint.Started, Is.True);
Assert.That(entryPoint.PostStarted, Is.True);
});
}
}
3 changes: 3 additions & 0 deletions VContainer/Assets/VContainer/Tests/Unity/UniTaskTest.cs.meta

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

20 changes: 8 additions & 12 deletions VContainer/Assets/VContainer/Tests/VContainer.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "VContainer.Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"VContainer",
"GUID:734d92eba21c94caba915361bd5ac177"
"GUID:27619889b8ba8c24980f49ee34dbb44a",
"GUID:0acc523941302664db1f4e527237feb3",
"GUID:b0214a6008ed146ff8f122a6a9c2f6cc",
"GUID:734d92eba21c94caba915361bd5ac177",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [],
"excludePlatforms": [
Expand All @@ -19,12 +21,6 @@
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [
{
"name": "com.unity.entities",
"expression": "",
"define": "VCONTAINER_ECS_INTEGRATION"
}
],
"versionDefines": [],
"noEngineReferences": false
}
}
7 changes: 6 additions & 1 deletion VContainer/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
"com.unity.entities": "0.11.1-preview.4",
"com.unity.ide.rider": "2.0.7",
"com.unity.ide.visualstudio": "2.0.5",
Expand All @@ -13,6 +14,10 @@
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.uielements": "1.0.0"
"com.unity.modules.uielements": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0",
"com.unity.modules.physics": "1.0.0",
"com.unity.modules.physics2d": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0"
}
}
Loading

0 comments on commit 11da734

Please sign in to comment.