-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh resources cache when the file changed
- Loading branch information
Showing
3 changed files
with
105 additions
and
21 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/My.Extensions.Localization.Json/Internal/JsonFileWatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace My.Extensions.Localization.Json.Internal; | ||
|
||
public class JsonFileWatcher : IDisposable | ||
{ | ||
private const string JsonExtension = "*.json"; | ||
|
||
private bool _disposed; | ||
|
||
private readonly FileSystemWatcher _filesWatcher; | ||
|
||
public event FileSystemEventHandler Changed; | ||
|
||
public JsonFileWatcher(string rootDirectory) | ||
{ | ||
_filesWatcher = new(rootDirectory) | ||
{ | ||
Filter = JsonExtension, | ||
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size, | ||
EnableRaisingEvents = true | ||
}; | ||
_filesWatcher.Changed += (s, e) => Changed?.Invoke(s, e); | ||
} | ||
|
||
~JsonFileWatcher() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(true); | ||
} | ||
|
||
public virtual void Dispose(bool disposing) | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
if (disposing) | ||
{ | ||
_filesWatcher.Dispose(); | ||
} | ||
|
||
_disposed = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters