22using System . Collections . Concurrent ;
33using System . Collections . Generic ;
44using System . Diagnostics ;
5- using System . IO ;
65using System . Linq ;
76using System . Threading ;
87using 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>
0 commit comments