Skip to content

Commit

Permalink
Refactor and cancellation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Jan 1, 2025
1 parent 357d1d3 commit 76d0418
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 59 deletions.
17 changes: 4 additions & 13 deletions src/PicView.Avalonia/Navigation/ImageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,6 @@ public async Task NextIteration(NavigateTo navigateTo, CancellationTokenSource c
await TimerIteration(index, cts).ConfigureAwait(false);
}
}

public async Task Next10Iteration(bool forwards, CancellationTokenSource cts)
{
var index = GetIteration(CurrentIndex, forwards ? NavigateTo.Next : NavigateTo.Previous, false, true);
await IterateToIndex(index, cts).ConfigureAwait(false);
}

public async Task Next100Iteration(bool forwards, CancellationTokenSource cts)
{
var index = GetIteration(CurrentIndex, forwards ? NavigateTo.Next : NavigateTo.Previous, false, false, true);
await IterateToIndex(index, cts).ConfigureAwait(false);
}

public async Task IterateToIndex(int index, CancellationTokenSource cts)
{
Expand Down Expand Up @@ -568,7 +556,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
});
}

await PreLoader.PreLoadAsync(CurrentIndex, ImagePaths.Count, IsReversed, ImagePaths, cts.Token)
await PreLoader.PreLoadAsync(CurrentIndex, ImagePaths.Count, IsReversed, ImagePaths)
.ConfigureAwait(false);
}

Expand All @@ -583,6 +571,9 @@ await PreLoader.PreLoadAsync(CurrentIndex, ImagePaths.Count, IsReversed, ImagePa
catch (OperationCanceledException)
{
// Ignore
#if DEBUG
Trace.WriteLine($"{nameof(IterateToIndex)} canceled");
#endif
}
catch (Exception e)
{
Expand Down
94 changes: 54 additions & 40 deletions src/PicView.Avalonia/Navigation/NavigationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,8 @@ public static async Task Navigate(bool next, MainViewModel vm)
}
else
{
await Task.Run(async () =>
{
var navigateTo = next ? NavigateTo.Next : NavigateTo.Previous;

if (_cancellationTokenSource is not null)
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.NextIteration(navigateTo, _cancellationTokenSource).ConfigureAwait(false);
}).ConfigureAwait(false);
var navigateTo = next ? NavigateTo.Next : NavigateTo.Previous;
await CheckCancellationAndStartNextIteration(navigateTo, vm).ConfigureAwait(false);
}
}

Expand All @@ -80,58 +70,52 @@ public static async Task Navigate(int index, MainViewModel vm)
{
return;
}

if (_cancellationTokenSource is not null)
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.IterateToIndex(index, _cancellationTokenSource).ConfigureAwait(false);

await CheckCancellationAndStartIterateToIndex(index, vm).ConfigureAwait(false);
}

public static async Task Next10(MainViewModel vm)
{
if (_cancellationTokenSource is not null)
if (!CanNavigate(vm))
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
return;
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.Next10Iteration(true, _cancellationTokenSource).ConfigureAwait(false);
var currentIndex = vm.ImageIterator.CurrentIndex;
var index = vm.ImageIterator.GetIteration(currentIndex, NavigateTo.Next, false, true);
await CheckCancellationAndStartIterateToIndex(index, vm).ConfigureAwait(false);
}

public static async Task Next100(MainViewModel vm)
{
if (_cancellationTokenSource is not null)
if (!CanNavigate(vm))
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
return;
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.Next100Iteration(true, _cancellationTokenSource).ConfigureAwait(false);
var currentIndex = vm.ImageIterator.CurrentIndex;
var index = vm.ImageIterator.GetIteration(currentIndex, NavigateTo.Next, false, false, true);
await CheckCancellationAndStartIterateToIndex(index, vm).ConfigureAwait(false);
}

public static async Task Prev10(MainViewModel vm)
{
if (_cancellationTokenSource is not null)
if (!CanNavigate(vm))
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
return;
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.Next10Iteration(false, _cancellationTokenSource).ConfigureAwait(false);
var currentIndex = vm.ImageIterator.CurrentIndex;
var index = vm.ImageIterator.GetIteration(currentIndex, NavigateTo.Previous, false, true);
await CheckCancellationAndStartIterateToIndex(index, vm).ConfigureAwait(false);
}

public static async Task Prev100(MainViewModel vm)
{
if (_cancellationTokenSource is not null)
if (!CanNavigate(vm))
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
return;
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.Next100Iteration(false, _cancellationTokenSource).ConfigureAwait(false);
var currentIndex = vm.ImageIterator.CurrentIndex;
var index = vm.ImageIterator.GetIteration(currentIndex, NavigateTo.Previous, false, false, true);
await CheckCancellationAndStartIterateToIndex(index, vm).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -628,6 +612,36 @@ public static async Task LoadPicFromDirectoryAsync(string file, MainViewModel vm
#endregion

#region Private helpers

private static async Task CheckCancellationAndStartNextIteration(NavigateTo navigateTo, MainViewModel vm)
{
await Task.Run(async () =>
{
if (_cancellationTokenSource is not null)
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.NextIteration(navigateTo, _cancellationTokenSource).ConfigureAwait(false);
_cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5));
}).ConfigureAwait(false);
}

private static async Task CheckCancellationAndStartIterateToIndex(int index, MainViewModel vm)
{
await Task.Run(async () =>
{
if (_cancellationTokenSource is not null)
{
await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
}

_cancellationTokenSource = new CancellationTokenSource();
await vm.ImageIterator.IterateToIndex(index, _cancellationTokenSource).ConfigureAwait(false);
_cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(5));
}).ConfigureAwait(false);
}

/// <summary>
/// Gets the list of files in the next or previous folder.
Expand Down
12 changes: 6 additions & 6 deletions src/PicView.Avalonia/Preloading/Preloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,14 @@ public bool Remove(int key, List<string> list)
}
}

public async Task PreLoadAsync(int currentIndex, int count, bool reverse, List<string> list,
CancellationToken cancellationToken = default)
public async Task PreLoadAsync(int currentIndex, int count, bool reverse, List<string> list)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMinutes(5));

try
{
await PreLoadInternalAsync(currentIndex, count, reverse, list);
await PreLoadInternalAsync(currentIndex, count, reverse, list, cts.Token);
}
catch (OperationCanceledException)
{
Expand All @@ -319,7 +318,7 @@ public async Task PreLoadAsync(int currentIndex, int count, bool reverse, List<s
}
}

private async Task PreLoadInternalAsync(int currentIndex, int count, bool reverse, List<string> list)
private async Task PreLoadInternalAsync(int currentIndex, int count, bool reverse, List<string> list, CancellationToken token)
{
if (list == null)
{
Expand Down Expand Up @@ -359,7 +358,8 @@ private async Task PreLoadInternalAsync(int currentIndex, int count, bool revers

var options = new ParallelOptions
{
MaxDegreeOfParallelism = _config.MaxParallelism
MaxDegreeOfParallelism = _config.MaxParallelism,
CancellationToken = token
};

try
Expand Down

0 comments on commit 76d0418

Please sign in to comment.