Skip to content

Commit 6fb377b

Browse files
committed
Remove cached delegate from ObservableObject
1 parent 48c7c73 commit 6fb377b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

CommunityToolkit.Mvvm/ComponentModel/ObservableObject.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ protected bool SetPropertyAndNotifyOnCompletion([NotNull] ref TaskNotifier? task
377377
// instance. This will result in no further allocations after the first time this method is called for a given
378378
// generic type. We only pay the cost of the virtual call to the delegate, but this is not performance critical
379379
// code and that overhead would still be much lower than the rest of the method anyway, so that's fine.
380-
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier(), newValue, static _ => { }, propertyName);
380+
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier(), newValue, null, propertyName);
381381
}
382382

383383
/// <summary>
@@ -441,7 +441,7 @@ protected bool SetPropertyAndNotifyOnCompletion([NotNull] ref TaskNotifier? task
441441
/// </remarks>
442442
protected bool SetPropertyAndNotifyOnCompletion<T>([NotNull] ref TaskNotifier<T>? taskNotifier, Task<T>? newValue, [CallerMemberName] string? propertyName = null)
443443
{
444-
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier<T>(), newValue, static _ => { }, propertyName);
444+
return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier<T>(), newValue, null, propertyName);
445445
}
446446

447447
/// <summary>
@@ -476,10 +476,10 @@ protected bool SetPropertyAndNotifyOnCompletion<T>([NotNull] ref TaskNotifier<T>
476476
/// <typeparam name="TTask">The type of <see cref="Task"/> to set and monitor.</typeparam>
477477
/// <param name="taskNotifier">The field notifier.</param>
478478
/// <param name="newValue">The property's value after the change occurred.</param>
479-
/// <param name="callback">A callback to invoke to update the property value.</param>
479+
/// <param name="callback">(optional) A callback to invoke to update the property value.</param>
480480
/// <param name="propertyName">(optional) The name of the property that changed.</param>
481481
/// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
482-
private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNotifier, TTask? newValue, Action<TTask?> callback, [CallerMemberName] string? propertyName = null)
482+
private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNotifier, TTask? newValue, Action<TTask?>? callback, [CallerMemberName] string? propertyName = null)
483483
where TTask : Task
484484
{
485485
if (ReferenceEquals(taskNotifier.Task, newValue))
@@ -507,7 +507,10 @@ private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNo
507507
// This mirrors the return value of all the other synchronous Set methods as well.
508508
if (isAlreadyCompletedOrNull)
509509
{
510-
callback(newValue);
510+
if (callback is not null)
511+
{
512+
callback(newValue);
513+
}
511514

512515
return true;
513516
}
@@ -537,7 +540,10 @@ async void MonitorTask()
537540
OnPropertyChanged(propertyName);
538541
}
539542

540-
callback(newValue);
543+
if (callback is not null)
544+
{
545+
callback(newValue);
546+
}
541547
}
542548

543549
MonitorTask();

0 commit comments

Comments
 (0)