File: Builder\RazorComponentsEndpointConventionBuilder.cs
Web Access
Project: src\src\aspnetcore\src\Components\Endpoints\src\Microsoft.AspNetCore.Components.Endpoints.csproj (Microsoft.AspNetCore.Components.Endpoints)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Discovery;
using Microsoft.AspNetCore.Components.Endpoints;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace Microsoft.AspNetCore.Builder;

/// <summary>
/// Builds conventions that will be used for customization of <see cref="EndpointBuilder"/> instances.
/// </summary>
public sealed class RazorComponentsEndpointConventionBuilder : IEndpointConventionBuilder
{
    private readonly object _lock;
    private readonly RazorComponentDataSourceOptions _options;
    private readonly List<Action<EndpointBuilder>> _conventions;
    private readonly List<Action<EndpointBuilder>> _finallyConventions;
    private readonly List<Action<ComponentApplicationBuilder>> _componentApplicationBuilderActions;

    internal RazorComponentsEndpointConventionBuilder(
        object @lock,
        IEndpointRouteBuilder endpointRouteBuilder,
        RazorComponentDataSourceOptions options,
        List<Action<EndpointBuilder>> conventions,
        List<Action<EndpointBuilder>> finallyConventions,
        List<Action<ComponentApplicationBuilder>> componentApplicationBuilderActions)
    {
        _lock = @lock;
        EndpointRouteBuilder = endpointRouteBuilder;
        _options = options;
        _conventions = conventions;
        _finallyConventions = finallyConventions;
        _componentApplicationBuilderActions = componentApplicationBuilderActions;
    }

    internal List<Action<ComponentApplicationBuilder>> ComponentApplicationBuilderActions => _componentApplicationBuilderActions;

    internal string? ManifestPath { get => _options.ManifestPath; set => _options.ManifestPath = value; }

    internal bool ResourceCollectionConventionRegistered { get; set; }

    internal IEndpointRouteBuilder EndpointRouteBuilder { get; }

    internal event Action<RazorComponentEndpointUpdateContext>? BeforeCreateEndpoints;

    /// <inheritdoc/>
    public void Add(Action<EndpointBuilder> convention)
    {
        ArgumentNullException.ThrowIfNull(convention);

        // The lock is shared with the data source. We want to lock here
        // to avoid mutating this list while its read in the data source.
        lock (_lock)
        {
            _conventions.Add(convention);
        }
    }

    /// <inheritdoc/>
    public void Finally(Action<EndpointBuilder> finallyConvention)
    {
        // The lock is shared with the data source. We want to lock here
        // to avoid mutating this list while its read in the data source.
        lock (_lock)
        {
            _finallyConventions.Add(finallyConvention);
        }
    }

    internal void AddRenderMode(IComponentRenderMode renderMode)
    {
        _options.ConfiguredRenderModes.Add(renderMode);
    }

    internal void OnBeforeCreateEndpoints(RazorComponentEndpointUpdateContext endpointContext) =>
        BeforeCreateEndpoints?.Invoke(endpointContext);
}

internal class RazorComponentEndpointUpdateContext(
    List<Endpoint> endpoints,
    RazorComponentDataSourceOptions options)
{
    public List<Endpoint> Endpoints { get; } = endpoints;

    public RazorComponentDataSourceOptions Options { get; } = options;
}