Skip to content

Commit

Permalink
Use MetadataUpdater.IsSupported to determine if application supports …
Browse files Browse the repository at this point in the history
…hot reload

Fixes #33477
  • Loading branch information
pranavkm committed Jul 7, 2021
1 parent 51dfc46 commit 798af0f
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/Components/Components/src/ComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void StateHasChanged()
return;
}

if (_hasNeverRendered || ShouldRender() || _renderHandle.IsHotReloading)
if (_hasNeverRendered || ShouldRender() || _renderHandle.IsMetadataUpdating)
{
_hasPendingQueuedRender = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Components.HotReload
{
internal static class HotReloadManager
{
internal static event Action? OnDeltaApplied;
internal static event Action? OnDeltaApplied;

public static void DeltaApplied()
{
Expand Down
17 changes: 17 additions & 0 deletions src/Components/Components/src/HotReload/TestableMetadataUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Reflection.Metadata;

namespace Microsoft.AspNetCore.Components.HotReload
{
internal sealed class TestableMetadataUpdate
{
public static bool TestIsSupported { private get; set; }

/// <summary>
/// A proxy for <see cref="MetadataUpdater.IsSupported" /> that is testable.
/// </summary>
public static bool IsSupported => MetadataUpdater.IsSupported || TestIsSupported;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<ItemGroup>
<Compile Include="$(ComponentsSharedSourceRoot)src\ArrayBuilder.cs" LinkBase="RenderTree" />
<Compile Include="$(ComponentsSharedSourceRoot)src\JsonSerializerOptionsProvider.cs" />
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadFeature.cs" LinkBase="HotReload" />
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)QueryStringEnumerable.cs" LinkBase="Shared" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Components/src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Blazor.Build.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<linker>
<assembly fullname="Microsoft.AspNetCore.Components">
<type fullname="Microsoft.AspNetCore.Components.HotReload.HotReloadFeature" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false">
<type fullname="Microsoft.AspNetCore.Components.HotReload.TestableMetadataUpdate" feature="System.Reflection.Metadata.MetadataUpdater.IsSupported" featurevalue="false">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
</assembly>
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Microsoft.AspNetCore.Components.NavigationOptions.ForceLoad.init -> void
Microsoft.AspNetCore.Components.NavigationOptions.NavigationOptions() -> void
Microsoft.AspNetCore.Components.NavigationOptions.ReplaceHistoryEntry.get -> bool
Microsoft.AspNetCore.Components.NavigationOptions.ReplaceHistoryEntry.init -> void
Microsoft.AspNetCore.Components.RenderHandle.IsHotReloading.get -> bool
Microsoft.AspNetCore.Components.RenderHandle.IsMetadataUpdating.get -> bool
Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.Dispose() -> void
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.get -> bool
Microsoft.AspNetCore.Components.Routing.Router.PreferExactMatches.set -> void
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Components/src/RenderHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Dispatcher Dispatcher
/// <summary>
/// Gets a value that determines if the <see cref="Renderer"/> is triggering a render in response to a hot-reload change.
/// </summary>
public bool IsHotReloading => HotReloadFeature.IsSupported && (_renderer?.IsHotReloading ?? false);
public bool IsMetadataUpdating => TestableMetadataUpdate.IsSupported && (_renderer?.IsMetadataUpdating ?? false);

/// <summary>
/// Notifies the renderer that the component should be rendered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ private static void UpdateRetainedChildComponent(
var oldParameters = new ParameterView(ParameterViewLifetime.Unbound, oldTree, oldComponentIndex);
var newParametersLifetime = new ParameterViewLifetime(diffContext.BatchBuilder);
var newParameters = new ParameterView(newParametersLifetime, newTree, newComponentIndex);
if (!newParameters.DefinitelyEquals(oldParameters) || (HotReloadFeature.IsSupported && diffContext.Renderer.IsHotReloading))
if (!newParameters.DefinitelyEquals(oldParameters) || (TestableMetadataUpdate.IsSupported && diffContext.Renderer.IsMetadataUpdating))
{
componentState.SetDirectParameters(newParameters);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Components/Components/src/RenderTree/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Renderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory,
_logger = loggerFactory.CreateLogger<Renderer>();
_componentFactory = new ComponentFactory(componentActivator);

if (HotReloadFeature.IsSupported)
if (TestableMetadataUpdate.IsSupported)
{
HotReloadManager.OnDeltaApplied += RenderRootComponentsOnHotReload;
}
Expand All @@ -121,9 +121,9 @@ private static IComponentActivator GetComponentActivatorOrDefault(IServiceProvid
protected internal ElementReferenceContext? ElementReferenceContext { get; protected set; }

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

private async void RenderRootComponentsOnHotReload()
{
Expand All @@ -139,7 +139,7 @@ await Dispatcher.InvokeAsync(() =>
return;
}

IsHotReloading = true;
IsMetadataUpdating = true;
try
{
foreach (var (componentState, initialParameters) in _rootComponents)
Expand All @@ -149,7 +149,7 @@ await Dispatcher.InvokeAsync(() =>
}
finally
{
IsHotReloading = false;
IsMetadataUpdating = false;
}
});
}
Expand Down Expand Up @@ -227,7 +227,7 @@ protected async Task RenderRootComponentAsync(int componentId, ParameterView ini
// During the asynchronous rendering process we want to wait up until all components have
// finished rendering so that we can produce the complete output.
var componentState = GetRequiredComponentState(componentId);
if (HotReloadFeature.IsSupported)
if (TestableMetadataUpdate.IsSupported)
{
// when we're doing hot-reload, stash away the parameters used while rendering root components.
// We'll use this to trigger re-renders on hot reload updates.
Expand Down Expand Up @@ -998,7 +998,7 @@ public void Dispose()
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (HotReloadFeature.IsSupported)
if (TestableMetadataUpdate.IsSupported)
{
HotReloadManager.OnDeltaApplied -= RenderRootComponentsOnHotReload;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Components/Components/src/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Metadata;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void Attach(RenderHandle renderHandle)
_locationAbsolute = NavigationManager.Uri;
NavigationManager.LocationChanged += OnLocationChanged;

if (HotReloadFeature.IsSupported)
if (TestableMetadataUpdate.IsSupported)
{
HotReloadManager.OnDeltaApplied += ClearRouteCaches;
}
Expand Down Expand Up @@ -143,7 +144,7 @@ public async Task SetParametersAsync(ParameterView parameters)
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
if (HotReloadFeature.IsSupported)
if (TestableMetadataUpdate.IsSupported)
{
HotReloadManager.OnDeltaApplied -= ClearRouteCaches;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public DataAnnotationsEventSubscriptions(EditContext editContext)
_editContext.OnFieldChanged += OnFieldChanged;
_editContext.OnValidationRequested += OnValidationRequested;

if (HotReloadFeature.IsSupported)
if (MetadataUpdater.IsSupported)
{
OnClearCache += ClearCache;
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public void Dispose()
_editContext.OnValidationRequested -= OnValidationRequested;
_editContext.NotifyValidationStateChanged();

if (HotReloadFeature.IsSupported)
if (MetadataUpdater.IsSupported)
{
OnClearCache -= ClearCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@
<Trimmable>true</Trimmable>
</PropertyGroup>

<ItemGroup>
<None Remove="Properties\ILLink.Substitutions.xml" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Components" />
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadFeature.cs" LinkBase="HotReload" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Properties\ILLink.Substitutions.xml">
<LogicalName>ILLink.Substitutions.xml</LogicalName>
</EmbeddedResource>
</ItemGroup>

</Project>
7 changes: 0 additions & 7 deletions src/Components/Forms/src/Properties/ILLink.Substitutions.xml

This file was deleted.

16 changes: 0 additions & 16 deletions src/Components/Shared/src/HotReloadFeature.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.HotReload;
Expand Down Expand Up @@ -138,7 +139,7 @@ internal async Task RunAsyncCore(CancellationToken cancellationToken, WebAssembl

await manager.RestoreStateAsync(store);

if (HotReloadFeature.IsSupported)
if (MetadataUpdater.IsSupported)
{
await WebAssemblyHotReload.InitializeAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;

namespace Microsoft.Extensions.HotReload
{
Expand Down Expand Up @@ -196,7 +197,7 @@ public void ApplyDeltas(IReadOnlyList<UpdateDelta> deltas)
{
if (TryGetModuleId(assembly) is Guid moduleId && moduleId == item.ModuleId)
{
System.Reflection.Metadata.AssemblyExtensions.ApplyUpdate(assembly, item.MetadataDelta, item.ILDelta, ReadOnlySpan<byte>.Empty);
MetadataUpdater.ApplyUpdate(assembly, item.MetadataDelta, item.ILDelta, ReadOnlySpan<byte>.Empty);
}
}

Expand Down Expand Up @@ -227,7 +228,7 @@ public void ApplyDeltas(Assembly assembly, IReadOnlyList<UpdateDelta> deltas)

foreach (var item in deltas)
{
System.Reflection.Metadata.AssemblyExtensions.ApplyUpdate(assembly, item.MetadataDelta, item.ILDelta, ReadOnlySpan<byte>.Empty);
MetadataUpdater.ApplyUpdate(assembly, item.MetadataDelta, item.ILDelta, ReadOnlySpan<byte>.Empty);
}

_log("Deltas applied.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.HotReload
public static class WebAssemblyHotReload
{
private static HotReloadAgent? _hotReloadAgent;
private static UpdateDelta[] _updateDeltas = new[]
private static readonly UpdateDelta[] _updateDeltas = new[]
{
new UpdateDelta(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<Compile Include="$(SharedSourceRoot)Components\ComponentParameter.cs" Link="Prerendering/ComponentParameter.cs" />
<Compile Include="$(SharedSourceRoot)Components\PrerenderComponentApplicationStore.cs" />
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadFeature.cs" LinkBase="HotReload" />
</ItemGroup>

<ItemGroup>
Expand All @@ -53,10 +52,6 @@
<Content Include="build\$(DefaultNetCoreTargetFramework)\*.props" Pack="true" PackagePath="build\$(DefaultNetCoreTargetFramework)\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Properties\ILLink.Substitutions.xml" LogicalName="ILLink.Substitutions.xml" />
</ItemGroup>

<!-- blazor.webassembly.js should exist after Microsoft.AspNetCore.Components.Web.JS.npmproj builds. -->
<Target Name="_CheckBlazorServerJSPath" BeforeTargets="GenerateNuspec" DependsOnTargets="ResolveProjectReferences" Condition=" '$(IsPackable)' == 'true' ">
<Error Text="'$(BlazorWebAssemblyJSFile)' does not exist. Enable NodeJS to pack this project." Condition=" !EXISTS('$(BlazorWebAssemblyJSFile)') " />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Net.Http;
using System.Threading.Tasks;
using BasicTestApp.HotReload;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
Expand All @@ -11,7 +12,6 @@
using TestServer;
using Xunit;
using Xunit.Abstractions;
using System.Net.Http;

namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Security.Claims;
Expand Down
6 changes: 6 additions & 0 deletions src/Components/test/testassets/TestServer/HotReloadStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Globalization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -11,6 +12,11 @@ namespace TestServer
{
public class HotReloadStartup
{
public HotReloadStartup()
{
TestableMetadataUpdate.TestIsSupported = true;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Buffers;
using System.Linq;
using System.Reflection.Metadata;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
Expand Down Expand Up @@ -61,7 +62,7 @@ public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)
ConfigureDefaultServices(services);
AddMvcCoreServices(services);

if (environment?.IsDevelopment() ?? false)
if (MetadataUpdater.IsSupported)
{
services.TryAddEnumerable(
ServiceDescriptor.Singleton<IActionDescriptorChangeProvider, HotReloadService>());
Expand Down

0 comments on commit 798af0f

Please sign in to comment.