Skip to content

Commit 39dccd7

Browse files
Use MetadataUpdater.IsSupported to determine if application supports … (#34022)
* Use MetadataUpdater.IsSupported to determine if application supports hot reload Fixes #33477 * Update src/Components/Components/src/HotReload/HotReloadManager.cs Co-authored-by: Safia Abdalla <[email protected]> Co-authored-by: Safia Abdalla <[email protected]>
1 parent 885cbe3 commit 39dccd7

File tree

22 files changed

+55
-69
lines changed

22 files changed

+55
-69
lines changed

src/Components/Components/src/ComponentBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected void StateHasChanged()
102102
return;
103103
}
104104

105-
if (_hasNeverRendered || ShouldRender() || _renderHandle.IsHotReloading)
105+
if (_hasNeverRendered || ShouldRender() || _renderHandle.IsRenderingOnMetadataUpdate)
106106
{
107107
_hasPendingQueuedRender = true;
108108

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Reflection.Metadata;
5+
6+
namespace Microsoft.AspNetCore.Components.HotReload
7+
{
8+
internal sealed class TestableMetadataUpdate
9+
{
10+
public static bool TestIsSupported { private get; set; }
11+
12+
/// <summary>
13+
/// A proxy for <see cref="MetadataUpdater.IsSupported" /> that is testable.
14+
/// </summary>
15+
public static bool IsSupported => MetadataUpdater.IsSupported || TestIsSupported;
16+
}
17+
}

src/Components/Components/src/Microsoft.AspNetCore.Components.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<ItemGroup>
1414
<Compile Include="$(ComponentsSharedSourceRoot)src\ArrayBuilder.cs" LinkBase="RenderTree" />
1515
<Compile Include="$(ComponentsSharedSourceRoot)src\JsonSerializerOptionsProvider.cs" />
16-
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadFeature.cs" LinkBase="HotReload" />
1716
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
1817
<Compile Include="$(SharedSourceRoot)QueryStringEnumerable.cs" LinkBase="Shared" />
1918
</ItemGroup>

src/Components/Components/src/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
14
using System.Runtime.CompilerServices;
25

36
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Blazor.Build.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

src/Components/Components/src/Properties/ILLink.Substitutions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<linker>
22
<assembly fullname="Microsoft.AspNetCore.Components">
3-
<type fullname="Microsoft.AspNetCore.Components.HotReload.HotReloadFeature" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false">
3+
<type fullname="Microsoft.AspNetCore.Components.HotReload.TestableMetadataUpdate" feature="System.Reflection.Metadata.MetadataUpdater.IsSupported" featurevalue="false">
44
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
55
</type>
66
</assembly>

src/Components/Components/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Microsoft.AspNetCore.Components.NavigationOptions.ForceLoad.init -> void
4040
Microsoft.AspNetCore.Components.NavigationOptions.NavigationOptions() -> void
4141
Microsoft.AspNetCore.Components.NavigationOptions.ReplaceHistoryEntry.get -> bool
4242
Microsoft.AspNetCore.Components.NavigationOptions.ReplaceHistoryEntry.init -> void
43-
Microsoft.AspNetCore.Components.RenderHandle.IsHotReloading.get -> bool
43+
Microsoft.AspNetCore.Components.RenderHandle.IsRenderingOnMetadataUpdate.get -> bool
4444
Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.Dispose() -> void
4545
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.get -> bool
4646
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.set -> void

src/Components/Components/src/RenderHandle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public Dispatcher Dispatcher
4545
public bool IsInitialized => _renderer is not null;
4646

4747
/// <summary>
48-
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a hot-reload change.
48+
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a metadata update (hot-reload) change.
4949
/// </summary>
50-
public bool IsHotReloading => HotReloadFeature.IsSupported && (_renderer?.IsHotReloading ?? false);
50+
public bool IsRenderingOnMetadataUpdate => TestableMetadataUpdate.IsSupported && (_renderer?.IsRenderingOnMetadataUpdate ?? false);
5151

5252
/// <summary>
5353
/// Notifies the renderer that the component should be rendered.

src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ private static void UpdateRetainedChildComponent(
542542
var oldParameters = new ParameterView(ParameterViewLifetime.Unbound, oldTree, oldComponentIndex);
543543
var newParametersLifetime = new ParameterViewLifetime(diffContext.BatchBuilder);
544544
var newParameters = new ParameterView(newParametersLifetime, newTree, newComponentIndex);
545-
if (!newParameters.DefinitelyEquals(oldParameters) || (HotReloadFeature.IsSupported && diffContext.Renderer.IsHotReloading))
545+
if (!newParameters.DefinitelyEquals(oldParameters) || (TestableMetadataUpdate.IsSupported && diffContext.Renderer.IsRenderingOnMetadataUpdate))
546546
{
547547
componentState.SetDirectParameters(newParameters);
548548
}

src/Components/Components/src/RenderTree/Renderer.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public Renderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory,
9797
_logger = loggerFactory.CreateLogger<Renderer>();
9898
_componentFactory = new ComponentFactory(componentActivator);
9999

100-
if (HotReloadFeature.IsSupported)
100+
if (TestableMetadataUpdate.IsSupported)
101101
{
102102
HotReloadManager.OnDeltaApplied += RenderRootComponentsOnHotReload;
103103
}
@@ -121,9 +121,9 @@ private static IComponentActivator GetComponentActivatorOrDefault(IServiceProvid
121121
protected internal ElementReferenceContext? ElementReferenceContext { get; protected set; }
122122

123123
/// <summary>
124-
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a hot-reload change.
124+
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a (metadata update) hot-reload change.
125125
/// </summary>
126-
internal bool IsHotReloading { get; private set; }
126+
internal bool IsRenderingOnMetadataUpdate { get; private set; }
127127

128128
private async void RenderRootComponentsOnHotReload()
129129
{
@@ -139,7 +139,7 @@ await Dispatcher.InvokeAsync(() =>
139139
return;
140140
}
141141

142-
IsHotReloading = true;
142+
IsRenderingOnMetadataUpdate = true;
143143
try
144144
{
145145
foreach (var (componentState, initialParameters) in _rootComponents)
@@ -149,7 +149,7 @@ await Dispatcher.InvokeAsync(() =>
149149
}
150150
finally
151151
{
152-
IsHotReloading = false;
152+
IsRenderingOnMetadataUpdate = false;
153153
}
154154
});
155155
}
@@ -227,7 +227,7 @@ protected async Task RenderRootComponentAsync(int componentId, ParameterView ini
227227
// During the asynchronous rendering process we want to wait up until all components have
228228
// finished rendering so that we can produce the complete output.
229229
var componentState = GetRequiredComponentState(componentId);
230-
if (HotReloadFeature.IsSupported)
230+
if (TestableMetadataUpdate.IsSupported)
231231
{
232232
// when we're doing hot-reload, stash away the parameters used while rendering root components.
233233
// We'll use this to trigger re-renders on hot reload updates.
@@ -998,7 +998,7 @@ public void Dispose()
998998
/// <inheritdoc />
999999
public async ValueTask DisposeAsync()
10001000
{
1001-
if (HotReloadFeature.IsSupported)
1001+
if (TestableMetadataUpdate.IsSupported)
10021002
{
10031003
HotReloadManager.OnDeltaApplied -= RenderRootComponentsOnHotReload;
10041004
}

src/Components/Components/src/Routing/Router.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Reflection;
9+
using System.Reflection.Metadata;
910
using System.Runtime.ExceptionServices;
1011
using System.Threading;
1112
using System.Threading.Tasks;
@@ -99,7 +100,7 @@ public void Attach(RenderHandle renderHandle)
99100
_locationAbsolute = NavigationManager.Uri;
100101
NavigationManager.LocationChanged += OnLocationChanged;
101102

102-
if (HotReloadFeature.IsSupported)
103+
if (TestableMetadataUpdate.IsSupported)
103104
{
104105
HotReloadManager.OnDeltaApplied += ClearRouteCaches;
105106
}
@@ -143,7 +144,7 @@ public async Task SetParametersAsync(ParameterView parameters)
143144
public void Dispose()
144145
{
145146
NavigationManager.LocationChanged -= OnLocationChanged;
146-
if (HotReloadFeature.IsSupported)
147+
if (TestableMetadataUpdate.IsSupported)
147148
{
148149
HotReloadManager.OnDeltaApplied -= ClearRouteCaches;
149150
}

0 commit comments

Comments
 (0)