Skip to content

Commit 60c20ba

Browse files
waldekmastykarzgarrytrinder
authored andcommitted
Fixes reloading files multiple times. Closes #1120
1 parent 458d9e2 commit 60c20ba

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

dev-proxy-abstractions/BaseLoader.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public abstract class BaseLoader(ILogger logger, bool validateSchemas) : IDispos
1212
private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger));
1313
private readonly bool _validateSchemas = validateSchemas;
1414
private FileSystemWatcher? _watcher;
15+
private Timer? _debounceTimer;
16+
private readonly Lock _debounceLock = new();
17+
private readonly int _debounceDelay = 300; // milliseconds
1518
protected abstract string FilePath { get; }
1619

1720
protected abstract void LoadData(string fileContents);
@@ -63,7 +66,11 @@ private void LoadFileContents()
6366

6467
private void File_Changed(object sender, FileSystemEventArgs e)
6568
{
66-
LoadFileContents();
69+
lock (_debounceLock)
70+
{
71+
_debounceTimer?.Dispose();
72+
_debounceTimer = new Timer(_ => LoadFileContents(), null, _debounceDelay, Timeout.Infinite);
73+
}
6774
}
6875

6976
public void InitFileWatcher()
@@ -89,9 +96,6 @@ public void InitFileWatcher()
8996
Filter = Path.GetFileName(FilePath)
9097
};
9198
_watcher.Changed += File_Changed;
92-
_watcher.Created += File_Changed;
93-
_watcher.Deleted += File_Changed;
94-
_watcher.Renamed += File_Changed;
9599
_watcher.EnableRaisingEvents = true;
96100

97101
LoadFileContents();
@@ -100,6 +104,7 @@ public void InitFileWatcher()
100104
public void Dispose()
101105
{
102106
_watcher?.Dispose();
107+
_debounceTimer?.Dispose();
103108
GC.SuppressFinalize(this);
104109
}
105110
}

0 commit comments

Comments
 (0)