fix(app): the Edit picker waits for a page the list is loading instead of spinning
To select the sequence edited last, the picker reads pages until it is listed. When the list had asked for a page as it scrolled, LibraryPager.LoadMoreAsync answered nothing at once while that page was in flight, and the loop asked again without ever yielding the UI thread that page needed to finish: a core spun at 100% and the picker never settled (EditSectionTests timed out under load and kept the test process alive). The loop now waits for the page in flight (LibraryPager.WhenLoadedAsync) and stops when a page brings nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f972174c7f
commit
4dd3050334
3 files changed
+69
-4
No files matched your search
@@ -18,6 +18,7 @@ public sealed class LibraryPager(Func<SequenceQuery, CancellationToken, Task<Pag
|
||||
|
||||
private CancellationTokenSource _cancellation = new();
|
||||
private Task<Page<Sequence>>? _pending;
|
||||
private Task<LibraryPage?>? _current;
|
||||
private string? _cursor;
|
||||
|
||||
public int PageSize { get; } = Math.Clamp(pageSize, 1, 1000);
|
||||
@@ -47,16 +48,25 @@ public sealed class LibraryPager(Func<SequenceQuery, CancellationToken, Task<Pag
|
||||
_cursor = null;
|
||||
Loaded = 0;
|
||||
HasMore = true;
|
||||
return LoadAsync(first: true);
|
||||
return _current = LoadAsync(first: true);
|
||||
}
|
||||
|
||||
/// <summary>The next page; null when everything is loaded, a page is already loading, or the filter changed meanwhile.</summary>
|
||||
/// <summary>
|
||||
/// The next page; null when everything is loaded, a page is already loading (at once: <see cref="WhenLoadedAsync"/>
|
||||
/// waits for that page), or the filter changed meanwhile.
|
||||
/// </summary>
|
||||
public Task<LibraryPage?> LoadMoreAsync()
|
||||
{
|
||||
if (_pending is not null || !HasMore || Generation == 0) return Task.FromResult<LibraryPage?>(null);
|
||||
return LoadAsync(first: false);
|
||||
return _current = LoadAsync(first: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Completes once the page in flight, if any, has been delivered or dropped; at once when none is. It never fails: the
|
||||
/// caller that asked for the page gets its failure.
|
||||
/// </summary>
|
||||
public Task WhenLoadedAsync() => _current is { IsCompleted: false } current ? Task.WhenAny(current) : Task.CompletedTask;
|
||||
|
||||
/// <summary>Stops the page in flight (screen closed).</summary>
|
||||
public void Cancel()
|
||||
{
|
||||
|
||||
@@ -163,7 +163,22 @@ public sealed class SequencePickerViewModel : LocalizedViewModel
|
||||
if (filter.Text is not null || Wanted is not { } wanted || Items.Any(i => i.Id == wanted)) return;
|
||||
if (await db.Sequences.GetAsync(wanted) is null && Shell.Library.RemoteSequences.All(s => s.Id != wanted)) return;
|
||||
while (generation == _generation && _pager is { HasMore: true } pager && Items.All(i => i.Id != wanted))
|
||||
await ApplyAsync(pager.LoadMoreAsync(), first: false);
|
||||
{
|
||||
if (pager.IsLoading)
|
||||
{
|
||||
// The list asked for a page as it scrolled: wait for that one. Asking again gets nothing at once, and this
|
||||
// loop would spin on the UI thread, which that page needs to finish.
|
||||
var loaded = pager.WhenLoadedAsync();
|
||||
if (loaded.IsCompleted) break;
|
||||
await loaded;
|
||||
await Task.Yield();
|
||||
continue;
|
||||
}
|
||||
var next = pager.LoadMoreAsync();
|
||||
await ApplyAsync(next, first: false);
|
||||
// Nothing came (the filter changed, the page failed): stop rather than ask again and again.
|
||||
if (!next.IsCompletedSuccessfully || next.Result is null) break;
|
||||
}
|
||||
if (generation == _generation) Preselect();
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +143,46 @@ public sealed class LibraryPagerTests
|
||||
}
|
||||
|
||||
/// <summary>An in-memory library: cursors are offsets, optionally held back by a gate.</summary>
|
||||
[Test]
|
||||
public async Task APageInFlightIsWaitedForNotAskedForAgain()
|
||||
{
|
||||
// The Edit picker reads on until a sequence is listed; the list may ask for a page as it scrolls meanwhile.
|
||||
var source = new FakeSource(30);
|
||||
var pager = new LibraryPager(source.FetchAsync, pageSize: 10);
|
||||
await pager.ResetAsync(new LibraryFilter());
|
||||
Assert.True(pager.WhenLoadedAsync().IsCompleted, "nothing is loading");
|
||||
|
||||
source.Gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var scrolled = pager.LoadMoreAsync();
|
||||
var again = pager.LoadMoreAsync();
|
||||
Assert.True(again.IsCompletedSuccessfully && again.Result is null, "asking again gets nothing at once");
|
||||
var loaded = pager.WhenLoadedAsync();
|
||||
Assert.False(loaded.IsCompleted, "the page in flight is waited for");
|
||||
|
||||
source.Gate.SetResult();
|
||||
await loaded;
|
||||
Assert.True(scrolled.IsCompleted, "the wait ends with that page");
|
||||
Assert.Equal(10, Assert.NotNull(await scrolled).Items.Count);
|
||||
Assert.Equal(2, source.Queries.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task WaitingForAPageThatFailsDoesNotFail()
|
||||
{
|
||||
var source = new FakeSource(30);
|
||||
var pager = new LibraryPager(source.FetchAsync, pageSize: 10);
|
||||
await pager.ResetAsync(new LibraryFilter());
|
||||
source.Gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
source.Fail = true;
|
||||
var page = pager.LoadMoreAsync();
|
||||
var loaded = pager.WhenLoadedAsync();
|
||||
|
||||
source.Gate.SetResult();
|
||||
await loaded;
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => page);
|
||||
Assert.False(pager.HasMore, "a failing page stops the reading");
|
||||
}
|
||||
|
||||
private sealed class FakeSource(int count)
|
||||
{
|
||||
private readonly Guid _archive = Guid.CreateVersion7();
|
||||
|
||||
Reference in new issue
Block a user