Skip to content

Commit

Permalink
WAF Add WeakEvent.TryRemove method
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 22, 2023
1 parent 37dae76 commit 6b91356
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class SelectContactController
private readonly SelectContactViewModel selectContactViewModel;
private readonly DelegateCommand selectContactCommand;
private ObservableListView<Contact> contactsView = null!;
private IWeakEventProxy contactListViewModelPropertyChangedProxy = null!;
private IWeakEventProxy? contactListViewModelPropertyChangedProxy;

[ImportingConstructor]
public SelectContactController(SelectContactViewModel selectContactViewModel, ContactListViewModel contactListViewModel)
Expand Down Expand Up @@ -44,7 +44,7 @@ public void Initialize()

public void Shutdown()
{
contactListViewModelPropertyChangedProxy.Remove();
WeakEvent.TryRemove(ref contactListViewModelPropertyChangedProxy);
contactsView.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Initialize()

public void Shutdown()
{
emailListViewModelPropertyChangedProxy?.Remove();
WeakEvent.TryRemove(ref emailListViewModelPropertyChangedProxy);
// Set the views to null so that the garbage collector can collect them.
emailLayoutViewModel.EmailListView = null;
emailLayoutViewModel.EmailView = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Email Email
set
{
if (email == value) return;
emailPropertyChangedProxy?.Remove();
WeakEvent.TryRemove(ref emailPropertyChangedProxy);
email = value;
emailPropertyChangedProxy = WeakEvent.PropertyChanged.Add(email, EmailPropertyChanged);
UpdateProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ public void WeakEventAddArgumentException()
AssertHelper.ExpectedException<ArgumentNullException>(() => WeakEvent.StaticEventHandler.Add(subscriber.Handler, h => StaticPublisher.Event1 += h, null!));
}

[TestMethod]
public void WeakEventRemoveTest()
{
var subscriber = new Subscriber();
var proxy = WeakEvent.StaticEventHandler.Add(subscriber.Handler, h => StaticPublisher.Event1 += h, h => StaticPublisher.Event1 -= h);
StaticPublisher.RaiseEvent();
Assert.AreEqual(1, subscriber.HandlerCallCount);

WeakEvent.TryRemove(ref proxy);
StaticPublisher.RaiseEvent();
Assert.AreEqual(1, subscriber.HandlerCallCount);
Assert.IsNull(proxy);

WeakEvent.TryRemove(ref proxy);
StaticPublisher.RaiseEvent();
Assert.AreEqual(1, subscriber.HandlerCallCount);
Assert.IsNull(proxy);
}

private static (WeakReference<Manager>, WeakReference<Subscriber>) WeakEventHandlerCore(Manager? manager, Subscriber? subscriber,
int raiseCount = 1, int addCount = 1, bool remove = false, bool removeTwice = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,13 @@ public static IWeakEventProxy Add(INotifyDataErrorInfo source, S.EventHandler<Da
return EventHandler<DataErrorsChangedEventArgs>.Add(source, targetHandler, (s, h) => s.ErrorsChanged += h, (s, h) => s.ErrorsChanged -= h);
}
}

/// <summary>Remove the weak event handler if the passed proxy is not null. And set the proxy to null.</summary>
/// <param name="proxy">The weak event proxy.</param>
public static void TryRemove(ref IWeakEventProxy? proxy)
{
proxy?.Remove();
proxy = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace System.Waf.Applications
/// <typeparam name="T">The type of elements in the collection.</typeparam>
public class ObservableListView<T> : ObservableListViewCore<T>
{
private readonly IWeakEventProxy? originalCollectionChangedProxy;
private IWeakEventProxy? originalCollectionChangedProxy;

/// <summary>Initializes a new instance of the ObservableListView class that represents a view of the specified list.</summary>
/// <param name="originalList">The original list.</param>
Expand Down Expand Up @@ -55,7 +55,7 @@ protected override void OnDispose(bool disposing)
{
if (disposing)
{
originalCollectionChangedProxy?.Remove();
WeakEvent.TryRemove(ref originalCollectionChangedProxy);
}
base.OnDispose(disposing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace System.Waf.Applications
[Obsolete("Use System.Waf.Foundation.SynchronizingList instead.")]
public class SynchronizingCollection<T, TOriginal> : SynchronizingCollectionCore<T, TOriginal>
{
private readonly IWeakEventProxy? originalCollectionChangedProxy;
private IWeakEventProxy? originalCollectionChangedProxy;

/// <summary>Initializes a new instance of the <see cref="SynchronizingCollection{T, TOriginal}"/> class.</summary>
/// <param name="originalCollection">The original collection.</param>
Expand All @@ -40,7 +40,7 @@ protected override void OnDispose(bool disposing)
{
if (disposing)
{
originalCollectionChangedProxy?.Remove();
WeakEvent.TryRemove(ref originalCollectionChangedProxy);
}
base.OnDispose(disposing);
}
Expand Down

0 comments on commit 6b91356

Please sign in to comment.