Skip to content

Commit 43ebb3b

Browse files
authored
Remove IReceiveHotReloadContext and HotReloadContext (#31281)
* Remove IReceiveHotReloadContext and HotReloadContext React to API review feedback. Substitute IReceiveHotReloadContext and HotReloadContext with RenderHandle.IsHotReloading * Fixup * Update src/Components/Components/src/RenderHandle.cs * Update src/Components/Components/src/Properties/ILLink.Substitutions.xml * Update src/Components/Components/src/Properties/ILLink.Substitutions.xml
1 parent b00ae1b commit 43ebb3b

File tree

8 files changed

+24
-84
lines changed

8 files changed

+24
-84
lines changed

src/Components/Components/src/ComponentBase.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Threading.Tasks;
6-
using Microsoft.AspNetCore.Components.HotReload;
76
using Microsoft.AspNetCore.Components.Rendering;
87

98
namespace Microsoft.AspNetCore.Components
@@ -22,15 +21,14 @@ namespace Microsoft.AspNetCore.Components
2221
/// Optional base class for components. Alternatively, components may
2322
/// implement <see cref="IComponent"/> directly.
2423
/// </summary>
25-
public abstract class ComponentBase : IComponent, IHandleEvent, IHandleAfterRender, IReceiveHotReloadContext
24+
public abstract class ComponentBase : IComponent, IHandleEvent, IHandleAfterRender
2625
{
2726
private readonly RenderFragment _renderFragment;
2827
private RenderHandle _renderHandle;
2928
private bool _initialized;
3029
private bool _hasNeverRendered = true;
3130
private bool _hasPendingQueuedRender;
3231
private bool _hasCalledOnAfterRender;
33-
private HotReloadContext? _hotReloadContext;
3432

3533
/// <summary>
3634
/// Constructs an instance of <see cref="ComponentBase"/>.
@@ -104,7 +102,7 @@ protected void StateHasChanged()
104102
return;
105103
}
106104

107-
if (_hasNeverRendered || ShouldRender() || (_hotReloadContext?.IsHotReloading ?? false))
105+
if (_hasNeverRendered || ShouldRender() || _renderHandle.IsHotReloading)
108106
{
109107
_hasPendingQueuedRender = true;
110108

@@ -331,10 +329,5 @@ Task IHandleAfterRender.OnAfterRenderAsync()
331329
// have to use "async void" and do their own exception handling in
332330
// the case where they want to start an async task.
333331
}
334-
335-
void IReceiveHotReloadContext.Receive(HotReloadContext context)
336-
{
337-
_hotReloadContext = context;
338-
}
339332
}
340333
}

src/Components/Components/src/HotReload/HotReloadContext.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Components/Components/src/HotReload/IReceiveHotReloadContext.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
<assembly fullname="Microsoft.AspNetCore.Components" >
33
<!-- HotReload will not be available in a trimmed app. We'll attempt to aggressively remove all references to it. -->
44
<type fullname="Microsoft.AspNetCore.Components.RenderTree.Renderer">
5+
<!-- Renderer.IsHotReloading will always return false in a trimmed app. -->
6+
<method signature="System.Boolean get_IsHotReloading()" body="stub" value="false" />
57
<method signature="System.Void RenderRootComponentsOnHotReload()" body="remove" />
68
<method signature="System.Void InitializeHotReload(System.IServiceProvider)" body="stub" />
7-
<method signature="System.Void InstatiateComponentForHotReload(Microsoft.AspNetCore.Components.IComponent)" body="stub" />
89
<method signature="System.Void CaptureRootComponentForHotReload(Microsoft.AspNetCore.Components.ParameterView,Microsoft.AspNetCore.Components.Rendering.ComponentState)" body="stub" />
910
<method signature="System.Void DisposeForHotReload()" body="stub" />
1011
</type>
11-
<type fullname="Microsoft.AspNetCore.Components.HotReload.HotReloadContext">
12-
<method signature="System.Boolean get_IsHotReloading()" body="stub" value="false" />
13-
<method signature="System.Void set_IsHotReloading(System.Boolean)" body="remove" />
14-
</type>
15-
<type fullname="Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder">
16-
<method signature="System.Boolean IsHotReloading(Microsoft.AspNetCore.Components.RenderTree.Renderer)" body="stub" value="false" />
12+
<!-- Avoid any overhead in RenderHandle.IsHotReloading by aggressively trimming it -->
13+
<type fullname="Microsoft.AspNetCore.Components.RenderHandle">
14+
<method signature="System.Boolean get_IsHotReloading()" body="stub" value="false" />
1715
</type>
1816
</assembly>
1917
</linker>

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ Microsoft.AspNetCore.Components.ComponentApplicationState.PersistAsJson<TValue>(
88
Microsoft.AspNetCore.Components.ComponentApplicationState.PersistState(string! key, byte[]! value) -> void
99
Microsoft.AspNetCore.Components.ComponentApplicationState.TryTakeAsJson<TValue>(string! key, out TValue? instance) -> bool
1010
Microsoft.AspNetCore.Components.ComponentApplicationState.TryTakePersistedState(string! key, out byte[]? value) -> bool
11-
Microsoft.AspNetCore.Components.HotReload.HotReloadContext
12-
Microsoft.AspNetCore.Components.HotReload.HotReloadContext.HotReloadContext() -> void
13-
Microsoft.AspNetCore.Components.HotReload.HotReloadContext.IsHotReloading.get -> bool
14-
Microsoft.AspNetCore.Components.HotReload.IReceiveHotReloadContext
15-
Microsoft.AspNetCore.Components.HotReload.IReceiveHotReloadContext.Receive(Microsoft.AspNetCore.Components.HotReload.HotReloadContext! context) -> void
1611
Microsoft.AspNetCore.Components.Lifetime.ComponentApplicationLifetime
1712
Microsoft.AspNetCore.Components.Lifetime.ComponentApplicationLifetime.ComponentApplicationLifetime(Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Components.Lifetime.ComponentApplicationLifetime!>! logger) -> void
1813
Microsoft.AspNetCore.Components.Lifetime.ComponentApplicationLifetime.PersistStateAsync(Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore! store, Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer) -> System.Threading.Tasks.Task!
@@ -21,6 +16,7 @@ Microsoft.AspNetCore.Components.Lifetime.ComponentApplicationLifetime.State.get
2116
Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore
2217
Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore.GetPersistedStateAsync() -> System.Threading.Tasks.Task<System.Collections.Generic.IDictionary<string!, byte[]!>!>!
2318
Microsoft.AspNetCore.Components.Lifetime.IComponentApplicationStateStore.PersistStateAsync(System.Collections.Generic.IReadOnlyDictionary<string!, byte[]!>! state) -> System.Threading.Tasks.Task!
19+
Microsoft.AspNetCore.Components.RenderHandle.IsHotReloading.get -> bool
2420
Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.Dispose() -> void
2521
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.get -> bool
2622
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.set -> void

src/Components/Components/src/RenderHandle.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Components
1212
/// </summary>
1313
public readonly struct RenderHandle
1414
{
15-
private readonly Renderer _renderer;
15+
private readonly Renderer? _renderer;
1616
private readonly int _componentId;
1717

1818
internal RenderHandle(Renderer renderer, int componentId)
@@ -22,7 +22,7 @@ internal RenderHandle(Renderer renderer, int componentId)
2222
}
2323

2424
/// <summary>
25-
/// Gets the <see cref="Microsoft.AspNetCore.Components.Dispatcher" /> associated with the component.
25+
/// Gets the <see cref="Components.Dispatcher" /> associated with the component.
2626
/// </summary>
2727
public Dispatcher Dispatcher
2828
{
@@ -41,8 +41,12 @@ public Dispatcher Dispatcher
4141
/// Gets a value that indicates whether the <see cref="RenderHandle"/> has been
4242
/// initialized and is ready to use.
4343
/// </summary>
44-
public bool IsInitialized
45-
=> _renderer != null;
44+
public bool IsInitialized => _renderer is not null;
45+
46+
/// <summary>
47+
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a hot-reload change.
48+
/// </summary>
49+
public bool IsHotReloading => _renderer?.IsHotReloading ?? false;
4650

4751
/// <summary>
4852
/// Notifies the renderer that the component should be rendered.

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,17 +541,12 @@ private static void UpdateRetainedChildComponent(
541541
var oldParameters = new ParameterView(ParameterViewLifetime.Unbound, oldTree, oldComponentIndex);
542542
var newParametersLifetime = new ParameterViewLifetime(diffContext.BatchBuilder);
543543
var newParameters = new ParameterView(newParametersLifetime, newTree, newComponentIndex);
544-
if (!newParameters.DefinitelyEquals(oldParameters) || IsHotReloading(diffContext.Renderer))
544+
if (!newParameters.DefinitelyEquals(oldParameters) || diffContext.Renderer.IsHotReloading)
545545
{
546546
componentState.SetDirectParameters(newParameters);
547547
}
548548
}
549549

550-
/// <remarks>
551-
/// Intentionally authored as a separate method so we can trim this code.
552-
/// </remarks>
553-
private static bool IsHotReloading(Renderer renderer) => renderer.HotReloadContext.IsHotReloading;
554-
555550
private static int NextSiblingIndex(in RenderTreeFrame frame, int frameIndex)
556551
{
557552
switch (frame.FrameTypeField)

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public abstract partial class Renderer : IDisposable, IAsyncDisposable
3333
private readonly Dictionary<ulong, ulong> _eventHandlerIdReplacements = new Dictionary<ulong, ulong>();
3434
private readonly ILogger<Renderer> _logger;
3535
private readonly ComponentFactory _componentFactory;
36-
private HotReloadContext _hotReloadContext;
3736
private HotReloadEnvironment? _hotReloadEnvironment;
3837
private List<(ComponentState, ParameterView)>? _rootComponents;
3938

@@ -102,8 +101,6 @@ public Renderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory,
102101

103102
private void InitializeHotReload(IServiceProvider serviceProvider)
104103
{
105-
_hotReloadContext = new();
106-
107104
// HotReloadEnvironment is a test-specific feature and may not be available in a running app. We'll fallback to the default instance
108105
// if the test fixture does not provide one.
109106
_hotReloadEnvironment = serviceProvider.GetService<HotReloadEnvironment>() ?? HotReloadEnvironment.Instance;
@@ -131,7 +128,10 @@ private static IComponentActivator GetComponentActivatorOrDefault(IServiceProvid
131128
/// </summary>
132129
protected internal ElementReferenceContext? ElementReferenceContext { get; protected set; }
133130

134-
internal HotReloadContext HotReloadContext => _hotReloadContext;
131+
/// <summary>
132+
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a hot-reload change.
133+
/// </summary>
134+
internal bool IsHotReloading { get; private set; }
135135

136136
private async void RenderRootComponentsOnHotReload()
137137
{
@@ -142,7 +142,7 @@ await Dispatcher.InvokeAsync(() =>
142142
return;
143143
}
144144

145-
HotReloadContext.IsHotReloading = true;
145+
IsHotReloading = true;
146146
try
147147
{
148148
foreach (var (componentState, initialParameters) in _rootComponents)
@@ -152,7 +152,7 @@ await Dispatcher.InvokeAsync(() =>
152152
}
153153
finally
154154
{
155-
HotReloadContext.IsHotReloading = false;
155+
IsHotReloading = false;
156156
}
157157
});
158158
}
@@ -164,20 +164,7 @@ await Dispatcher.InvokeAsync(() =>
164164
/// <returns>The component instance.</returns>
165165
protected IComponent InstantiateComponent([DynamicallyAccessedMembers(Component)] Type componentType)
166166
{
167-
var component = _componentFactory.InstantiateComponent(_serviceProvider, componentType);
168-
InstatiateComponentForHotReload(component);
169-
return component;
170-
}
171-
172-
/// <remarks>
173-
/// Intentionally authored as a separate method call so we can trim this code.
174-
/// </remarks>
175-
private void InstatiateComponentForHotReload(IComponent component)
176-
{
177-
if (_hotReloadEnvironment is not null && _hotReloadEnvironment.IsHotReloadEnabled && component is IReceiveHotReloadContext receiveHotReloadContext)
178-
{
179-
receiveHotReloadContext.Receive(HotReloadContext);
180-
}
167+
return _componentFactory.InstantiateComponent(_serviceProvider, componentType);
181168
}
182169

183170
/// <summary>

0 commit comments

Comments
 (0)