Skip to content

Commit

Permalink
Merge pull request #119 from hadashiA/ku/partial-lifetimescope
Browse files Browse the repository at this point in the history
Make LifetimeScope as patial
  • Loading branch information
hadashiA authored Feb 9, 2021
2 parents 7692e01 + 28a802f commit 21c04ac
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace VContainer.Unity
{
public sealed class VContainerParentTypeReferenceNotFound : Exception
{
public readonly Type ParentType;

public VContainerParentTypeReferenceNotFound(Type parentType, string message)
: base(message)
{
ParentType = parentType;
}
}


public partial class LifetimeScope
{
static readonly List<(LifetimeScope, VContainerParentTypeReferenceNotFound)> WaitingList =
new List<(LifetimeScope, VContainerParentTypeReferenceNotFound)>();

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void SubscribeSceneEvents()
{
SceneManager.sceneLoaded -= ReleaseWaitingList;
SceneManager.sceneLoaded += ReleaseWaitingList;
}

static void WaitForAwake(LifetimeScope lifetimeScope, VContainerParentTypeReferenceNotFound ex)
{
WaitingList.Add((lifetimeScope, ex));
}

static void CancelWaiting(LifetimeScope lifetimeScope)
{
for (var i = WaitingList.Count - 1; i >= 0; i--)
{
if (WaitingList[i].Item1 == lifetimeScope)
{
WaitingList.RemoveAt(i);
}
}
}

static void ReleaseWaitingList(Scene scene, LoadSceneMode mode)
{
for (var i = WaitingList.Count - 1; i >= 0; i--)
{
var (waiting, ex) = WaitingList[i];
if (waiting.gameObject.scene == scene)
{
WaitingList.RemoveAt(i);
waiting.Awake(); // Re-throw if parent not found.
}
}
}

static void ReleaseWaitingListFrom(LifetimeScope awakedParent)
{
if (WaitingList.Count < 0) return;

var type = awakedParent.GetType();

for (var i = WaitingList.Count - 1; i >= 0; i--)
{
var (waiting, ex) = WaitingList[i];
if (ex.ParentType == type)
{
waiting.parentReference.Object = awakedParent;
WaitingList.RemoveAt(i);
waiting.Awake();
}
}
}
}
}

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

122 changes: 122 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.Dispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Collections.Generic;

namespace VContainer.Unity
{
public partial class LifetimeScope
{
void DispatchEntryPoints()
{
PlayerLoopHelper.Initialize();

EntryPointExceptionHandler exceptionHandler = null;
try
{
exceptionHandler = Container.Resolve<EntryPointExceptionHandler>();
}
catch (VContainerException ex) when (ex.InvalidType == typeof(EntryPointExceptionHandler))
{
}

var initializables = Container.Resolve<IReadOnlyList<IInitializable>>();
if (initializables.Count > 0)
{
var loopItem = new InitializationLoopItem(initializables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Initialization, loopItem);
}

var postInitializables = Container.Resolve<IReadOnlyList<IPostInitializable>>();
if (postInitializables.Count > 0)
{
var loopItem = new PostInitializationLoopItem(postInitializables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostInitialization, loopItem);
}

var startables = Container.Resolve<IReadOnlyList<IStartable>>();
if (startables.Count > 0)
{
var loopItem = new StartableLoopItem(startables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Startup, loopItem);
}

var postStartables = Container.Resolve<IReadOnlyList<IPostStartable>>();
if (postStartables.Count > 0)
{
var loopItem = new PostStartableLoopItem(postStartables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostStartup, loopItem);
}

var fixedTickables = Container.Resolve<IReadOnlyList<IFixedTickable>>();
if (fixedTickables.Count > 0)
{
var loopItem = new FixedTickableLoopItem(fixedTickables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.FixedUpdate, loopItem);
}

var postFixedTickables = Container.Resolve<IReadOnlyList<IPostFixedTickable>>();
if (postFixedTickables.Count > 0)
{
var loopItem = new PostFixedTickableLoopItem(postFixedTickables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostFixedUpdate, loopItem);
}

var tickables = Container.Resolve<IReadOnlyList<ITickable>>();
if (tickables.Count > 0)
{
var loopItem = new TickableLoopItem(tickables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.Update, loopItem);
}

var postTickables = Container.Resolve<IReadOnlyList<IPostTickable>>();
if (postTickables.Count > 0)
{
var loopItem = new PostTickableLoopItem(postTickables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostUpdate, loopItem);
}

var lateTickables = Container.Resolve<IReadOnlyList<ILateTickable>>();
if (lateTickables.Count > 0)
{
var loopItem = new LateTickableLoopItem(lateTickables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.LateUpdate, loopItem);
}

var postLateTickables = Container.Resolve<IReadOnlyList<IPostLateTickable>>();
if (postLateTickables.Count > 0)
{
var loopItem = new PostLateTickableLoopItem(postLateTickables, exceptionHandler);
disposable.Add(loopItem);
PlayerLoopHelper.Dispatch(PlayerLoopTiming.PostLateUpdate, loopItem);
}

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

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

var worldHelpers = Container.Resolve<IEnumerable<WorldConfigurationHelper>>();
foreach (var x in worldHelpers)
{
x.SortSystems();
}
#endif
}

}
}

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

Loading

1 comment on commit 21c04ac

@vercel
Copy link

@vercel vercel bot commented on 21c04ac Feb 9, 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.