Files
Khaemhat/src/Khaemhat.Application/Querying/InMemoryListSource.cs
T
Alby96andClaude Opus 5.5 75d60ee4c4 feat: grids, radar and document lines (#28)
Lists need one component for the whole app (SPEC §10.6-§10.7): a list
query model that turns search, filters, sort and paging into SQL or runs
over rows in memory, a KhGrid that pages 100,000 rows while scrolling
(or in pages) with sort, selection, keyboard, inline edit, totals, row
menu, saved column layout and CSV/XLSX export, the KhRadar around it
with search, quick filters, a filter builder and saved filters, a radar
window for F4 lookups, and KhLinesGrid for document lines typed from the
keyboard.

Checked in the browser on the component catalog and the sample document.
Fixed on the way: the grid height lost to the flex basis, a new search
kept the old scroll position, Save of a filter needed a second click,
page shortcuts (Ctrl+Canc) reached the record behind a modal window,
Escape did nothing once focus left a dialog, totals did not follow an
inline edit, and amounts exported as 0 instead of 0,00.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:47:38 +01:00

55 lines
1.8 KiB
C#

namespace Khaemhat.Application.Querying;
/// <summary>
/// A <see cref="ListSource{T}"/> over rows kept in memory (small tables, catalogs, tests). The rows a request selects
/// are kept until the search, filters or ordering change, so scrolling through the pages of 100,000 rows only cuts
/// slices.
/// </summary>
/// <typeparam name="T">The row type.</typeparam>
/// <param name="rows">The rows.</param>
/// <param name="fields">The fields.</param>
public sealed class InMemoryListSource<T>(IReadOnlyList<T> rows, ListFields<T> fields)
where T : class
{
private readonly Lock _lock = new();
private (ListQuery Query, IReadOnlyList<T> Rows)? _last;
/// <summary>The fields.</summary>
public ListFields<T> Fields => fields;
/// <summary>Answers a request.</summary>
/// <param name="query">The request.</param>
/// <param name="cancellationToken">Cancellation.</param>
/// <returns>The page.</returns>
public Task<ListPage<T>> QueryAsync(ListQuery query, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(query);
cancellationToken.ThrowIfCancellationRequested();
return Task.FromResult(fields.Page(Selected(query), query));
}
/// <summary>Every row a request selects, in order, for exports.</summary>
/// <param name="query">The request; its page is ignored.</param>
/// <returns>The rows.</returns>
public IReadOnlyList<T> All(ListQuery query) => Selected(query);
private IReadOnlyList<T> Selected(ListQuery query)
{
lock (_lock)
{
if (_last is { } last && last.Query.SameRows(query))
{
return last.Rows;
}
}
var selected = fields.Select(rows, query);
lock (_lock)
{
_last = (query, selected);
}
return selected;
}
}