Skip to content

Commit

Permalink
Merge pull request #285 from hadashiA/ku/initialize-immediate
Browse files Browse the repository at this point in the history
Make sure that Initialize is called immediately
  • Loading branch information
hadashiA authored Jul 21, 2021
2 parents 6d29936 + 6ff8a06 commit b3f21cf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 92 deletions.
34 changes: 25 additions & 9 deletions VContainer/Assets/VContainer/Runtime/Unity/EntryPointDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,35 @@ public void Dispatch()
}

var initializables = container.Resolve<IReadOnlyList<IInitializable>>();
if (initializables.Count > 0)
for (var i = 0; i < initializables.Count; i++)
{
var loopItem = new InitializationLoopItem(initializables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Initialization, loopItem);
try
{
initializables[i].Initialize();
}
catch (Exception ex)
{
if (exceptionHandler != null)
exceptionHandler.Publish(ex);
else
UnityEngine.Debug.LogException(ex);
}
}

var postInitializables = container.Resolve<IReadOnlyList<IPostInitializable>>();
if (postInitializables.Count > 0)
for (var i = 0; i < postInitializables.Count; i++)
{
var loopItem = new PostInitializationLoopItem(postInitializables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostInitialization, loopItem);
try
{
postInitializables[i].PostInitialize();
}
catch (Exception ex)
{
if (exceptionHandler != null)
exceptionHandler.Publish(ex);
else
UnityEngine.Debug.LogException(ex);
}
}

var startables = container.Resolve<IReadOnlyList<IStartable>>();
Expand Down Expand Up @@ -130,7 +146,7 @@ public void Dispatch()
x.SortSystems();
}
#endif
}
}

public void Dispose() => disposable.Dispose();
}
Expand Down
71 changes: 0 additions & 71 deletions VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs
Original file line number Diff line number Diff line change
@@ -1,83 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if VCONTAINER_UNITASK_INTEGRATION
using System.Threading;
using Cysharp.Threading.Tasks;
#endif

namespace VContainer.Unity
{
sealed class InitializationLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IInitializable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public InitializationLoopItem(
IEnumerable<IInitializable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
this.exceptionHandler = exceptionHandler;
}

public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
{
try
{
x.Initialize();
}
catch (Exception ex)
{
if (exceptionHandler == null) throw;
exceptionHandler.Publish(ex);
}
}
return false;
}

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

sealed class PostInitializationLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IPostInitializable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public PostInitializationLoopItem(
IEnumerable<IPostInitializable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
this.exceptionHandler = exceptionHandler;
}

public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
{
try
{
x.PostInitialize();
}
catch (Exception ex)
{
if (exceptionHandler == null) throw;
exceptionHandler.Publish(ex);
}
}
return false;
}

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

sealed class StartableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IStartable> entries;
Expand Down
4 changes: 2 additions & 2 deletions website/docs/integrations/entrypoint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Since it uses PlayerLoopSystem, it works even if you register at any time (e.g:

| VContaienr entry point | Timing |
|:--------------------------------------|:----------------------------------|
| `IInitializable.Initialize()` | Early `PlayerLoop.Initialization` |
| `IPostInitializable.PostInitialize()` | Late `PlayerLoop.Initialization` |
| `IInitializable.Initialize()` | Immediately after building the container |
| `IPostInitializable.PostInitialize()` | Late `IInitializable.Initialize()` |
| `IStartable.Start()` | Nearly `MonoBehaviour.Start()` |
| `IPostStartable.PostStart()` | After `MonoBehaviour.Start()` |
| `IFixedTickable.FixedTick()` | Nearly `MonoBehaviour.FixedUpdate()` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
title: Plain C# Entry point
---

VContainerを使うと、素のC#オブジェクトをアプリケーションの処理の起点にすることができます。
VContainer allows plain C# to be the starting point for application processing.

これを `MonoBehaviour` の代わりに使用することで、制御フローのみをUnityの色々な機能とは分離することができます。

以下は簡単な例です:
Using it instead of MonoBehaviour, which has a lot of features, can help you build a simple control flow.

```csharp
clas FooController : IStartable
{
void IStartable.Start()
{
// イベント監視やルーチンなどを開始...
// Do something ...
}
}
```
Expand All @@ -23,9 +19,7 @@ clas FooController : IStartable
builder.RegisterEntryPoint<FooController>();
```

:::tip
See [register](../registering/register-type#register-lifecycle-marker-interfaces)
:::

VContainer does this with its own PlayerLoopSystem.

Expand All @@ -38,8 +32,8 @@ Since it uses PlayerLoopSystem, it works even if you register at any time (e.g:

| VContaienr entry point | Timing |
|:--------------------------------------|:----------------------------------|
| `IInitializable.Initialize()` | Early `PlayerLoop.Initialization` |
| `IPostInitializable.PostInitialize()` | Late `PlayerLoop.Initialization` |
| `IInitializable.Initialize()` | Immediately after building the container |
| `IPostInitializable.PostInitialize()` | Late `IInitializable.Initialize()` |
| `IStartable.Start()` | Nearly `MonoBehaviour.Start()` |
| `IPostStartable.PostStart()` | After `MonoBehaviour.Start()` |
| `IFixedTickable.FixedTick()` | Nearly `MonoBehaviour.FixedUpdate()` |
Expand Down

1 comment on commit b3f21cf

@vercel
Copy link

@vercel vercel bot commented on b3f21cf Jul 21, 2021

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.