Don't try to unsubscribe from fixed cascading parameters. Fixes #11653

This commit is contained in:
Steve Sanderson 2019-07-03 16:19:10 +01:00
parent de91125593
commit 1b6f721648
2 changed files with 28 additions and 10 deletions

View File

@ -165,7 +165,11 @@ namespace Microsoft.AspNetCore.Components.Rendering
var numCascadingParameters = _cascadingParameters.Count;
for (var i = 0; i < numCascadingParameters; i++)
{
_cascadingParameters[i].ValueSupplier.Unsubscribe(this);
var supplier = _cascadingParameters[i].ValueSupplier;
if (!supplier.CurrentValueIsFixed)
{
supplier.Unsubscribe(this);
}
}
}
}

View File

@ -188,16 +188,25 @@ namespace Microsoft.AspNetCore.Components.Test
var renderer = new TestRenderer();
var component = new TestComponent(builder =>
{
builder.OpenComponent<CascadingValue<string>>(0);
builder.AddAttribute(1, "Value", providedValue);
builder.AddAttribute(2, RenderTreeBuilder.ChildContent, new RenderFragment(childBuilder =>
// At the outer level, have an unrelated fixed cascading value to show we can deal with combining both types
builder.OpenComponent<CascadingValue<int>>(0);
builder.AddAttribute(1, "Value", 123);
builder.AddAttribute(2, "IsFixed", true);
builder.AddAttribute(3, RenderTreeBuilder.ChildContent, new RenderFragment(builder2 =>
{
if (displayNestedComponent)
// Then also have a non-fixed cascading value so we can show that unsubscription works
builder2.OpenComponent<CascadingValue<string>>(0);
builder2.AddAttribute(1, "Value", providedValue);
builder2.AddAttribute(2, RenderTreeBuilder.ChildContent, new RenderFragment(builder3 =>
{
childBuilder.OpenComponent<CascadingParameterConsumerComponent<string>>(0);
childBuilder.AddAttribute(1, "RegularParameter", "Goodbye");
childBuilder.CloseComponent();
}
if (displayNestedComponent)
{
builder3.OpenComponent<SecondCascadingParameterConsumerComponent<string, int>>(0);
builder3.AddAttribute(1, "RegularParameter", "Goodbye");
builder3.CloseComponent();
}
}));
builder2.CloseComponent();
}));
builder.CloseComponent();
});
@ -219,7 +228,7 @@ namespace Microsoft.AspNetCore.Components.Test
Assert.Equal(2, renderer.Batches.Count);
var secondBatch = renderer.Batches[1];
Assert.Equal(1, nestedComponent.NumRenders);
Assert.Equal(2, secondBatch.DiffsByComponentId.Count); // Root + CascadingValue, but not nested one
Assert.Equal(3, secondBatch.DiffsByComponentId.Count); // Root + CascadingValue + CascadingValue, but not nested component
// We *did* send updated params during the first render where it was removed,
// because the params are sent before the disposal logic runs. We could avoid
@ -386,5 +395,10 @@ namespace Microsoft.AspNetCore.Components.Test
builder.AddContent(0, $"CascadingParameter={CascadingParameter}; RegularParameter={RegularParameter}");
}
}
class SecondCascadingParameterConsumerComponent<T1, T2> : CascadingParameterConsumerComponent<T1>
{
[CascadingParameter] T2 SecondCascadingParameter { get; set; }
}
}
}