public sealed class RunicPendingValidation : IRunicPendingValidation, INotifyPropertyChanged Tracks validation in progress per member path, for ViewModels of any MVVM framework. Mark a path pending for the duration of a check with RunicPendingValidation.Begin or RunicPendingValidation.Track, or follow an observable of validation states with RunicPendingValidationExtensions.TrackPending. WPF can bind Pending[UserName] or RunicPendingValidation.HasPending.
Remarks
Paths are counted: a path stays pending until every RunicPendingValidation.Begin for it ended, so overlapping checks of one member do not end each other. It is thread-safe. RunicPendingValidation.PendingChanged and INotifyPropertyChanged.PropertyChanged are raised on the thread that changed the set, outside its lock.
Example
public RunicPendingValidation Pending { get; } = new();
IReadOnlyCollection<string> IRunicPendingValidation.PendingMemberPaths => Pending.PendingMemberPaths;
event EventHandler? IRunicPendingValidation.PendingChanged { add => Pending.PendingChanged += value; remove => Pending.PendingChanged -= value; }
// CommunityToolkit: check the name when it changes. A newer check cancels the older one, so an older result never
// overwrites a newer one.
partial void OnUserNameChanged(string value) => _ = Pending.Track(nameof(UserName), async cancellationToken =>
{
var taken = await _users.ExistsAsync(value, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
NameTaken = taken;
ValidateProperty(UserName, nameof(UserName));
});Constructors
RunicPendingValidation
public RunicPendingValidation() No documentation.
Properties
HasPending
public bool HasPending { get; } Whether the validation of any path is in progress.
this[]
public bool this[string memberPath] { get; } Whether the validation of memberPath is in progress ("": the object).
PendingMemberPaths
public IReadOnlyCollection<string> PendingMemberPaths { get; } The member paths whose validation is in progress: relative CLR property paths as in RunicValidationMessage.MemberPaths (UserName, Address.Postcode, Items[0].Name), and "" for the object itself.
Documentation from IRunicPendingValidation.PendingMemberPaths.
Methods
Begin
public IDisposable Begin(string memberPath = "") Marks memberPath pending until the result is disposed. Disposing it again does nothing.
Parameters
memberPathA relative CLR property path, or
""for the object.
IsPending
public bool IsPending(string memberPath = "") Whether the validation of memberPath is in progress ("": the object).
Track
public Task Track(string memberPath, Func<Task> validate) Marks memberPath pending while validate runs.
Track
public Task<T> Track<T>(string memberPath, Func<Task<T>> validate) Marks memberPath pending while validate runs, and returns its result.
Track
public Task Track(string memberPath, Func<CancellationToken, Task> validate) Marks memberPath pending while validate runs, and cancels the check of the path that a previous call of this overload started, so an older check cannot finish after a newer one and overwrite its result. The returned task is cancelled when a newer check cancelled it.
Track
public Task<T> Track<T>(string memberPath, Func<CancellationToken, Task<T>> validate) Marks memberPath pending while validate runs, cancels the previous check of the path that this overload started, and returns the check's result. See RunicPendingValidation.Track.
Events
PendingChanged
public event EventHandler? PendingChanged Raised when IRunicPendingValidation.PendingMemberPaths changed.
Documentation from IRunicPendingValidation.PendingChanged.
PropertyChanged
public event PropertyChangedEventHandler? PropertyChanged Inherits documentation from INotifyPropertyChanged.PropertyChanged.