Skip to content

Commit

Permalink
FIx
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 25, 2023
1 parent a7b2d0f commit eca633f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
15 changes: 10 additions & 5 deletions VContainer/Assets/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,19 +571,24 @@ public void Inject()
public void OnContainerDisposeCallback()
{
NoDependencyServiceA resolvedJustBeforeDispose = null;

NoDependencyServiceB resolvedJustBeforeDispose2 = null;

var builder = new ContainerBuilder();

builder.Register<NoDependencyServiceA>(Lifetime.Scoped);
builder.Register<NoDependencyServiceB>(Lifetime.Scoped);
builder.RegisterDisposeCallback(resolver => resolvedJustBeforeDispose = resolver.Resolve<NoDependencyServiceA>());
builder.RegisterDisposeCallback(resolver => resolvedJustBeforeDispose2 = resolver.Resolve<NoDependencyServiceB>());

var container = builder.Build();

Assert.That(resolvedJustBeforeDispose, Is.Null);

Assert.That(resolvedJustBeforeDispose2, Is.Null);

container.Dispose();

Assert.That(resolvedJustBeforeDispose, Is.Not.Null);
Assert.That(resolvedJustBeforeDispose2, Is.Not.Null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using VContainer.Internal;

Expand Down Expand Up @@ -147,7 +148,8 @@ public static RegistrationBuilder RegisterFactory<TParam1, TParam2, TParam3, TPa

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

[Obsolete("IObjectResolver is registered by default. This method does nothing.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

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

0 comments on commit eca633f

Please sign in to comment.