Select item upon entering container

This commit is contained in:
2022-05-24 18:40:24 +02:00
parent 8167909781
commit c08e345c2f
10 changed files with 127 additions and 25 deletions

View File

@@ -36,16 +36,16 @@ public static class DynamicDataExtensions
public void OnCompleted()
{
Disposable?.Dispose();
_taskCompletionSource.SetResult(default(TTaskResult));
_taskCompletionSource.SetResult(default);
}
}
public static async Task<IEnumerable<AbsolutePath>?> GetItemsAsync(
this IObservable<IObservable<IChangeSet<AbsolutePath>>?> stream)
=> await GetItemsAsync(stream
.Select(s =>
s is null
? new SourceList<AbsolutePath>().Connect().StartWithEmpty().ToCollection()
.Select(s =>
s is null
? new SourceList<AbsolutePath>().Connect().StartWithEmpty().ToCollection()
: s.ToCollection())
.Switch());
@@ -60,13 +60,12 @@ public static class DynamicDataExtensions
var context = new DisposableContext<IReadOnlyCollection<AbsolutePath>, IEnumerable<AbsolutePath>?>(r => r,
taskCompletionSource);
var disposable = stream
context.Disposable = stream
.Subscribe(
context.OnNext,
context.OnError,
context.OnCompleted
);
context.Disposable = disposable;
return taskCompletionSource.Task;
}

View File

@@ -0,0 +1,16 @@
namespace FileTime.Core.Extensions;
public static class TaskExtensions
{
public static async Task<T?> AwaitWithTimeout<T>(this Task<T> task, int timeout, T? defaultValue = default)
{
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
{
return task.Result;
}
else
{
return defaultValue;
}
}
}

View File

@@ -0,0 +1,54 @@
using FileTime.Core.Models;
namespace FileTime.Core.Helper;
public static class PathHelper
{
public static string GetLongerPath(string? oldPath, string? newPath)
{
var oldPathParts = oldPath?.Split(Constants.SeparatorChar) ?? Array.Empty<string>();
var newPathParts = newPath?.Split(Constants.SeparatorChar) ?? Array.Empty<string>();
var commonPathParts = new List<string>();
var max = oldPathParts.Length > newPathParts.Length ? oldPathParts.Length : newPathParts.Length;
for (var i = 0; i < max; i++)
{
if (newPathParts.Length <= i)
{
commonPathParts.AddRange(oldPathParts.Skip(i));
break;
}
else if (oldPathParts.Length <= i || oldPathParts[i] != newPathParts[i])
{
commonPathParts.AddRange(newPathParts.Skip(i));
break;
}
else if (oldPathParts[i] == newPathParts[i])
{
commonPathParts.Add(oldPathParts[i]);
}
}
return string.Join(Constants.SeparatorChar, commonPathParts);
}
public static string GetCommonPath(string? path1, string? path2)
{
var path1Parts = path1?.Split(Constants.SeparatorChar) ?? Array.Empty<string>();
var path2Parts = path2?.Split(Constants.SeparatorChar) ?? Array.Empty<string>();
var commonPathParts = new List<string>();
var max = path1Parts.Length > path2Parts.Length ? path2Parts.Length : path1Parts.Length;
for (var i = 0; i < max; i++)
{
if (path1Parts[i] != path2Parts[i]) break;
commonPathParts.Add(path1Parts[i]);
}
return string.Join(Constants.SeparatorChar, commonPathParts);
}
}

View File

@@ -9,13 +9,12 @@ public class CommandTimeState
public bool ForceRun { get; set; }
public ExecutionState ExecutionState { get; set; }
public CommandTimeState(ICommand command, PointInTime? startTime)
public CommandTimeState(ICommand command)
{
Command = command;
Task.Run(async () => await UpdateState(startTime)).Wait();
}
public async Task UpdateState(PointInTime? startPoint)
public async Task UpdateStateAsync(PointInTime? startPoint)
{
CanRun = startPoint == null ? CanCommandRun.False : await Command.CanRun(startPoint);
}

View File

@@ -31,7 +31,8 @@ public class ParallelCommands
var currentTime = startTime;
foreach (var command in commands)
{
CommandTimeState commandTimeState = new(command, currentTime);
var commandTimeState = new CommandTimeState(command);
await commandTimeState.UpdateStateAsync(currentTime);
if (currentTime != null)
{
var canRun = await command.CanRun(currentTime);
@@ -53,7 +54,9 @@ public class ParallelCommands
public async Task AddCommand(ICommand command)
{
_commands.Add(new CommandTimeState(command, Result));
var commandTimeState = new CommandTimeState(command);
await commandTimeState.UpdateStateAsync(Result);
_commands.Add(commandTimeState);
if (Result != null)
{
Result = await command.SimulateCommand(Result);
@@ -63,15 +66,15 @@ public class ParallelCommands
public async Task<PointInTime?> RefreshResult(PointInTime? startPoint)
{
var result = startPoint;
foreach (var command in _commands)
foreach (var commandTimeState in _commands)
{
await command.UpdateState(result);
await commandTimeState.UpdateStateAsync(result);
if (result != null)
{
var canRun = await command.Command.CanRun(result);
if (canRun == CanCommandRun.True || (canRun == CanCommandRun.Forcable && command.ForceRun))
var canRun = await commandTimeState.Command.CanRun(result);
if (canRun == CanCommandRun.True || (canRun == CanCommandRun.Forcable && commandTimeState.ForceRun))
{
result = await command.Command.SimulateCommand(result);
result = await commandTimeState.Command.SimulateCommand(result);
}
else
{