-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSystemEventBuffer.cs
174 lines (151 loc) · 3.75 KB
/
FileSystemEventBuffer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlaxEngine;
namespace ResourcesPlugin.Source.Editor
{
/// <summary>
/// Buffers the events of a <see cref="FileSystemWatcher"/>
/// </summary>
public class FileSystemEventBuffer : IDisposable
{
private Dictionary<string, WatcherChangeTypes> _changedPaths = new Dictionary<string, WatcherChangeTypes>();
private Dictionary<string, WatcherChangeTypes> _currentChangedPaths = new Dictionary<string, WatcherChangeTypes>();
private readonly System.Timers.Timer _timer = new System.Timers.Timer(500);
public double EventInterval
{
get => _timer.Interval;
set => _timer.Interval = value;
}
public bool Enabled
{
get
{
return _timer.Enabled;
}
set
{
if (_timer.Enabled != value)
{
if (value)
{
_timer.Elapsed += _timer_Elapsed;
}
else
{
_timer.Elapsed -= _timer_Elapsed;
}
_timer.Enabled = value;
}
}
}
private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
RaiseEvents();
}
public void ChangeEvent(object sender, RenamedEventArgs e)
{
_changedPaths.Add(e.OldFullPath, WatcherChangeTypes.Deleted);
_changedPaths.Add(e.FullPath, WatcherChangeTypes.Created);
}
public void ChangeEvent(object sender, FileSystemEventArgs e)
{
//WatcherChangeTypes
string path = e.FullPath;
if (_changedPaths.TryGetValue(path, out WatcherChangeTypes changeType))
{
_changedPaths[path] = CombineChangeTypes(changeType, e.ChangeType);
}
else
{
_changedPaths.Add(path, e.ChangeType);
}
}
private WatcherChangeTypes CombineChangeTypes(WatcherChangeTypes oldChangeType, WatcherChangeTypes newChangeType)
{
switch (newChangeType)
{
case WatcherChangeTypes.Created:
{
if (oldChangeType == WatcherChangeTypes.Deleted) return WatcherChangeTypes.Changed; // TODO: Is this a good idea?
else return WatcherChangeTypes.Created;
}
case WatcherChangeTypes.Changed:
{
return WatcherChangeTypes.Changed;
}
case WatcherChangeTypes.Deleted:
{
return WatcherChangeTypes.Deleted;
}
default:
{
throw new Exception("Unknown change type");
}
}
}
public void RaiseEvents()
{
_currentChangedPaths.Clear();
Dictionary<string, WatcherChangeTypes> temp = _changedPaths;
_changedPaths = _currentChangedPaths;
_currentChangedPaths = temp;
// Now raise all the events
foreach (var item in _currentChangedPaths)
{
switch (item.Value)
{
case WatcherChangeTypes.Created:
{
Created?.Invoke(item.Key);
break;
}
case WatcherChangeTypes.Changed:
{
Changed?.Invoke(item.Key);
break;
}
case WatcherChangeTypes.Deleted:
{
Deleted?.Invoke(item.Key);
break;
}
default:
{
throw new Exception("Unknown change type");
}
}
}
}
public event Action<string> Created;
public event Action<string> Changed;
public event Action<string> Deleted;
#region IDisposable Support
private bool _disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
Enabled = false;
_timer?.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
_disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion IDisposable Support
}
}