From 760ce933bd5c1208ab22215587d66a58859aa7db Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sun, 29 Sep 2024 13:10:39 +0100 Subject: [PATCH] Added DirectoryObserver --- Toolkit.Foundation/DirectoryObserver.cs | 54 ++++++++++++++ Toolkit.Foundation/IMediator.cs | 4 +- Toolkit.Foundation/Mediator.cs | 74 ++++++++++--------- Toolkit.Foundation/MediatorExtensions.cs | 5 -- .../TabStrip/TabStrip.axaml | 8 +- 5 files changed, 96 insertions(+), 49 deletions(-) create mode 100644 Toolkit.Foundation/DirectoryObserver.cs delete mode 100644 Toolkit.Foundation/MediatorExtensions.cs diff --git a/Toolkit.Foundation/DirectoryObserver.cs b/Toolkit.Foundation/DirectoryObserver.cs new file mode 100644 index 0000000..188f1f1 --- /dev/null +++ b/Toolkit.Foundation/DirectoryObserver.cs @@ -0,0 +1,54 @@ +using System.Collections.Concurrent; + +namespace Toolkit.Foundation; + +public class DirectoryObserver +{ + public static async Task EnumerateFiles(string path, + string[] filter, + int count, + CancellationToken cancellationToken = default) + { + string[] files = []; + HashSet extensions = filter.Select(x => $".{x.ToLower()}").ToHashSet(); + + bool IsBatchComplete() + { + files = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly) + .Where(x => extensions.Contains(Path.GetExtension(x).ToLower())) + .ToArray(); + + if (files.Length != count) + { + return false; + } + + ConcurrentBag fileAccessResults = []; + Parallel.ForEach(files, (file) => + { + try + { + using FileStream fileStream = new(file, FileMode.Open, FileAccess.Read, FileShare.None); + fileAccessResults.Add(true); + } + catch (IOException) + { + fileAccessResults.Add(false); + } + }); + + return fileAccessResults.All(result => result); + } + + await Task.Run(async () => + { + while (!IsBatchComplete()) + { + cancellationToken.ThrowIfCancellationRequested(); + await Task.Delay(5000, cancellationToken); + } + }, cancellationToken); + + return files; + } +} diff --git a/Toolkit.Foundation/IMediator.cs b/Toolkit.Foundation/IMediator.cs index a47d276..6a8cdc3 100644 --- a/Toolkit.Foundation/IMediator.cs +++ b/Toolkit.Foundation/IMediator.cs @@ -12,12 +12,12 @@ public interface IMediator CancellationToken cancellationToken = default) where TMessage : notnull; - IAsyncEnumerable HandleManyAsync(Type responseType, + IAsyncEnumerable HandleAsyncMany(Type responseType, object message, object? key = null, CancellationToken cancellationToken = default); - IAsyncEnumerable HandleManyAsync(TMessage message, + IAsyncEnumerable HandleAsyncMany(TMessage message, object? key = null, CancellationToken cancellationToken = default) where TMessage : notnull; diff --git a/Toolkit.Foundation/Mediator.cs b/Toolkit.Foundation/Mediator.cs index de1dbfc..b3731a1 100644 --- a/Toolkit.Foundation/Mediator.cs +++ b/Toolkit.Foundation/Mediator.cs @@ -23,7 +23,7 @@ public class Mediator(IHandlerProvider handlerProvider, MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [message.GetType(), typeof(CancellationToken)]); if (handleMethod is not null) { - return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + return await (Task)handleMethod.Invoke(handler, [message, cancellationToken])!; } } @@ -47,7 +47,7 @@ public class Mediator(IHandlerProvider handlerProvider, if (handleMethod is not null) { - dynamic task = handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + dynamic task = handleMethod.Invoke(handler, [message, cancellationToken])!; await task; return task.Result; @@ -57,35 +57,7 @@ public class Mediator(IHandlerProvider handlerProvider, return default; } - public async Task> HandleMany(Type responseType, - object message, - object? key = null, - CancellationToken cancellationToken = default) - { - List responses = []; - await foreach (object? response in HandleManyAsync(responseType, message, key, cancellationToken)) - { - responses.Add(response); - } - - return responses; - } - - public async Task> HandleMany(TMessage message, - object? key = null, - CancellationToken cancellationToken = default) - where TMessage : notnull - { - List responses = []; - await foreach (TResponse? response in HandleManyAsync(message, key, cancellationToken)) - { - responses.Add(response); - } - - return responses; - } - - public async IAsyncEnumerable HandleManyAsync(Type responseType, + public async IAsyncEnumerable HandleAsyncMany(Type responseType, object message, object? key = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) @@ -102,12 +74,12 @@ public class Mediator(IHandlerProvider handlerProvider, if (handleMethod is not null) { - yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + yield return await (Task)handleMethod.Invoke(handler, [message, cancellationToken])!; } } } - public async IAsyncEnumerable HandleManyAsync(TMessage message, + public async IAsyncEnumerable HandleAsyncMany(TMessage message, object? key = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) where TMessage : notnull @@ -122,11 +94,38 @@ public class Mediator(IHandlerProvider handlerProvider, MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [message.GetType(), typeof(CancellationToken)]); if (handleMethod is not null) { - yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + yield return await (Task)handleMethod.Invoke(handler, [message, cancellationToken])!; } } } + public async Task> HandleMany(Type responseType, + object message, + object? key = null, + CancellationToken cancellationToken = default) + { + List responses = []; + await foreach (object? response in HandleAsyncMany(responseType, message, key, cancellationToken)) + { + responses.Add(response); + } + + return responses; + } + + public async Task> HandleMany(TMessage message, + object? key = null, + CancellationToken cancellationToken = default) + where TMessage : notnull + { + List responses = []; + await foreach (TResponse? response in HandleAsyncMany(message, key, cancellationToken)) + { + responses.Add(response); + } + + return responses; + } private List GetHandlers(object message, Type handlerWrapperType, object? key) @@ -155,8 +154,11 @@ public class Mediator(IHandlerProvider handlerProvider, provider.GetServices(handlerWrapperType); AddHandlers(keyedServices); - IEnumerable additionalHandlers = handlerProvider.Get(key); - AddHandlers(additionalHandlers); + if (key is not null) + { + IEnumerable additionalHandlers = handlerProvider.Get(key); + AddHandlers(additionalHandlers); + } return handlers.SelectMany(entry => entry.Value).ToList(); } diff --git a/Toolkit.Foundation/MediatorExtensions.cs b/Toolkit.Foundation/MediatorExtensions.cs deleted file mode 100644 index e6965fb..0000000 --- a/Toolkit.Foundation/MediatorExtensions.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace Toolkit.Foundation; - -public static class MediatorExtensions -{ -} \ No newline at end of file diff --git a/Toolkit.UI.Controls.Avalonia/TabStrip/TabStrip.axaml b/Toolkit.UI.Controls.Avalonia/TabStrip/TabStrip.axaml index f3c3f2f..deaba4d 100644 --- a/Toolkit.UI.Controls.Avalonia/TabStrip/TabStrip.axaml +++ b/Toolkit.UI.Controls.Avalonia/TabStrip/TabStrip.axaml @@ -3,7 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:CompileBindings="True"> - + @@ -45,7 +45,7 @@ - + @@ -66,7 +66,6 @@ - - - -