1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-04-10 11:15:45 +02:00

Bug fixes in ObservableObject.SetAndNotifyOnCompletion

This commit is contained in:
Sergio Pedri 2020-04-16 19:11:49 +02:00
parent 0909506a20
commit e7fc8d7842

View File

@ -107,7 +107,7 @@ protected bool Set<T>(ref T field, T newValue, [CallerMemberName] string propert
/// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised
/// if the current and new value for the target property are the same.
/// </remarks>
protected bool SetAndNotifyOnCompletion<TTask>(Expression<Func<TTask>> field, TTask newValue, [CallerMemberName] string propertyName = null!)
protected bool SetAndNotifyOnCompletion<TTask>(Expression<Func<TTask?>> field, TTask? newValue, [CallerMemberName] string propertyName = null!)
where TTask : Task
{
// Get the target field to set
@ -119,9 +119,9 @@ protected bool SetAndNotifyOnCompletion<TTask>(Expression<Func<TTask>> field, TT
return false;
}
TTask oldTask = (TTask)fieldInfo.GetValue(this);
TTask? oldTask = (TTask?)fieldInfo.GetValue(this);
if (EqualityComparer<TTask>.Default.Equals(oldTask, newValue))
if (EqualityComparer<TTask?>.Default.Equals(oldTask, newValue))
{
return false;
}
@ -132,6 +132,11 @@ protected bool SetAndNotifyOnCompletion<TTask>(Expression<Func<TTask>> field, TT
OnPropertyChanged(propertyName);
if (newValue is null)
{
return true;
}
/* We use a local async function here so that the main method can
* remain synchronous and return a value that can be immediately
* used by the caller. This mirrors Set<T>(ref T, T, string). */
@ -146,7 +151,7 @@ async Task MonitorTaskAsync()
{
}
TTask currentTask = (TTask)fieldInfo.GetValue(this);
TTask? currentTask = (TTask?)fieldInfo.GetValue(this);
// Only notify if the property hasn't changed
if (ReferenceEquals(newValue, currentTask))