@@ -21,11 +21,14 @@ namespace React.Owin
2121 /// </summary>
2222 public class JsxFileMiddleware
2323 {
24- private readonly StaticFileMiddleware _internalStaticMiddleware ;
24+ private readonly Func < IDictionary < string , object > , Task > _next ;
25+ private readonly StaticFileOptions _fileOptions ;
26+ private readonly IEnumerable < string > _extensions ;
2527
2628 static JsxFileMiddleware ( )
2729 {
28- Initializer . Initialize ( _ => _ ) ;
30+ // Assume that request will ask for the "per request" instances only once.
31+ Initializer . Initialize ( options => options . AsMultiInstance ( ) ) ;
2932 }
3033
3134 /// <summary>
@@ -38,34 +41,46 @@ public JsxFileMiddleware(Func<IDictionary<string, object>, Task> next, JsxFileOp
3841 if ( next == null )
3942 throw new ArgumentNullException ( "next" ) ;
4043
44+ _next = next ;
45+
4146 // Default values
4247 options = options ?? new JsxFileOptions ( ) ;
43- var extensions = ( options . Extensions == null || ! options . Extensions . Any ( ) ) ? new [ ] { ".jsx" , ".js" } : options . Extensions ;
44- var fileOptions = options . StaticFileOptions ?? new StaticFileOptions ( ) ;
45-
46- // Wrap the file system with JSX file system
47- var reactEnvironment = React . AssemblyRegistration . Container . Resolve < IReactEnvironment > ( ) ;
48- _internalStaticMiddleware = new StaticFileMiddleware (
49- next ,
50- new StaticFileOptions ( )
51- {
52- ContentTypeProvider = fileOptions . ContentTypeProvider ,
53- DefaultContentType = fileOptions . DefaultContentType ,
54- OnPrepareResponse = fileOptions . OnPrepareResponse ,
55- RequestPath = fileOptions . RequestPath ,
56- ServeUnknownFileTypes = fileOptions . ServeUnknownFileTypes ,
57- FileSystem = new JsxFileSystem ( reactEnvironment . JsxTransformer , fileOptions . FileSystem , extensions )
58- } ) ;
48+ _extensions = ( options . Extensions == null || ! options . Extensions . Any ( ) ) ? new [ ] { ".jsx" , ".js" } : options . Extensions ;
49+ _fileOptions = options . StaticFileOptions ?? new StaticFileOptions ( ) ;
5950 }
6051
6152 /// <summary>
6253 /// Processes a request to determine if it matches a known JSX file, and if so, serves it compiled to JavaScript.
6354 /// </summary>
6455 /// <param name="environment">OWIN environment dictionary which stores state information about the request, response and relevant server state.</param>
6556 /// <returns/>
66- public Task Invoke ( IDictionary < string , object > environment )
57+ public async Task Invoke ( IDictionary < string , object > environment )
6758 {
68- return _internalStaticMiddleware . Invoke ( environment ) ;
59+ // Create all "per request" instances
60+ var reactEnvironment = React . AssemblyRegistration . Container . Resolve < IReactEnvironment > ( ) ;
61+
62+ var internalStaticMiddleware = CreateFileMiddleware ( reactEnvironment . JsxTransformer ) ;
63+ await internalStaticMiddleware . Invoke ( environment ) ;
64+
65+ // Clean up all "per request" instances
66+ var disposable = reactEnvironment as IDisposable ;
67+ if ( disposable != null )
68+ disposable . Dispose ( ) ;
69+ }
70+
71+ private StaticFileMiddleware CreateFileMiddleware ( IJsxTransformer jsxTransformer )
72+ {
73+ return new StaticFileMiddleware (
74+ _next ,
75+ new StaticFileOptions ( )
76+ {
77+ ContentTypeProvider = _fileOptions . ContentTypeProvider ,
78+ DefaultContentType = _fileOptions . DefaultContentType ,
79+ OnPrepareResponse = _fileOptions . OnPrepareResponse ,
80+ RequestPath = _fileOptions . RequestPath ,
81+ ServeUnknownFileTypes = _fileOptions . ServeUnknownFileTypes ,
82+ FileSystem = new JsxFileSystem ( jsxTransformer , _fileOptions . FileSystem , _extensions )
83+ } ) ;
6984 }
7085 }
7186}
0 commit comments