Skip to content

Commit

Permalink
Fix bug where DisposeCallback would not close to parent and child
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Nov 29, 2024
1 parent 96a916f commit 6cb626c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions VContainer/Assets/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,31 @@ public void OnContainerDisposeCallback()
Assert.That(resolvedJustBeforeDispose2, Is.Not.Null);
}

[Test]
public void OnContainerDisposeCallback_ParentChild()
{
var parentDisposeCalled = false;
var childDisposeCalled = false;

var builder = new ContainerBuilder();
builder.Register<NoDependencyServiceA>(Lifetime.Scoped);

builder.RegisterDisposeCallback(x => parentDisposeCalled = true);

var container = builder.Build();

var childContainer = container.CreateScope(childBuilder =>
{
childBuilder.Register<NoDependencyServiceB>(Lifetime.Scoped);
childBuilder.RegisterDisposeCallback(x => childDisposeCalled = true);
});

childContainer.Dispose();

Assert.That(childDisposeCalled, Is.True);
Assert.That(parentDisposeCalled, Is.False);
}

[Test]
public void TryResolveTransient()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public static RegistrationBuilder RegisterFactory<TParam1, TParam2, TParam3, TPa

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

[Obsolete("IObjectResolver is registered by default. This method does nothing.")]
Expand Down

0 comments on commit 6cb626c

Please sign in to comment.