Skip to content

Commit

Permalink
Add RegisterDisposeCallback and refine callback impl
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 23, 2023
1 parent a057311 commit 8b00eaa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
14 changes: 3 additions & 11 deletions VContainer/Assets/VContainer/Runtime/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public DiagnosticsCollector Diagnostics
}

readonly List<RegistrationBuilder> registrationBuilders = new List<RegistrationBuilder>();
List<Action<IObjectResolver>> buildCallbacks;
Action<IObjectResolver> buildCallback;
DiagnosticsCollector diagnostics;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -83,9 +83,7 @@ public T Register<T>(T registrationBuilder) where T : RegistrationBuilder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RegisterBuildCallback(Action<IObjectResolver> callback)
{
if (buildCallbacks == null)
buildCallbacks = new List<Action<IObjectResolver>>();
buildCallbacks.Add(callback);
buildCallback += callback;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -149,13 +147,7 @@ protected Registry BuildRegistry()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void EmitCallbacks(IObjectResolver container)
{
if (buildCallbacks == null) return;

foreach (var callback in buildCallbacks)
{
callback.Invoke(container);
}

buildCallback?.Invoke(container);
Diagnostics?.NotifyContainerBuilt(container);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public static RegistrationBuilder RegisterFactory<TParam1, TParam2, TParam3, TPa
Lifetime lifetime)
=> builder.Register(new FuncRegistrationBuilder(factoryFactory, typeof(Func<TParam1, TParam2, TParam3, TParam4, T>), lifetime));

public static void RegisterDisposeCallback(this IContainerBuilder builder, Action<IDisposable> callback)
{
builder.Register(container => new ActionDisposable(callback, container), Lifetime.Scoped);
}

[Obsolete("IObjectResolver is registered by default. This method does nothing.")]
public static void RegisterContainer(this IContainerBuilder builder)
{
Expand Down
21 changes: 21 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Internal/ActionDisposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace VContainer.Internal
{
struct ActionDisposable : IDisposable
{
readonly Action<IObjectResolver> callback;
readonly IObjectResolver container;

public ActionDisposable(Action<IObjectResolver> callback, IObjectResolver container)
{
this.callback = callback;
this.container = container;
}

public void Dispose()
{
callback.Invoke(container);
}
}
}

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

0 comments on commit 8b00eaa

Please sign in to comment.