This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix register/unregister issues of WeakReferenceMessenger #4082
Merged
5 commits merged into
CommunityToolkit:main
from
steinhilber:fix-weakreferencemessenger-bug
Jul 5, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04977e1
Fix register/unregister issues of WeakReferenceMessenger
steinhilber 05d95f8
Update UnitTests/UnitTests.Shared/Mvvm/Test_Messenger.cs
steinhilber 928fc47
Update UnitTests/UnitTests.Shared/Mvvm/Test_Messenger.cs
steinhilber 0927e56
Merge branch 'main' into fix-weakreferencemessenger-bug
steinhilber 805438c
Merge branch 'main' into fix-weakreferencemessenger-bug
steinhilber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -511,6 +511,32 @@ void Test() | |
| messenger.Cleanup(); | ||
| } | ||
|
|
||
| // See https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/4081 | ||
| [TestCategory("Mvvm")] | ||
steinhilber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [TestMethod] | ||
| [DataRow(typeof(StrongReferenceMessenger))] | ||
| [DataRow(typeof(WeakReferenceMessenger))] | ||
| public void Test_Messenger_RegisterMultiple_UnregisterSingle(Type type) | ||
| { | ||
| var messenger = (IMessenger)Activator.CreateInstance(type); | ||
|
|
||
| var recipient1 = new object(); | ||
| var recipient2 = new object(); | ||
|
|
||
| int handler1CalledCount = 0; | ||
| int handler2CalledCount = 0; | ||
|
|
||
| messenger.Register<object, MessageA>(recipient1, (r, m) => { handler1CalledCount++; }); | ||
| messenger.Register<object, MessageA>(recipient2, (r, m) => { handler2CalledCount++; }); | ||
|
|
||
| messenger.UnregisterAll(recipient2); | ||
|
|
||
| messenger.Send(new MessageA()); | ||
|
|
||
| Assert.AreEqual(1, handler1CalledCount); | ||
| Assert.AreEqual(0, handler2CalledCount); | ||
| } | ||
|
|
||
| public sealed class RecipientWithNoMessages | ||
| { | ||
| public int Number { get; set; } | ||
|
|
@@ -550,4 +576,4 @@ public sealed class MessageB | |
| { | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michael-hawker Just a note: this is the same issue you had raised as well while reviewing #3893: without some automated process to either fix the formatting or at least validate that the state is kept consistent, it's really really easy to reintroduce differences in whitespace formatting while making changes (especially because VS sometimes applies these changes automatically) and still end up with the same situation we had before. As in, the changes in #3893 aren't really that easily maintainable in the long term right now without still ending up with some inconsistencies here and there by accident 🤔 |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.