RemoteItemMover, Startup/Exit handler refactor

This commit is contained in:
2023-07-26 13:51:17 +02:00
parent 3de71cbdbe
commit 0b36fb939c
23 changed files with 210 additions and 110 deletions

View File

@@ -1,4 +1,5 @@
using FileTime.App.Core.Services;
using FileTime.Core.Extensions;
using Microsoft.Extensions.Logging;
namespace FileTime.GuiApp.Services;
@@ -36,27 +37,30 @@ public class LifecycleService
public async Task ExitAsync()
{
foreach (var exitHandler in _exitHandlers)
var exitCancellation = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var exitHandlerTasks = _exitHandlers.Select(e =>
{
try
{
await exitHandler.ExitAsync();
return e.ExitAsync(exitCancellation.Token);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while running exit handler {Handler}", exitHandler?.GetType().FullName);
_logger.LogError(ex, "Error while running exit handler {Handler}", e.GetType().FullName);
}
return Task.CompletedTask;
});
try
{
await Task.WhenAll(exitHandlerTasks).TimeoutAfter(10000);
}
catch
{
}
foreach (var disposable in
_startupHandlers
.OfType<IDisposable>()
.Concat(
_exitHandlers.OfType<IDisposable>()
)
)
{
disposable.Dispose();
}
exitCancellation.Cancel();
}
}

View File

@@ -7,7 +7,7 @@ using ObservableComputations;
namespace FileTime.GuiApp.Services;
public class RootDriveInfoService : IStartupHandler, IDisposable
public class RootDriveInfoService : IExitHandler
{
private readonly ILocalContentProvider _localContentProvider;
private readonly List<DriveInfo> _rootDrives = new();
@@ -21,13 +21,13 @@ public class RootDriveInfoService : IStartupHandler, IDisposable
InitRootDrives();
var rootDriveInfos = localContentProvider.Items.Selecting<AbsolutePath, (AbsolutePath Path, DriveInfo? Drive)>(
i => MatchRootDrive(i)
)
.Filtering(t => IsNotNull(t.Drive))
.Selecting(t => Resolve(t))
.Filtering(t => t.Item is IContainer)
.Selecting(t => new RootDriveInfo(t.Drive, (IContainer)t.Item!))
.Ordering(d => d.Name);
i => MatchRootDrive(i)
)
.Filtering(t => IsNotNull(t.Drive))
.Selecting(t => Resolve(t))
.Filtering(t => t.Item is IContainer)
.Selecting(t => new RootDriveInfo(t.Drive, (IContainer) t.Item!))
.Ordering(d => d.Name);
rootDriveInfos.For(_rootDriveInfosConsumer);
@@ -51,7 +51,7 @@ public class RootDriveInfoService : IStartupHandler, IDisposable
}
private static bool IsNotNull(object? obj) => obj is not null;
private static (IItem? Item, DriveInfo Drive) Resolve((AbsolutePath Path, DriveInfo? Drive) tuple)
{
var t = Task.Run(async () => await tuple.Path.ResolveAsyncSafe());
@@ -73,6 +73,9 @@ public class RootDriveInfoService : IStartupHandler, IDisposable
return (Path: sourceItem, Drive: rootDrive);
}
public Task InitAsync() => Task.CompletedTask;
public void Dispose() => _rootDriveInfosConsumer.Dispose();
public Task ExitAsync(CancellationToken token = default)
{
_rootDriveInfosConsumer.Dispose();
return Task.CompletedTask;
}
}

View File

@@ -51,7 +51,7 @@ public class WindowsPlacesService : IPlacesService
}
public Dictionary<string, SpecialPathType> GetSpecialPaths()
=> new Dictionary<string, SpecialPathType>
=> new()
{
{KnownFolders.Desktop.Path, SpecialPathType.Desktop},
{KnownFolders.Documents.Path, SpecialPathType.Documents},