Skip to content

Commit 7e63b1e

Browse files
committed
Minor optimization to WeakReferenceMessenger.Unregister
Specifically the Unregister<TMessage, TToken> overload
1 parent 0c39af9 commit 7e63b1e

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,13 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
147147
lock (this.recipientsMap)
148148
{
149149
Type2 type2 = new(typeof(TMessage), typeof(TToken));
150-
var enumerator = this.recipientsMap.GetEnumerator();
151150

152-
// Traverse all the existing token and message pairs matching the current type
153-
// arguments, and remove all the handlers with a matching token, as above.
154-
while (enumerator.MoveNext())
151+
// Get the target mapping table for the combination of message and token types,
152+
// and remove the handler with a matching token (the entire map), if present.
153+
if (this.recipientsMap.TryGetValue(type2, out RecipientsTable? value) &&
154+
value!.TryGetValue(recipient, out IDictionarySlim? mapping))
155155
{
156-
if (enumerator.Key.Equals(type2) &&
157-
enumerator.Value.TryGetValue(recipient, out IDictionarySlim? mapping))
158-
{
159-
Unsafe.As<DictionarySlim<TToken, object>>(mapping)!.TryRemove(token, out _);
160-
}
156+
Unsafe.As<DictionarySlim<TToken, object>>(mapping)!.TryRemove(token, out _);
161157
}
162158
}
163159
}

0 commit comments

Comments
 (0)