Skip to content

Commit

Permalink
Make file change debounce period configurable. References #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel15 committed Sep 19, 2016
1 parent 0200efd commit 3ed10aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/JSPool/FileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace JSPool
/// </summary>
public class FileWatcher : IFileWatcher, IDisposable
{
/// <summary>
/// Default value for <see cref="DebounceTimeout"/>.
/// </summary>
public static int DEFAULT_DEBOUNCE_TIMEOUT = 200;

/// <summary>
/// FileSystemWatcher that handles actually watching the path.
/// </summary>
Expand All @@ -31,16 +36,19 @@ public class FileWatcher : IFileWatcher, IDisposable
/// Timer for debouncing changes.
/// </summary>
protected Timer _timer;
/// <summary>
/// Time period to debounce file system changed events, in milliseconds.
/// </summary>
protected const int DEBOUNCE_TIMEOUT = 25;

/// <summary>
/// Occurs when any watched files have changed (including renames and deletions).
/// </summary>
public event EventHandler Changed;

/// <summary>
/// Gets or sets the time period to debounce file system changed events, in milliseconds.
/// This is useful to handle when multiple file change events happen in a short period of
/// time. JsPool will not reload/recycle the engines until this period elapses.
/// </summary>
public int DebounceTimeout { get; set; } = DEFAULT_DEBOUNCE_TIMEOUT;

/// <summary>
/// Gets or sets the path to watch.
/// </summary>
Expand Down Expand Up @@ -134,7 +142,7 @@ protected virtual void OnFileChanged(object sender, FileSystemEventArgs args)

Trace.WriteLine(string.Format("[JSPool] Watched file '{0}' changed", args.FullPath));
// Use a timer so multiple changes only result in a single reset.
_timer.Change(DEBOUNCE_TIMEOUT, Timeout.Infinite);
_timer.Change(DebounceTimeout, Timeout.Infinite);

}

Expand Down
1 change: 1 addition & 0 deletions src/JSPool/JsPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected virtual void InitializeWatcher()
{
_fileWatcher = new FileWatcher
{
DebounceTimeout = _config.DebounceTimeout,
Path = _config.WatchPath,
Files = _config.WatchFiles,
};
Expand Down
8 changes: 8 additions & 0 deletions src/JSPool/JsPoolConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ public class JsPoolConfig<T>
/// </summary>
public IEnumerable<string> WatchFiles { get; set; }

/// <summary>
/// Gets or sets the time period to debounce file system changed events, in milliseconds.
/// This is useful to handle when multiple file change events happen in a short period of
/// time. JsPool will not reload/recycle the engines until this period elapses.
/// </summary>
public int DebounceTimeout { get; set; }

/// <summary>
/// Creates a new JavaScript pool configuration. Default values will be set automatically.
/// </summary>
public JsPoolConfig()
{
DebounceTimeout = FileWatcher.DEFAULT_DEBOUNCE_TIMEOUT;
StartEngines = 10;
MaxEngines = 25;
MaxUsagesPerEngine = 100;
Expand Down

0 comments on commit 3ed10aa

Please sign in to comment.