simple refactoring

This commit is contained in:
bao-qian
2016-07-21 00:02:13 +01:00
parent 59fbb2acb1
commit a8c200f25a
2 changed files with 10 additions and 18 deletions

View File

@@ -1,33 +1,31 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
namespace Wox.Plugin.Program namespace Wox.Plugin.Program
{ {
internal class FileChangeWatcher internal static class FileChangeWatcher
{ {
private static bool isIndexing; private static readonly List<string> WatchedPath = new List<string>();
private static List<string> watchedPath = new List<string>();
public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true) public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true)
{ {
if (watchedPath.Contains(path)) return; if (WatchedPath.Contains(path)) return;
if (!Directory.Exists(path)) if (!Directory.Exists(path))
{ {
Log.Warn($"FileChangeWatcher: {path} doesn't exist"); Log.Warn($"FileChangeWatcher: {path} doesn't exist");
return; return;
} }
watchedPath.Add(path); WatchedPath.Add(path);
foreach (string fileType in programSuffixes) foreach (string fileType in programSuffixes)
{ {
FileSystemWatcher watcher = new FileSystemWatcher FileSystemWatcher watcher = new FileSystemWatcher
{ {
Path = path, Path = path,
IncludeSubdirectories = includingSubDirectory, IncludeSubdirectories = includingSubDirectory,
Filter = string.Format("*.{0}", fileType), Filter = $"*.{fileType}",
EnableRaisingEvents = true EnableRaisingEvents = true
}; };
watcher.Changed += FileChanged; watcher.Changed += FileChanged;
@@ -39,15 +37,10 @@ namespace Wox.Plugin.Program
private static void FileChanged(object source, FileSystemEventArgs e) private static void FileChanged(object source, FileSystemEventArgs e)
{ {
if (!isIndexing) Task.Run(() =>
{ {
Task.Run(() => Main.IndexPrograms();
{ });
Main.IndexPrograms();
isIndexing = false;
});
}
} }
} }
} }

View File

@@ -14,7 +14,7 @@ namespace Wox.Plugin.Program
{ {
public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable
{ {
private static object lockObject = new object(); private static readonly object IndexLock = new object();
private static List<Program> _programs = new List<Program>(); private static List<Program> _programs = new List<Program>();
private static List<IProgramSource> _sources = new List<IProgramSource>(); private static List<IProgramSource> _sources = new List<IProgramSource>();
private static readonly Dictionary<string, Type> SourceTypes = new Dictionary<string, Type> private static readonly Dictionary<string, Type> SourceTypes = new Dictionary<string, Type>
@@ -96,8 +96,7 @@ namespace Wox.Plugin.Program
public static void IndexPrograms() public static void IndexPrograms()
{ {
// todo why there is a lock?? lock (IndexLock)
lock (lockObject)
{ {
var sources = DefaultProgramSources(); var sources = DefaultProgramSources();
if (_settings.ProgramSources != null && if (_settings.ProgramSources != null &&