Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Apr 16, 2024
1 parent 905e99a commit a488faa
Showing 1 changed file with 3 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Reflection;
using System.Reflection.Metadata;
using Microsoft.AspNetCore.Components.Endpoints;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Http;

[assembly: MetadataUpdateHandler(typeof(RazorComponentsEndpointHttpContextExtensions.MetadataUpdateHandler))]

namespace Microsoft.AspNetCore.Components.Routing;

/// <summary>
/// Extensions to <see cref="HttpContext"/> for Razor component applications.
/// </summary>
public static class RazorComponentsEndpointHttpContextExtensions
{
private static readonly ConcurrentDictionary<Type, bool> AllowsInteractiveRoutingCache = new();

/// <summary>
/// Determines whether the current endpoint is a Razor component that can be reached through
/// interactive routing. This is true for all page components except if they declare the
Expand All @@ -28,20 +20,8 @@ public static class RazorComponentsEndpointHttpContextExtensions
/// <returns>True if the current endpoint is a Razor component that does not declare <see cref="AllowInteractiveRoutingAttribute"/> with value <see langword="false"/>.</returns>
public static bool AllowsInteractiveRouting(this HttpContext? context)
{
var pageType = context?.GetEndpoint()?.Metadata.GetMetadata<ComponentTypeMetadata>()?.Type;

return pageType is not null
&& AllowsInteractiveRoutingCache.GetOrAdd(
pageType,
pageType.GetCustomAttribute<AllowInteractiveRoutingAttribute>() is not { Allow: false });
}

internal static class MetadataUpdateHandler
{
/// <summary>
/// Invoked as part of <see cref="MetadataUpdateHandlerAttribute" /> contract for hot reload.
/// </summary>
public static void ClearCache(Type[]? _)
=> AllowsInteractiveRoutingCache.Clear();
var metadata = context?.GetEndpoint()?.Metadata;
return metadata?.GetMetadata<ComponentTypeMetadata>() is not null
&& metadata.GetMetadata<AllowInteractiveRoutingAttribute>() is not { Allow: false };
}
}

0 comments on commit a488faa

Please sign in to comment.