RunicQueryCollection<TRow, TKey> class

Namespace Runic.Application.Views in Runic.Application.Views 0.23.0

public sealed class RunicQueryCollection<TRow, TKey> : INotifyPropertyChanged, IDisposable where TKey : notnull

Inherits or implements INotifyPropertyChanged, IDisposable.

A large list that the page sorts, filters and scrolls without holding it: rows stay in an IRunicRowSource, and each presentation loads only the range it shows.

Type parameters

TRow

The row type, a Bridge DTO.

TKey

The row key: a String, Guid or Int32.

Remarks

Expose it as a get-only ViewModel property. The page's state holds only the collection's RunicQueryCollection.Query, RunicQueryCollection.Total and RunicQueryCollection.Version; each page asks for the rows it shows with the generated client's queries.{name}.setRange(start, count), and .NET answers that page alone (query-collections/1). Two pages that scroll the same list therefore don't move each other's rows. The query is ViewModel state that every presentation shares, like any other member: a sort or filter one page sets shows on every page, and the ViewModel can read it. A ViewModel per presentation gives each its own query.

Loads are coalesced, bounded and cancellable: the collection reads the source in pages of RunicQueryCollectionOptions.PageSize rows, at most RunicQueryCollectionOptions.MaximumConcurrentLoads at a time, shares a page that several requests need, keeps at most RunicQueryCollectionOptions.MaximumCachedPages pages, and cancels a load once no request waits for it. Memory therefore does not grow with the number of rows.

RunicQueryCollection.SetQuery, RunicQueryCollection.Refresh and RunicQueryCollection.Invalidate raise RunicQueryCollection.Version, which tells every page to load its range again. Call RunicQueryCollection.Invalidate when rows change in place and RunicQueryCollection.Refresh when rows are added or removed, since that moves the rows after them. Property changes are raised in the RunicQueryCollectionOptions.ModelContext, or else in the SynchronizationContext that was current when the collection was created.

A WPF View binds a RunicQueryCollection.CreateView instead, which loads the rows its virtualizing panel asks for.

Constructors

RunicQueryCollection

public RunicQueryCollection(IRunicRowSource<TRow, TKey> source, Func<TRow, TKey> key, RunicQueryCollectionOptions? options = null)

Creates a query collection over source.

Parameters

source

The rows.

key

The key of a row. Keys are unique and do not change while a row exists.

options

The cache, concurrency, initial query and model context, or null for the defaults.

Exceptions

NotSupportedException

TKey is not a string, Guid or Int32.

Properties

Query

public RunicQuery Query { get; }

The sort and filter. The page changes it with setQuery; .NET with RunicQueryCollection.SetQuery.

Statistics

public RunicQueryCollectionStatistics Statistics { get; }

What the collection has loaded and keeps, for tests and diagnostics.

Total

public int? Total { get; }

The number of rows that match RunicQueryCollection.Query, or null until it was counted.

Remarks

The collection counts when a page first loads rows, and again when a page loads rows after a query change or a refresh. After a query change it is null until then; a refresh keeps the old total until the new count arrives.

Version

public long Version { get; }

Increases whenever loaded rows may be out of date: after a query change, RunicQueryCollection.Refresh or RunicQueryCollection.Invalidate. Pages load their range again when it changes.

Methods

CreateView

public RunicQueryCollectionView<TRow> CreateView(SynchronizationContext? context = null)

Creates a list that a WPF ItemsControl with a virtualizing panel binds as its ItemsSource: it has RunicQueryCollection.Total items, loads the pages its panel reads, and shows null for a row until its page arrives. Each View creates its own, as each page has its own range.

Parameters

context

Where the list raises its change notifications: the UI thread's context. Defaults to SynchronizationContext.Current.

Returns

The list. Dispose it with its View.

Dispose

public void Dispose()

Stops loading and cancels the loads in flight.

IndexOfAsync

public ValueTask<int> IndexOfAsync(TKey key, CancellationToken cancellationToken = default)

Returns the position of the row with key in the current query's order, or -1.

Parameters

key

The row's key.

cancellationToken

Cancels the search.

Invalidate

public void Invalidate(TKey key)

Discards the loaded page that holds the row with key, after the row changed in place.

Parameters

key

The row's key.

Invalidate

public void Invalidate(IEnumerable<TKey> keys)

Discards the loaded pages that hold rows with these keys, after the rows changed in place.

Parameters

keys

The rows' keys.

Remarks

The rows stay where they are: when a change moves a row in the query's order, call RunicQueryCollection.Refresh.

LoadAsync

public ValueTask<IReadOnlyList<TRow>> LoadAsync(RunicRowRange range, CancellationToken cancellationToken = default)

Loads the rows of range in the query's order, from the cache where it can.

Parameters

range

The rows; at most RunicQueryCollectionOptions.MaximumRangeCount.

cancellationToken

Cancels this request. A page load stops when no request waits for it any more.

Returns

The rows, fewer at the end of the list.

Refresh

public void Refresh()

Discards every loaded row and the count, as after rows were added or removed. Every page loads its range again.

SetQuery

public void SetQuery(RunicQuery query)

Changes the sort and filter. Every page loads its range again.

Parameters

query

The new query.

Exceptions

ArgumentException

The source rejects the query (IRunicRowSource.Validate).

Events

PropertyChanged

public event PropertyChangedEventHandler? PropertyChanged

Inherits documentation from INotifyPropertyChanged.PropertyChanged.