-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
HtmlRenderer.cs
138 lines (123 loc) · 8.29 KB
/
HtmlRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure;
using Microsoft.AspNetCore.Components.Web.HtmlRendering;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Components.Web;
/// <summary>
/// Provides a mechanism for rendering components non-interactively as HTML markup.
/// </summary>
public sealed class HtmlRenderer : IDisposable, IAsyncDisposable
{
private readonly StaticHtmlRenderer _passiveHtmlRenderer;
/// <summary>
/// Constructs an instance of <see cref="HtmlRenderer"/>.
/// </summary>
/// <param name="services">The services to use when rendering components.</param>
/// <param name="loggerFactory">The logger factory to use.</param>
public HtmlRenderer(IServiceProvider services, ILoggerFactory loggerFactory)
{
_passiveHtmlRenderer = new StaticHtmlRenderer(services, loggerFactory);
}
/// <inheritdoc />
public void Dispose()
=> _passiveHtmlRenderer.Dispose();
/// <inheritdoc />
public ValueTask DisposeAsync()
=> _passiveHtmlRenderer.DisposeAsync();
/// <summary>
/// Gets the <see cref="Components.Dispatcher" /> associated with this instance. Any calls to
/// <see cref="RenderComponentAsync{TComponent}()"/> or <see cref="BeginRenderingComponent{TComponent}()"/>
/// must be performed using this <see cref="Components.Dispatcher" />.
/// </summary>
public Dispatcher Dispatcher => _passiveHtmlRenderer.Dispatcher;
/// <summary>
/// Adds an instance of the specified component and instructs it to render. The resulting content represents the
/// initial synchronous rendering output, which may later change. To wait for the component hierarchy to complete
/// any asynchronous operations such as loading, await <see cref="HtmlRootComponent.QuiescenceTask"/> before
/// reading content from the <see cref="HtmlRootComponent"/>.
/// </summary>
/// <typeparam name="TComponent">The component type.</typeparam>
/// <returns>An <see cref="HtmlRootComponent"/> instance representing the render output.</returns>
public HtmlRootComponent BeginRenderingComponent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TComponent>() where TComponent : IComponent
=> _passiveHtmlRenderer.BeginRenderingComponent(typeof(TComponent), ParameterView.Empty);
/// <summary>
/// Adds an instance of the specified component and instructs it to render. The resulting content represents the
/// initial synchronous rendering output, which may later change. To wait for the component hierarchy to complete
/// any asynchronous operations such as loading, await <see cref="HtmlRootComponent.QuiescenceTask"/> before
/// reading content from the <see cref="HtmlRootComponent"/>.
/// </summary>
/// <typeparam name="TComponent">The component type.</typeparam>
/// <param name="parameters">Parameters for the component.</param>
/// <returns>An <see cref="HtmlRootComponent"/> instance representing the render output.</returns>
public HtmlRootComponent BeginRenderingComponent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TComponent>(
ParameterView parameters) where TComponent : IComponent
=> _passiveHtmlRenderer.BeginRenderingComponent(typeof(TComponent), parameters);
/// <summary>
/// Adds an instance of the specified component and instructs it to render. The resulting content represents the
/// initial synchronous rendering output, which may later change. To wait for the component hierarchy to complete
/// any asynchronous operations such as loading, await <see cref="HtmlRootComponent.QuiescenceTask"/> before
/// reading content from the <see cref="HtmlRootComponent"/>.
/// </summary>
/// <param name="componentType">The component type. This must implement <see cref="IComponent"/>.</param>
/// <returns>An <see cref="HtmlRootComponent"/> instance representing the render output.</returns>
public HtmlRootComponent BeginRenderingComponent(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType)
=> _passiveHtmlRenderer.BeginRenderingComponent(componentType, ParameterView.Empty);
/// <summary>
/// Adds an instance of the specified component and instructs it to render. The resulting content represents the
/// initial synchronous rendering output, which may later change. To wait for the component hierarchy to complete
/// any asynchronous operations such as loading, await <see cref="HtmlRootComponent.QuiescenceTask"/> before
/// reading content from the <see cref="HtmlRootComponent"/>.
/// </summary>
/// <param name="componentType">The component type. This must implement <see cref="IComponent"/>.</param>
/// <param name="parameters">Parameters for the component.</param>
/// <returns>An <see cref="HtmlRootComponent"/> instance representing the render output.</returns>
public HtmlRootComponent BeginRenderingComponent(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType,
ParameterView parameters)
=> _passiveHtmlRenderer.BeginRenderingComponent(componentType, parameters);
/// <summary>
/// Adds an instance of the specified component and instructs it to render, waiting
/// for the component hierarchy to complete asynchronous tasks such as loading.
/// </summary>
/// <typeparam name="TComponent">The component type.</typeparam>
/// <returns>A task that completes with <see cref="HtmlRootComponent"/> once the component hierarchy has completed any asynchronous tasks such as loading.</returns>
public Task<HtmlRootComponent> RenderComponentAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TComponent>() where TComponent : IComponent
=> RenderComponentAsync<TComponent>(ParameterView.Empty);
/// <summary>
/// Adds an instance of the specified component and instructs it to render, waiting
/// for the component hierarchy to complete asynchronous tasks such as loading.
/// </summary>
/// <param name="componentType">The component type. This must implement <see cref="IComponent"/>.</param>
/// <returns>A task that completes with <see cref="HtmlRootComponent"/> once the component hierarchy has completed any asynchronous tasks such as loading.</returns>
public Task<HtmlRootComponent> RenderComponentAsync(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType)
=> RenderComponentAsync(componentType, ParameterView.Empty);
/// <summary>
/// Adds an instance of the specified component and instructs it to render, waiting
/// for the component hierarchy to complete asynchronous tasks such as loading.
/// </summary>
/// <typeparam name="TComponent">The component type.</typeparam>
/// <param name="parameters">Parameters for the component.</param>
/// <returns>A task that completes with <see cref="HtmlRootComponent"/> once the component hierarchy has completed any asynchronous tasks such as loading.</returns>
public Task<HtmlRootComponent> RenderComponentAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TComponent>(
ParameterView parameters) where TComponent : IComponent
=> RenderComponentAsync(typeof (TComponent), parameters);
/// <summary>
/// Adds an instance of the specified component and instructs it to render, waiting
/// for the component hierarchy to complete asynchronous tasks such as loading.
/// </summary>
/// <param name="componentType">The component type. This must implement <see cref="IComponent"/>.</param>
/// <param name="parameters">Parameters for the component.</param>
/// <returns>A task that completes with <see cref="HtmlRootComponent"/> once the component hierarchy has completed any asynchronous tasks such as loading.</returns>
public async Task<HtmlRootComponent> RenderComponentAsync(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType,
ParameterView parameters)
{
var content = BeginRenderingComponent(componentType, parameters);
await content.QuiescenceTask;
return content;
}
}