Skip to content

Commit 57970a7

Browse files
author
Marcin Drobik
committed
Fixed tabs and spaces, internal class, and memory management for incoming requests
1 parent ae020e3 commit 57970a7

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

src/React.Owin/AssemblyRegistration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class AssemblyRegistration : IAssemblyRegistration
2323
/// <param name="container">Container to register components in</param>
2424
public void Register(TinyIoCContainer container)
2525
{
26-
container.Register<IFileSystem, EntryAssemblyFileSystem>();
27-
container.Register<ICache, MemoryFileCache>();
26+
container.Register<IFileSystem, EntryAssemblyFileSystem>();
27+
container.Register<ICache, MemoryFileCache>();
2828
}
2929
}
3030
}

src/React.Owin/JsxFileMiddleware.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/React.Owin/JsxFileSystem.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@ namespace React.Owin
2020
/// <summary>
2121
/// Owin file system that serves transformed JSX files.
2222
/// </summary>
23-
internal class JsxFileSystem : Microsoft.Owin.FileSystems.IFileSystem
23+
public class JsxFileSystem : Microsoft.Owin.FileSystems.IFileSystem
2424
{
2525
private readonly IJsxTransformer _transformer;
2626
private readonly Microsoft.Owin.FileSystems.IFileSystem _physicalFileSystem;
2727
private readonly string[] _extensions;
2828

29+
/// <summary>
30+
/// Creates a new instance of the JsxFileSystem.
31+
/// </summary>
32+
/// <param name="transformer">JSX transformer used to compile JSX files</param>
33+
/// <param name="root">The root directory</param>
34+
/// <param name="extensions">Extensions of files that will be treated as JSX files</param>
2935
public JsxFileSystem(IJsxTransformer transformer, string root, IEnumerable<string> extensions)
3036
: this(transformer, new PhysicalFileSystem(root), extensions)
3137
{
3238
}
3339

40+
/// <summary>
41+
/// Creates a new instance of the JsxFileSystem.
42+
/// </summary>
43+
/// <param name="transformer">JSX transformer used to compile JSX files</param>
44+
/// <param name="fileSystem">File system used to look up files</param>
45+
/// <param name="extensions">Extensions of files that will be treated as JSX files</param>
3446
public JsxFileSystem(IJsxTransformer transformer, Microsoft.Owin.FileSystems.IFileSystem fileSystem, IEnumerable<string> extensions)
3547
{
3648
_transformer = transformer;
@@ -40,6 +52,14 @@ public JsxFileSystem(IJsxTransformer transformer, Microsoft.Owin.FileSystems.IFi
4052
_extensions = extensions.Select(extension => extension.StartsWith(".") ? extension : "." + extension).ToArray();
4153
}
4254

55+
/// <summary>
56+
/// Locate a JSX file at the given path.
57+
/// </summary>
58+
/// <param name="subpath">The path that identifies the file</param>
59+
/// <param name="fileInfo">The discovered file if any</param>
60+
/// <returns>
61+
/// True if a JSX file was located at the given path
62+
/// </returns>
4363
public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
4464
{
4565
IFileInfo internalFileInfo;
@@ -55,6 +75,14 @@ public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
5575
return true;
5676
}
5777

78+
/// <summary>
79+
/// Enumerate a directory at the given path, if any
80+
/// </summary>
81+
/// <param name="subpath">The path that identifies the directory</param>
82+
/// <param name="contents">The contents if any</param>
83+
/// <returns>
84+
/// True if a directory was located at the given path
85+
/// </returns>
5886
public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents)
5987
{
6088
return _physicalFileSystem.TryGetDirectoryContents(subpath, out contents);

0 commit comments

Comments
 (0)