@@ -27,10 +27,7 @@ public class ReactComponent : IReactComponent
27
27
{
28
28
private static readonly ConcurrentDictionary < string , bool > _componentNameValidCache = new ConcurrentDictionary < string , bool > ( ) ;
29
29
30
- [ ThreadStatic ]
31
- private static StringWriter _sharedStringWriter ;
32
-
33
- private PagedPooledTextWriter _serializedProps ;
30
+ private ReactPooledTextWriter _serializedProps ;
34
31
35
32
/// <summary>
36
33
/// Regular expression used to validate JavaScript identifiers. Used to ensure component
@@ -49,6 +46,10 @@ public class ReactComponent : IReactComponent
49
46
/// </summary>
50
47
protected readonly IReactSiteConfiguration _configuration ;
51
48
49
+ /// <summary>
50
+ /// Raw props for this component
51
+ /// </summary>
52
+ protected object _props ;
52
53
53
54
/// <summary>
54
55
/// Gets or sets the name of the component
@@ -75,8 +76,6 @@ public class ReactComponent : IReactComponent
75
76
/// </summary>
76
77
public bool ServerOnly { get ; set ; }
77
78
78
- private object _props ;
79
-
80
79
/// <summary>
81
80
/// Gets or sets the props for this component
82
81
/// </summary>
@@ -117,9 +116,16 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
117
116
/// <returns>HTML</returns>
118
117
public string RenderHtml ( bool renderContainerOnly = false , bool renderServerOnly = false , Action < Exception , string , string > exceptionHandler = null )
119
118
{
120
- var writer = new StringWriter ( ) ;
121
- RenderHtml ( writer , renderContainerOnly , renderServerOnly , exceptionHandler ) ;
122
- return writer . ToString ( ) ;
119
+ var pooledWriter = new ReactPooledTextWriter ( ReactArrayPool < char > . Instance ) ;
120
+ try
121
+ {
122
+ RenderHtml ( pooledWriter , renderContainerOnly , renderServerOnly , exceptionHandler ) ;
123
+ return pooledWriter . ToString ( ) ;
124
+ }
125
+ finally
126
+ {
127
+ pooledWriter . Dispose ( ) ;
128
+ }
123
129
}
124
130
125
131
/// <summary>
@@ -146,25 +152,14 @@ public virtual void RenderHtml(TextWriter writer, bool renderContainerOnly = fal
146
152
var html = string . Empty ;
147
153
if ( ! renderContainerOnly )
148
154
{
149
- var stringWriter = _sharedStringWriter ;
150
- if ( stringWriter != null )
151
- {
152
- stringWriter . GetStringBuilder ( ) . Clear ( ) ;
153
- }
154
- else
155
- {
156
- _sharedStringWriter =
157
- stringWriter =
158
- new StringWriter ( new StringBuilder ( 512 ) ) ;
159
- }
160
-
155
+ var pooledWriter = new ReactPooledTextWriter ( ReactArrayPool < char > . Instance ) ;
161
156
try
162
157
{
163
- stringWriter . Write ( renderServerOnly ? "ReactDOMServer.renderToStaticMarkup(" : "ReactDOMServer.renderToString(" ) ;
164
- WriteComponentInitialiser ( stringWriter ) ;
165
- stringWriter . Write ( ')' ) ;
158
+ pooledWriter . Write ( renderServerOnly ? "ReactDOMServer.renderToStaticMarkup(" : "ReactDOMServer.renderToString(" ) ;
159
+ WriteComponentInitialiser ( pooledWriter ) ;
160
+ pooledWriter . Write ( ')' ) ;
166
161
167
- html = _environment . Execute < string > ( stringWriter . ToString ( ) ) ;
162
+ html = _environment . Execute < string > ( pooledWriter . ToString ( ) ) ;
168
163
169
164
if ( renderServerOnly )
170
165
{
@@ -181,6 +176,10 @@ public virtual void RenderHtml(TextWriter writer, bool renderContainerOnly = fal
181
176
182
177
exceptionHandler ( ex , ComponentName , ContainerId ) ;
183
178
}
179
+ finally
180
+ {
181
+ pooledWriter . Dispose ( ) ;
182
+ }
184
183
}
185
184
186
185
writer . Write ( '<' ) ;
@@ -270,7 +269,7 @@ protected void WriteSerializedProps(TextWriter writer)
270
269
{
271
270
var pool = ReactArrayPool < char > . Instance ;
272
271
273
- _serializedProps = new PagedPooledTextWriter ( pool ) ;
272
+ _serializedProps = new ReactPooledTextWriter ( pool ) ;
274
273
275
274
using ( var jsonWriter = new JsonTextWriter ( _serializedProps ) )
276
275
{
0 commit comments