Skip to content

Commit 6d8efb5

Browse files
committed
Upgrade to JSPool 0.3 and use its file watching
1 parent 5888655 commit 6d8efb5

File tree

5 files changed

+12
-81
lines changed

5 files changed

+12
-81
lines changed

src/React.Core/JavaScriptEngineFactory.cs

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5-
using System.IO;
65
using System.Linq;
76
using System.Threading;
87
using JavaScriptEngineSwitcher.Core;
@@ -40,28 +39,10 @@ protected readonly ConcurrentDictionary<int, IJsEngine> _engines
4039
/// </summary>
4140
protected IJsPool _pool;
4241
/// <summary>
43-
/// Used to recycle the JavaScript engine pool when relevant JavaScript files are modified.
44-
/// </summary>
45-
protected readonly FileSystemWatcher _watcher;
46-
/// <summary>
47-
/// Names of all the files that are loaded into the JavaScript engine. If any of these
48-
/// files are changed, the engines should be recycled
49-
/// </summary>
50-
protected readonly ISet<string> _watchedFiles;
51-
/// <summary>
52-
/// Timer for debouncing pool recycling
53-
/// </summary>
54-
protected readonly Timer _resetPoolTimer;
55-
/// <summary>
5642
/// Whether this class has been disposed.
5743
/// </summary>
5844
protected bool _disposed;
5945

60-
/// <summary>
61-
/// Time period to debounce file system changed events, in milliseconds.
62-
/// </summary>
63-
protected const int DEBOUNCE_TIMEOUT = 25;
64-
6546
/// <summary>
6647
/// Initializes a new instance of the <see cref="JavaScriptEngineFactory"/> class.
6748
/// </summary>
@@ -77,32 +58,6 @@ IFileSystem fileSystem
7758
if (_config.ReuseJavaScriptEngines)
7859
{
7960
_pool = CreatePool();
80-
_resetPoolTimer = new Timer(OnResetPoolTimer);
81-
82-
var allFiles = _config.Scripts.Concat(_config.ScriptsWithoutTransform);
83-
_watchedFiles = new HashSet<string>(allFiles.Select(
84-
fileName => _fileSystem.MapPath(fileName).ToLowerInvariant()
85-
));
86-
try
87-
{
88-
// Attempt to initialise a FileSystemWatcher so we can recycle the JavaScript
89-
// engine pool when files are changed.
90-
_watcher = new FileSystemWatcher
91-
{
92-
Path = _fileSystem.MapPath("~"),
93-
IncludeSubdirectories = true,
94-
EnableRaisingEvents = true,
95-
};
96-
_watcher.Changed += OnFileChanged;
97-
_watcher.Created += OnFileChanged;
98-
_watcher.Deleted += OnFileChanged;
99-
_watcher.Renamed += OnFileChanged;
100-
}
101-
catch (Exception ex)
102-
{
103-
// Can't use FileSystemWatcher (eg. not running in Full Trust)
104-
Trace.WriteLine("Unable to initialise FileSystemWatcher: " + ex.Message);
105-
}
10661
}
10762
}
10863

@@ -111,10 +66,16 @@ IFileSystem fileSystem
11166
/// </summary>
11267
protected virtual IJsPool CreatePool()
11368
{
69+
var allFiles = _config.Scripts
70+
.Concat(_config.ScriptsWithoutTransform)
71+
.Select(_fileSystem.MapPath);
72+
11473
var poolConfig = new JsPoolConfig
11574
{
11675
EngineFactory = _factory,
11776
Initializer = InitialiseEngine,
77+
WatchPath = _fileSystem.MapPath("~/"),
78+
WatchFiles = allFiles
11879
};
11980
if (_config.MaxEngines != null)
12081
{
@@ -337,36 +298,6 @@ public virtual void Dispose()
337298
}
338299
}
339300

340-
/// <summary>
341-
/// Handles events fired when any files are changed.
342-
/// </summary>
343-
/// <param name="sender">The sender</param>
344-
/// <param name="args">The <see cref="FileSystemEventArgs"/> instance containing the event data</param>
345-
protected virtual void OnFileChanged(object sender, FileSystemEventArgs args)
346-
{
347-
if (_watchedFiles.Contains(args.FullPath.ToLowerInvariant()))
348-
{
349-
// Use a timer so multiple changes only result in a single reset.
350-
_resetPoolTimer.Change(DEBOUNCE_TIMEOUT, Timeout.Infinite);
351-
}
352-
}
353-
354-
/// <summary>
355-
/// Called when any of the watched files have changed. Recycles the JavaScript engine pool
356-
/// so the files are all reloaded.
357-
/// </summary>
358-
/// <param name="state">Unused</param>
359-
protected virtual void OnResetPoolTimer(object state)
360-
{
361-
// Create the new pool before disposing the old pool so that _pool is never null.
362-
var oldPool = _pool;
363-
_pool = CreatePool();
364-
if (oldPool != null)
365-
{
366-
oldPool.Dispose();
367-
}
368-
}
369-
370301
/// <summary>
371302
/// Ensures that this object has not been disposed.
372303
/// </summary>

src/React.Core/React.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
<Reference Include="JavaScriptEngineSwitcher.V8">
6161
<HintPath>..\packages\JavaScriptEngineSwitcher.V8.1.2.4\lib\net40\JavaScriptEngineSwitcher.V8.dll</HintPath>
6262
</Reference>
63-
<Reference Include="JSPool, Version=0.2.0.0, Culture=neutral, PublicKeyToken=2fc7775f73072640, processorArchitecture=MSIL">
63+
<Reference Include="JSPool, Version=0.3.0.0, Culture=neutral, PublicKeyToken=2fc7775f73072640, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>
65-
<HintPath>..\packages\JSPool.0.2.0\lib\net40-Client\JSPool.dll</HintPath>
65+
<HintPath>..\packages\JSPool.0.3.0\lib\net40-Client\JSPool.dll</HintPath>
6666
</Reference>
6767
<Reference Include="MsieJavaScriptEngine, Version=1.5.1.0, Culture=neutral, PublicKeyToken=a3a2846a37ac0d3e, processorArchitecture=MSIL">
6868
<SpecificVersion>False</SpecificVersion>

src/React.Core/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package id="JavaScriptEngineSwitcher.Core" version="1.2.4" targetFramework="net40" />
44
<package id="JavaScriptEngineSwitcher.Msie" version="1.2.4" targetFramework="net40" />
55
<package id="JavaScriptEngineSwitcher.V8" version="1.2.4" targetFramework="net40" />
6-
<package id="JSPool" version="0.2.0" targetFramework="net40" />
6+
<package id="JSPool" version="0.3.0" targetFramework="net40" />
77
<package id="MsieJavaScriptEngine" version="1.5.1" targetFramework="net40" />
88
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net40" />
99
</packages>

src/wrap/React.Core/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"JavaScriptEngineSwitcher.Core": "1.2.4",
1212
"JavaScriptEngineSwitcher.V8": "1.2.4",
13-
"JSPool": "0.2.0",
13+
"JSPool": "0.3.0",
1414
"Newtonsoft.Json": "5.0.4",
1515
"VroomJs": "1.0.0-*",
1616
"JavaScriptEngineSwitcher.Msie": "1.2.4",

src/wrap/React/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"dependencies": {
1111
"JavaScriptEngineSwitcher.Core": "1.2.4",
1212
"JavaScriptEngineSwitcher.V8": "1.2.4",
13-
"JSPool": "0.2.0",
14-
"Newtonsoft.Json": "5.0.4",
13+
"JSPool": "0.3.0",
14+
"Newtonsoft.Json": "6.0.8",
1515
"VroomJs": "1.0.0-*",
1616
"JavaScriptEngineSwitcher.Msie": "1.2.4",
1717
"MsieJavaScriptEngine": "1.5.1"

0 commit comments

Comments
 (0)