2
2
using System . Collections . Concurrent ;
3
3
using System . Collections . Generic ;
4
4
using System . Diagnostics ;
5
- using System . IO ;
6
5
using System . Linq ;
7
6
using System . Threading ;
8
7
using JavaScriptEngineSwitcher . Core ;
@@ -40,28 +39,10 @@ protected readonly ConcurrentDictionary<int, IJsEngine> _engines
40
39
/// </summary>
41
40
protected IJsPool _pool ;
42
41
/// <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>
56
42
/// Whether this class has been disposed.
57
43
/// </summary>
58
44
protected bool _disposed ;
59
45
60
- /// <summary>
61
- /// Time period to debounce file system changed events, in milliseconds.
62
- /// </summary>
63
- protected const int DEBOUNCE_TIMEOUT = 25 ;
64
-
65
46
/// <summary>
66
47
/// Initializes a new instance of the <see cref="JavaScriptEngineFactory"/> class.
67
48
/// </summary>
@@ -77,32 +58,6 @@ IFileSystem fileSystem
77
58
if ( _config . ReuseJavaScriptEngines )
78
59
{
79
60
_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
- }
106
61
}
107
62
}
108
63
@@ -111,10 +66,16 @@ IFileSystem fileSystem
111
66
/// </summary>
112
67
protected virtual IJsPool CreatePool ( )
113
68
{
69
+ var allFiles = _config . Scripts
70
+ . Concat ( _config . ScriptsWithoutTransform )
71
+ . Select ( _fileSystem . MapPath ) ;
72
+
114
73
var poolConfig = new JsPoolConfig
115
74
{
116
75
EngineFactory = _factory ,
117
76
Initializer = InitialiseEngine ,
77
+ WatchPath = _fileSystem . MapPath ( "~/" ) ,
78
+ WatchFiles = allFiles
118
79
} ;
119
80
if ( _config . MaxEngines != null )
120
81
{
@@ -337,36 +298,6 @@ public virtual void Dispose()
337
298
}
338
299
}
339
300
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
-
370
301
/// <summary>
371
302
/// Ensures that this object has not been disposed.
372
303
/// </summary>
0 commit comments