From 4dd30503346899eda5d0030b2ffe5c6f402465b9 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Mon, 2 Nov 2026 11:37:27 +0100 Subject: [PATCH] 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 --- src/Chronos.App/Library/LibraryPager.cs | 16 ++++++-- .../Sections/SequencePickerViewModel.cs | 17 +++++++- .../Library/LibraryPagerTests.cs | 40 +++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/Chronos.App/Library/LibraryPager.cs b/src/Chronos.App/Library/LibraryPager.cs index 8d9971de..4536fd6b 100644 --- a/src/Chronos.App/Library/LibraryPager.cs +++ b/src/Chronos.App/Library/LibraryPager.cs @@ -18,6 +18,7 @@ public sealed class LibraryPager(Func>? _pending; + private Task? _current; private string? _cursor; public int PageSize { get; } = Math.Clamp(pageSize, 1, 1000); @@ -47,16 +48,25 @@ public sealed class LibraryPager(FuncThe next page; null when everything is loaded, a page is already loading, or the filter changed meanwhile. + /// + /// The next page; null when everything is loaded, a page is already loading (at once: + /// waits for that page), or the filter changed meanwhile. + /// public Task LoadMoreAsync() { if (_pending is not null || !HasMore || Generation == 0) return Task.FromResult(null); - return LoadAsync(first: false); + return _current = LoadAsync(first: false); } + /// + /// 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. + /// + public Task WhenLoadedAsync() => _current is { IsCompleted: false } current ? Task.WhenAny(current) : Task.CompletedTask; + /// Stops the page in flight (screen closed). public void Cancel() { diff --git a/src/Chronos.App/Sections/SequencePickerViewModel.cs b/src/Chronos.App/Sections/SequencePickerViewModel.cs index a462aa02..4fee505d 100644 --- a/src/Chronos.App/Sections/SequencePickerViewModel.cs +++ b/src/Chronos.App/Sections/SequencePickerViewModel.cs @@ -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(); } diff --git a/tests/Chronos.App.Tests/Library/LibraryPagerTests.cs b/tests/Chronos.App.Tests/Library/LibraryPagerTests.cs index aaf995f2..c1a5a19c 100644 --- a/tests/Chronos.App.Tests/Library/LibraryPagerTests.cs +++ b/tests/Chronos.App.Tests/Library/LibraryPagerTests.cs @@ -143,6 +143,46 @@ public sealed class LibraryPagerTests } /// An in-memory library: cursors are offsets, optionally held back by a gate. + [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(() => page); + Assert.False(pager.HasMore, "a failing page stops the reading"); + } + private sealed class FakeSource(int count) { private readonly Guid _archive = Guid.CreateVersion7();