Improve WatchProperty

This commit is contained in:
tyrrrz
2026-04-03 00:13:40 +03:00
parent 1f189e5206
commit 295f4cf9a2
4 changed files with 11 additions and 17 deletions

View File

@@ -12,7 +12,7 @@ internal static class NotifyPropertyChangedExtensions
{
public IDisposable WatchProperty<TProperty>(
Expression<Func<TOwner, TProperty>> propertyExpression,
Action callback,
Action<TProperty> callback,
bool watchInitialValue = false
)
{
@@ -20,6 +20,8 @@ internal static class NotifyPropertyChangedExtensions
if (memberExpression?.Member is not PropertyInfo property)
throw new ArgumentException("Provided expression must reference a property.");
var getValue = propertyExpression.Compile();
void OnPropertyChanged(object? sender, PropertyChangedEventArgs args)
{
if (
@@ -27,14 +29,14 @@ internal static class NotifyPropertyChangedExtensions
|| string.Equals(args.PropertyName, property.Name, StringComparison.Ordinal)
)
{
callback();
callback(getValue(owner));
}
}
owner.PropertyChanged += OnPropertyChanged;
if (watchInitialValue)
callback();
callback(getValue(owner));
return Disposable.Create(() => owner.PropertyChanged -= OnPropertyChanged);
}