public void AddEvent(WatcherChangeTypes type, string newLocation, string fullFilename) {
Debug.WriteLine("ACMGR: WatchingTask.AddEvent({0}, {1})", type, newLocation);
// try to collapse new entry with the last one (mostly for optimization purposes)
if (_queue.Any()) {
var last = _queue.Last();
switch (type) {
case WatcherChangeTypes.Created:
if (last.Type == WatcherChangeTypes.Renamed) {
// RENAMED-CREATED occasion
_queue.Enqueue(new WatchingChange {
Type = WatcherChangeTypes.Created
});
} else if (last.Type == WatcherChangeTypes.Deleted) {
// DELETED-CREATED occasion, replace by CHANGED
// why we clear queue so easily? maybe there is something before DELETED?
// well, take a look at the next case
_queue.Clear();
_queue.Enqueue(new WatchingChange { Type = WatcherChangeTypes.Changed });
}
// CREATED-CREATED, CHANGED-CREATED ignored
break;
case WatcherChangeTypes.Deleted:
if (last.Type == WatcherChangeTypes.Created) {
// special CREATED-DELETED occasion, this way AcManager won’t be bothered
_queue.Clear();
} else if (last.Type == WatcherChangeTypes.Changed) {
// CHANGED-DELETED case, remove useless CHANGED
_queue.Clear();
_queue.Enqueue(new WatchingChange { Type = WatcherChangeTypes.Deleted });
}
// RENAMED-DELETED and DELETED-DELETED ignored
break;
case WatcherChangeTypes.Renamed:
// sort of the most important case, outweighs everything else
_queue.Clear();
_queue.Enqueue(new WatchingChange { Type = WatcherChangeTypes.Renamed, NewLocation = newLocation });
break;
case WatcherChangeTypes.All:
// what does it mean? all?
break;
case WatcherChangeTypes.Changed:
// some file inside was changed
// if last entry is CHANGED too, we should add FULL_FILENAME for smart reload
// FULL_FILENAME=null means that changes can’t be processes with smart reload
if (last.Type == WatcherChangeTypes.Changed && last.FullFilename != null) {
if (fullFilename == null) {
last.FullFilename = null;
} else if (!_queue.Any(x => string.Equals(x.FullFilename, fullFilename,
StringComparison.OrdinalIgnoreCase))) {
_queue.Enqueue(new WatchingChange {
Type = WatcherChangeTypes.Changed,
FullFilename = fullFilename
});
}
}
break;
default:
throw new ArgumentOutOfRangeException();
}
_delay = true;
} else {
// if queue is empty, add a new entry to queue and run start processing queue
_queue.Enqueue(new WatchingChange {
Type = type,
FullFilename = fullFilename,
NewLocation = newLocation
});
_delay = true;
Action();
}
}
}