@@ -12,6 +12,9 @@ public abstract class BaseLoader(ILogger logger, bool validateSchemas) : IDispos
12
12
private readonly ILogger _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
13
13
private readonly bool _validateSchemas = validateSchemas ;
14
14
private FileSystemWatcher ? _watcher ;
15
+ private Timer ? _debounceTimer ;
16
+ private readonly Lock _debounceLock = new ( ) ;
17
+ private readonly int _debounceDelay = 300 ; // milliseconds
15
18
protected abstract string FilePath { get ; }
16
19
17
20
protected abstract void LoadData ( string fileContents ) ;
@@ -63,7 +66,11 @@ private void LoadFileContents()
63
66
64
67
private void File_Changed ( object sender , FileSystemEventArgs e )
65
68
{
66
- LoadFileContents ( ) ;
69
+ lock ( _debounceLock )
70
+ {
71
+ _debounceTimer ? . Dispose ( ) ;
72
+ _debounceTimer = new Timer ( _ => LoadFileContents ( ) , null , _debounceDelay , Timeout . Infinite ) ;
73
+ }
67
74
}
68
75
69
76
public void InitFileWatcher ( )
@@ -89,9 +96,6 @@ public void InitFileWatcher()
89
96
Filter = Path . GetFileName ( FilePath )
90
97
} ;
91
98
_watcher . Changed += File_Changed ;
92
- _watcher . Created += File_Changed ;
93
- _watcher . Deleted += File_Changed ;
94
- _watcher . Renamed += File_Changed ;
95
99
_watcher . EnableRaisingEvents = true ;
96
100
97
101
LoadFileContents ( ) ;
@@ -100,6 +104,7 @@ public void InitFileWatcher()
100
104
public void Dispose ( )
101
105
{
102
106
_watcher ? . Dispose ( ) ;
107
+ _debounceTimer ? . Dispose ( ) ;
103
108
GC . SuppressFinalize ( this ) ;
104
109
}
105
110
}
0 commit comments