Hook up fswatcher to csv parser, needs proper filename parsing.

This commit is contained in:
2024-09-18 20:34:30 +02:00
parent 802180a26e
commit 275e6de228
6 changed files with 82 additions and 65 deletions

View File

@ -3,28 +3,8 @@ using System.Reactive.Concurrency;
namespace StarsAssistant.Helpers;
public class FsWatcher
public static class FsWatcher
{
/// <summary>
/// Throttle a FileSystemObserver and eliminate all duplicates.
/// </summary>
/// <remarks>
/// Curtesy of https://endjin.com/blog/2024/05/observe-file-system-changes-with-rx-dotnet
/// </remarks>
/// <param name="watcher">An observer created with <c>ObserveFileSystem</c></param>
/// <param name="inactivitySeconds">Throttling window, defaults to 1 second.</param>
/// <param name="scheduler">Scheduler to user for throttling, default is set by <c>Quiescent</c></param>
/// <returns>A throttled observer with duplicate elimination.</returns>
public static IObservable<IEnumerable<FileSystemEventArgs>> ThrottleAndDistinctObserver (
IObservable<FileSystemEventArgs> watcher,
int inactivitySeconds = 1,
IScheduler? scheduler = null)
{
return watcher
.Quiescent(TimeSpan.FromSeconds(inactivitySeconds), scheduler)
.Select(changes => changes.DistinctBy(x => (x.ChangeType, x.FullPath)));
}
/// <summary>
/// Helper to convert a FileSystemWatcher into an obervable stream. See FileSystemWatcher
/// documentation for further details on the parameters.
@ -52,6 +32,7 @@ public class FsWatcher
if (filters != null)
foreach (string filter in filters)
fsw.Filters.Add(filter);
if (notifyFilter != null)
fsw.NotifyFilter = (NotifyFilters) notifyFilter;
fsw.EnableRaisingEvents = true;
@ -90,4 +71,29 @@ public class FsWatcher
.RefCount();
}
/// <summary>
/// Throttle a FileSystemObserver and eliminate all duplicates.
/// </summary>
/// <remarks>
/// Curtesy of https://endjin.com/blog/2024/05/observe-file-system-changes-with-rx-dotnet
/// </remarks>
/// <param name="watcher">An observer created with <c>ObserveFileSystem</c></param>
/// <param name="inactivitySeconds">Throttling window, defaults to 1 second.</param>
/// <param name="scheduler">Scheduler to user for throttling, default is set by <c>Quiescent</c></param>
/// <returns>A throttled observer with duplicate elimination.</returns>
public static IObservable<FileSystemEventArgs> ThrottleAndDistinct (
this IObservable<FileSystemEventArgs> watcher,
int inactivitySeconds = 1,
IScheduler? scheduler = null)
{
return watcher
.Quiescent(TimeSpan.FromSeconds(inactivitySeconds), scheduler)
.SelectMany(
changes => changes
.DistinctBy(x => (x.ChangeType, x.FullPath))
.ToObservable()
)
;
}
}

View File

@ -34,24 +34,4 @@ public static class RxExtensions
return src.Buffer(zeroCrossings);
}
/// <summary>
/// Throttle a FileSystemObserver and eliminate all duplicates.
/// </summary>
/// <remarks>
/// Curtesy of https://endjin.com/blog/2024/05/observe-file-system-changes-with-rx-dotnet
/// </remarks>
/// <param name="watcher">An observer created with <c>ObserveFileSystem</c></param>
/// <param name="inactivitySeconds">Throttling window, defaults to 1 second.</param>
/// <param name="scheduler">Scheduler to user for throttling, default is set by <c>Quiescent</c></param>
/// <returns>A throttled observer with duplicate elimination.</returns>
public static IObservable<IEnumerable<FileSystemEventArgs>> ThrottleAndDistinct (
this IObservable<FileSystemEventArgs> watcher,
int inactivitySeconds = 1,
IScheduler? scheduler = null)
{
return watcher
.Quiescent(TimeSpan.FromSeconds(inactivitySeconds), scheduler)
.Select(changes => changes.DistinctBy(x => (x.ChangeType, x.FullPath)));
}
}