Skip to content

Commit 8daecad

Browse files
committed
Minor code tweaks
1 parent 3fc26f0 commit 8daecad

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

Microsoft.Toolkit.Mvvm/ComponentModel/ObservableRecipient.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,18 @@ protected virtual void Broadcast<T>(T oldValue, T newValue, string? propertyName
141141
/// </remarks>
142142
protected bool SetProperty<T>(ref T field, T newValue, bool broadcast, [CallerMemberName] string? propertyName = null)
143143
{
144-
return SetProperty(ref field, newValue, EqualityComparer<T>.Default, broadcast, propertyName);
144+
T oldValue = field;
145+
146+
// We duplicate the code as in the base class here to leverage
147+
// the intrinsics support for EqualityComparer<T>.Default.Equals.
148+
bool propertyChanged = SetProperty(ref field, newValue, propertyName);
149+
150+
if (propertyChanged && broadcast)
151+
{
152+
Broadcast(oldValue, newValue, propertyName);
153+
}
154+
155+
return propertyChanged;
145156
}
146157

147158
/// <summary>
@@ -159,21 +170,16 @@ protected bool SetProperty<T>(ref T field, T newValue, bool broadcast, [CallerMe
159170
/// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
160171
protected bool SetProperty<T>(ref T field, T newValue, IEqualityComparer<T> comparer, bool broadcast, [CallerMemberName] string? propertyName = null)
161172
{
162-
if (!broadcast)
163-
{
164-
return SetProperty(ref field, newValue, comparer, propertyName);
165-
}
166-
167173
T oldValue = field;
168174

169-
if (SetProperty(ref field, newValue, comparer, propertyName))
175+
bool propertyChanged = SetProperty(ref field, newValue, comparer, propertyName);
176+
177+
if (propertyChanged && broadcast)
170178
{
171179
Broadcast(oldValue, newValue, propertyName);
172-
173-
return true;
174180
}
175181

176-
return false;
182+
return propertyChanged;
177183
}
178184

179185
/// <summary>
@@ -217,19 +223,14 @@ protected bool SetProperty<T>(T oldValue, T newValue, Action<T> callback, bool b
217223
/// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
218224
protected bool SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> comparer, Action<T> callback, bool broadcast, [CallerMemberName] string? propertyName = null)
219225
{
220-
if (!broadcast)
221-
{
222-
return SetProperty(oldValue, newValue, comparer, callback, propertyName);
223-
}
226+
bool propertyChanged = SetProperty(oldValue, newValue, comparer, callback, propertyName);
224227

225-
if (SetProperty(oldValue, newValue, comparer, callback, propertyName))
228+
if (propertyChanged && broadcast)
226229
{
227230
Broadcast(oldValue, newValue, propertyName);
228-
229-
return true;
230231
}
231232

232-
return false;
233+
return propertyChanged;
233234
}
234235

235236
/// <summary>

0 commit comments

Comments
 (0)