Cancel commands

This commit is contained in:
2023-07-28 13:08:46 +02:00
parent 12c9aa039a
commit ee42e38e45
19 changed files with 75 additions and 18 deletions

View File

@@ -11,4 +11,5 @@ public interface ICommand
Task<CanCommandRun> CanRun(PointInTime currentTime);
Task<PointInTime> SimulateCommand(PointInTime currentTime);
void Cancel();
}

View File

@@ -4,6 +4,6 @@ public interface IContentWriter : IDisposable
{
int PreferredBufferSize { get; }
Task WriteBytesAsync(byte[] data, int? index = null);
Task FlushAsync();
Task WriteBytesAsync(byte[] data, int? index = null, CancellationToken cancellationToken = default);
Task FlushAsync(CancellationToken cancellationToken = default);
}

View File

@@ -31,6 +31,7 @@ public abstract class CommandBase : ICommand
public abstract Task<CanCommandRun> CanRun(PointInTime currentTime);
public abstract Task<PointInTime> SimulateCommand(PointInTime currentTime);
public abstract void Cancel();
protected void SetDisplayLabel(string? displayLabel) => _displayLabel.OnNext(displayLabel ?? string.Empty);
protected void SetDisplayDetailLabel(string? displayLabel) => _displayDetailLabel.OnNext(displayLabel ?? string.Empty);

View File

@@ -14,6 +14,7 @@ public class CopyCommand : CommandBase, ITransportationCommand
private readonly ITimelessContentProvider _timelessContentProvider;
private readonly ICommandSchedulerNotifier _commandSchedulerNotifier;
private readonly ILogger<CopyCommand> _logger;
private readonly CancellationTokenSource _cancellationTokenSource = new();
private readonly List<OperationProgress> _operationProgresses = new();
private readonly BehaviorSubject<OperationProgress?> _currentOperationProgress = new(null);
@@ -94,6 +95,9 @@ public class CopyCommand : CommandBase, ITransportationCommand
return currentTime.WithDifferences(simulateOperation.NewDiffs);
}
public override void Cancel()
=> _cancellationTokenSource.Cancel();
public async Task ExecuteAsync(CopyFunc copy)
{
var currentTime = PointInTime.Present;
@@ -163,6 +167,8 @@ public class CopyCommand : CommandBase, ITransportationCommand
{
foreach (var source in sources)
{
if (_cancellationTokenSource.IsCancellationRequested) return;
var resolvedTarget = (IContainer) await target.ResolveAsync() ?? throw new Exception();
var item = await _timelessContentProvider.GetItemByFullNameAsync(source, currentTime);
@@ -188,12 +194,13 @@ public class CopyCommand : CommandBase, ITransportationCommand
var currentProgress = _operationProgresses.Find(o => o.Key == element.FullName!.Path);
_currentOperationProgress.OnNext(currentProgress);
await copyOperation.CopyAsync(new AbsolutePath(_timelessContentProvider, element), newElementPath, new CopyCommandContext(UpdateProgress, currentProgress));
await copyOperation.CopyAsync(new AbsolutePath(_timelessContentProvider, element), newElementPath, new CopyCommandContext(UpdateProgress, currentProgress, _cancellationTokenSource.Token));
}
}
}
private readonly object _updateProgressLock = new();
private Task UpdateProgress()
{
lock (_updateProgressLock)

View File

@@ -4,13 +4,18 @@ public class CopyCommandContext
{
private readonly Func<Task> _updateProgress;
public CopyCommandContext(Func<Task> updateProgress, OperationProgress? currentProgress)
public CopyCommandContext(
Func<Task> updateProgress,
OperationProgress? currentProgress,
CancellationToken cancellationToken)
{
_updateProgress = updateProgress;
CancellationToken = cancellationToken;
CurrentProgress = currentProgress;
}
public OperationProgress? CurrentProgress { get; }
public CancellationToken CancellationToken { get; }
public async Task UpdateProgressAsync() => await _updateProgress.Invoke();
}

View File

@@ -23,4 +23,8 @@ public class CreateContainerCommand : CreateItemBase
await itemCreator.CreateContainerAsync(resolvedParent.Provider, Parent!.GetChild(NewItemName!));
await _commandSchedulerNotifier.RefreshContainer(Parent);
}
public override void Cancel()
{
}
}

View File

@@ -10,7 +10,7 @@ public class CreateElementCommand : CreateItemBase
private readonly ICommandSchedulerNotifier _commandSchedulerNotifier;
public CreateElementCommand(
ITimelessContentProvider timelessContentProvider,
ITimelessContentProvider timelessContentProvider,
IContentAccessorFactory contentAccessorFactory,
ICommandSchedulerNotifier commandSchedulerNotifier)
: base(timelessContentProvider, contentAccessorFactory)
@@ -23,4 +23,8 @@ public class CreateElementCommand : CreateItemBase
await itemCreator.CreateElementAsync(resolvedParent.Provider, Parent!.GetChild(NewItemName!));
await _commandSchedulerNotifier.RefreshContainer(Parent);
}
public override void Cancel()
{
}
}

View File

@@ -35,6 +35,11 @@ public class DeleteCommand : CommandBase, IExecutableCommand
return Task.FromResult(currentTime);
}
public override void Cancel()
{
//TODO: Implement
}
public async Task Execute()
{
//Calculate

View File

@@ -41,6 +41,11 @@ public class MoveCommand : CommandBase, IExecutableCommand
return Task.FromResult(currentTime);
}
public override void Cancel()
{
//TODO: Implement
}
public async Task Execute()
{
Calculate();

View File

@@ -50,6 +50,8 @@ public class StreamCopyCommandHandler : ICommandHandler
public async Task CopyElement(AbsolutePath sourcePath, AbsolutePath targetPath, CopyCommandContext copyCommandContext)
{
if (copyCommandContext.CancellationToken.IsCancellationRequested) return;
var parent = (IContainer?) (await targetPath.GetParent()!.ResolveAsync())!;
var elementName = targetPath.Path;
var parentChildren = parent.Items.ToList();
@@ -70,11 +72,12 @@ public class StreamCopyCommandHandler : ICommandHandler
do
{
if (copyCommandContext.CancellationToken.IsCancellationRequested) return;
dataRead = await reader.ReadBytesAsync(writer.PreferredBufferSize);
if (dataRead.Length > 0)
{
await writer.WriteBytesAsync(dataRead);
await writer.FlushAsync();
await writer.WriteBytesAsync(dataRead, cancellationToken: copyCommandContext.CancellationToken);
await writer.FlushAsync(copyCommandContext.CancellationToken);
currentProgress += dataRead.LongLength;
if (copyCommandContext.CurrentProgress is not null)
{