【优化】解决Luban.Client在watch模式下,数据目录变化后,触发两次生成(第二次多余)的问题

main
walon 2021-06-18 13:03:03 +08:00
parent 8977b5c4c3
commit dd4368faf9
1 changed files with 24 additions and 6 deletions

View File

@ -26,13 +26,13 @@ namespace Luban.Client.Utils
{ {
var watcher = new FileSystemWatcher(file); var watcher = new FileSystemWatcher(file);
watcher.NotifyFilter = NotifyFilters.Attributes watcher.NotifyFilter = // NotifyFilters.Attributes
| NotifyFilters.CreationTime //| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName NotifyFilters.DirectoryName
| NotifyFilters.FileName | NotifyFilters.FileName
| NotifyFilters.LastAccess //| NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.LastWrite
| NotifyFilters.Security //| NotifyFilters.Security
| NotifyFilters.Size; | NotifyFilters.Size;
watcher.Changed += this.OnChange; watcher.Changed += this.OnChange;
@ -50,19 +50,37 @@ namespace Luban.Client.Utils
} }
} }
private readonly static List<string> _filterSuffixs = new List<string>
{
".xlsx",
".csv",
".xls",
".json",
".lua",
".xml",
};
private void OnChange(object sender, FileSystemEventArgs e) private void OnChange(object sender, FileSystemEventArgs e)
{ {
var dirtyName = e.Name;
if (string.IsNullOrWhiteSpace(dirtyName) || !_filterSuffixs.Any(s => dirtyName.EndsWith(s)))
{
return;
}
lock (_watchLocker) lock (_watchLocker)
{ {
if (_watchDirChange) if (_watchDirChange)
{ {
s_logger.Trace("== has mark dirty:{}. ignore", e.FullPath);
return; return;
} }
_watchDirChange = true; _watchDirChange = true;
s_logger.Trace("== mark dirty:{}", e.FullPath);
Task.Run(async () => Task.Run(async () =>
{ {
await Task.Delay(400); await Task.Delay(100);
lock (_watchLocker) lock (_watchLocker)
{ {