55using Microsoft . AspNetCore . Html ;
66using Microsoft . AspNetCore . Http ;
77using Microsoft . AspNetCore . Mvc . Rendering ;
8+ using Microsoft . AspNetCore . Mvc . ViewFeatures . Buffers ;
89
910namespace Microsoft . AspNetCore . Mvc . ViewFeatures ;
1011
11- internal class ComponentRenderer : IComponentRenderer
12+ internal sealed class ComponentRenderer : IComponentRenderer
1213{
1314 private static readonly object ComponentSequenceKey = new object ( ) ;
1415 private static readonly object InvokedRenderModesKey = new object ( ) ;
1516
1617 private readonly StaticComponentRenderer _staticComponentRenderer ;
1718 private readonly ServerComponentSerializer _serverComponentSerializer ;
18- private readonly WebAssemblyComponentSerializer _WebAssemblyComponentSerializer ;
19+ private readonly IViewBufferScope _viewBufferScope ;
1920
2021 public ComponentRenderer (
2122 StaticComponentRenderer staticComponentRenderer ,
2223 ServerComponentSerializer serverComponentSerializer ,
23- WebAssemblyComponentSerializer WebAssemblyComponentSerializer )
24+ IViewBufferScope viewBufferScope )
2425 {
2526 _staticComponentRenderer = staticComponentRenderer ;
2627 _serverComponentSerializer = serverComponentSerializer ;
27- _WebAssemblyComponentSerializer = WebAssemblyComponentSerializer ;
28+ _viewBufferScope = viewBufferScope ;
2829 }
2930
30- public async Task < IHtmlContent > RenderComponentAsync (
31+ public async ValueTask < IHtmlContent > RenderComponentAsync (
3132 ViewContext viewContext ,
3233 Type componentType ,
3334 RenderMode renderMode ,
@@ -117,14 +118,12 @@ internal static InvokedRenderModes.Mode GetPersistStateRenderMode(ViewContext vi
117118 }
118119 }
119120
120- private async Task < IHtmlContent > StaticComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
121+ private ValueTask < IHtmlContent > StaticComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
121122 {
122- var result = await _staticComponentRenderer . PrerenderComponentAsync (
123+ return _staticComponentRenderer . PrerenderComponentAsync (
123124 parametersCollection ,
124125 context ,
125126 type ) ;
126-
127- return new ComponentHtmlContent ( result ) ;
128127 }
129128
130129 private async Task < IHtmlContent > PrerenderedServerComponentAsync ( HttpContext context , ServerComponentInvocationSequence invocationId , Type type , ParameterView parametersCollection )
@@ -145,13 +144,15 @@ private async Task<IHtmlContent> PrerenderedServerComponentAsync(HttpContext con
145144 context ,
146145 type ) ;
147146
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 ;
152153 }
153154
154- private async Task < IHtmlContent > PrerenderedWebAssemblyComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
155+ private async ValueTask < IHtmlContent > PrerenderedWebAssemblyComponentAsync ( HttpContext context , Type type , ParameterView parametersCollection )
155156 {
156157 var currentInvocation = WebAssemblyComponentSerializer . SerializeInvocation (
157158 type ,
@@ -163,10 +164,12 @@ private async Task<IHtmlContent> PrerenderedWebAssemblyComponentAsync(HttpContex
163164 context ,
164165 type ) ;
165166
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 ;
170173 }
171174
172175 private IHtmlContent NonPrerenderedServerComponent ( HttpContext context , ServerComponentInvocationSequence invocationId , Type type , ParameterView parametersCollection )
@@ -178,31 +181,16 @@ private IHtmlContent NonPrerenderedServerComponent(HttpContext context, ServerCo
178181
179182 var currentInvocation = _serverComponentSerializer . SerializeInvocation ( invocationId , type , parametersCollection , prerendered : false ) ;
180183
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 ;
182187 }
183188
184- private static IHtmlContent NonPrerenderedWebAssemblyComponent ( HttpContext context , Type type , ParameterView parametersCollection )
189+ private IHtmlContent NonPrerenderedWebAssemblyComponent ( HttpContext context , Type type , ParameterView parametersCollection )
185190 {
186191 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 ;
207195 }
208196}
0 commit comments