@@ -21,11 +21,14 @@ namespace React.Owin
21
21
/// </summary>
22
22
public class JsxFileMiddleware
23
23
{
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 ;
25
27
26
28
static JsxFileMiddleware ( )
27
29
{
28
- Initializer . Initialize ( _ => _ ) ;
30
+ // Assume that request will ask for the "per request" instances only once.
31
+ Initializer . Initialize ( options => options . AsMultiInstance ( ) ) ;
29
32
}
30
33
31
34
/// <summary>
@@ -38,34 +41,46 @@ public JsxFileMiddleware(Func<IDictionary<string, object>, Task> next, JsxFileOp
38
41
if ( next == null )
39
42
throw new ArgumentNullException ( "next" ) ;
40
43
44
+ _next = next ;
45
+
41
46
// Default values
42
47
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 ( ) ;
59
50
}
60
51
61
52
/// <summary>
62
53
/// Processes a request to determine if it matches a known JSX file, and if so, serves it compiled to JavaScript.
63
54
/// </summary>
64
55
/// <param name="environment">OWIN environment dictionary which stores state information about the request, response and relevant server state.</param>
65
56
/// <returns/>
66
- public Task Invoke ( IDictionary < string , object > environment )
57
+ public async Task Invoke ( IDictionary < string , object > environment )
67
58
{
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
+ } ) ;
69
84
}
70
85
}
71
86
}
0 commit comments