5
5
using Microsoft . AspNetCore . Html ;
6
6
using Microsoft . AspNetCore . Http ;
7
7
using Microsoft . AspNetCore . Mvc . Rendering ;
8
+ using Microsoft . AspNetCore . Mvc . ViewFeatures . Buffers ;
8
9
9
10
namespace Microsoft . AspNetCore . Mvc . ViewFeatures ;
10
11
11
- internal class ComponentRenderer : IComponentRenderer
12
+ internal sealed class ComponentRenderer : IComponentRenderer
12
13
{
13
14
private static readonly object ComponentSequenceKey = new object ( ) ;
14
15
private static readonly object InvokedRenderModesKey = new object ( ) ;
15
16
16
17
private readonly StaticComponentRenderer _staticComponentRenderer ;
17
18
private readonly ServerComponentSerializer _serverComponentSerializer ;
18
- private readonly WebAssemblyComponentSerializer _WebAssemblyComponentSerializer ;
19
+ private readonly IViewBufferScope _viewBufferScope ;
19
20
20
21
public ComponentRenderer (
21
22
StaticComponentRenderer staticComponentRenderer ,
22
23
ServerComponentSerializer serverComponentSerializer ,
23
- WebAssemblyComponentSerializer WebAssemblyComponentSerializer )
24
+ IViewBufferScope viewBufferScope )
24
25
{
25
26
_staticComponentRenderer = staticComponentRenderer ;
26
27
_serverComponentSerializer = serverComponentSerializer ;
27
- _WebAssemblyComponentSerializer = WebAssemblyComponentSerializer ;
28
+ _viewBufferScope = viewBufferScope ;
28
29
}
29
30
30
- public async Task < IHtmlContent > RenderComponentAsync (
31
+ public async ValueTask < IHtmlContent > RenderComponentAsync (
31
32
ViewContext viewContext ,
32
33
Type componentType ,
33
34
RenderMode renderMode ,
@@ -117,14 +118,12 @@ internal static InvokedRenderModes.Mode GetPersistStateRenderMode(ViewContext vi
117
118
}
118
119
}
119
120
120
- private async Task < IHtmlContent > StaticComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
121
+ private ValueTask < IHtmlContent > StaticComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
121
122
{
122
- var result = await _staticComponentRenderer . PrerenderComponentAsync (
123
+ return _staticComponentRenderer . PrerenderComponentAsync (
123
124
parametersCollection ,
124
125
context ,
125
126
type ) ;
126
-
127
- return new ComponentHtmlContent ( result ) ;
128
127
}
129
128
130
129
private async Task < IHtmlContent > PrerenderedServerComponentAsync ( HttpContext context , ServerComponentInvocationSequence invocationId , Type type , ParameterView parametersCollection )
@@ -145,13 +144,15 @@ private async Task<IHtmlContent> PrerenderedServerComponentAsync(HttpContext con
145
144
context ,
146
145
type ) ;
147
146
148
- return new ComponentHtmlContent (
149
- ServerComponentSerializer . GetPreamble ( currentInvocation ) ,
150
- result ,
151
- ServerComponentSerializer . GetEpilogue ( currentInvocation ) ) ;
147
+ var viewBuffer = new ViewBuffer ( _viewBufferScope , nameof ( ComponentRenderer ) , ViewBuffer . ViewPageSize ) ;
148
+ ServerComponentSerializer . AppendPreamble ( viewBuffer , currentInvocation ) ;
149
+ viewBuffer . AppendHtml ( result ) ;
150
+ ServerComponentSerializer . AppendEpilogue ( viewBuffer , currentInvocation ) ;
151
+
152
+ return viewBuffer ;
152
153
}
153
154
154
- private async Task < IHtmlContent > PrerenderedWebAssemblyComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
155
+ private async ValueTask < IHtmlContent > PrerenderedWebAssemblyComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
155
156
{
156
157
var currentInvocation = WebAssemblyComponentSerializer . SerializeInvocation (
157
158
type ,
@@ -163,10 +164,12 @@ private async Task<IHtmlContent> PrerenderedWebAssemblyComponentAsync(HttpContex
163
164
context ,
164
165
type ) ;
165
166
166
- return new ComponentHtmlContent (
167
- WebAssemblyComponentSerializer . GetPreamble ( currentInvocation ) ,
168
- result ,
169
- WebAssemblyComponentSerializer . GetEpilogue ( currentInvocation ) ) ;
167
+ var viewBuffer = new ViewBuffer ( _viewBufferScope , nameof ( ComponentRenderer ) , ViewBuffer . ViewPageSize ) ;
168
+ WebAssemblyComponentSerializer . AppendPreamble ( viewBuffer , currentInvocation ) ;
169
+ viewBuffer . AppendHtml ( result ) ;
170
+ WebAssemblyComponentSerializer . AppendEpilogue ( viewBuffer , currentInvocation ) ;
171
+
172
+ return viewBuffer ;
170
173
}
171
174
172
175
private IHtmlContent NonPrerenderedServerComponent ( HttpContext context , ServerComponentInvocationSequence invocationId , Type type , ParameterView parametersCollection )
@@ -178,31 +181,16 @@ private IHtmlContent NonPrerenderedServerComponent(HttpContext context, ServerCo
178
181
179
182
var currentInvocation = _serverComponentSerializer . SerializeInvocation ( invocationId , type , parametersCollection , prerendered : false ) ;
180
183
181
- return new ComponentHtmlContent ( ServerComponentSerializer . GetPreamble ( currentInvocation ) ) ;
184
+ var viewBuffer = new ViewBuffer ( _viewBufferScope , nameof ( ComponentRenderer ) , ServerComponentSerializer . PreambleBufferSize ) ;
185
+ ServerComponentSerializer . AppendPreamble ( viewBuffer , currentInvocation ) ;
186
+ return viewBuffer ;
182
187
}
183
188
184
- private static IHtmlContent NonPrerenderedWebAssemblyComponent ( HttpContext context , Type type , ParameterView parametersCollection )
189
+ private IHtmlContent NonPrerenderedWebAssemblyComponent ( HttpContext context , Type type , ParameterView parametersCollection )
185
190
{
186
191
var currentInvocation = WebAssemblyComponentSerializer . SerializeInvocation ( type , parametersCollection , prerendered : false ) ;
187
-
188
- return new ComponentHtmlContent ( WebAssemblyComponentSerializer . GetPreamble ( currentInvocation ) ) ;
189
- }
190
- }
191
-
192
- internal class InvokedRenderModes
193
- {
194
- public InvokedRenderModes ( Mode mode )
195
- {
196
- Value = mode ;
197
- }
198
-
199
- public Mode Value { get ; set ; }
200
-
201
- internal enum Mode
202
- {
203
- None ,
204
- Server ,
205
- WebAssembly ,
206
- ServerAndWebAssembly
192
+ var viewBuffer = new ViewBuffer ( _viewBufferScope , nameof ( ComponentRenderer ) , ServerComponentSerializer . PreambleBufferSize ) ;
193
+ WebAssemblyComponentSerializer . AppendPreamble ( viewBuffer , currentInvocation ) ;
194
+ return viewBuffer ;
207
195
}
208
196
}
0 commit comments