Skip to content

Commit 72a2236

Browse files
committed
Minor code refactoring
1 parent 0e9e7db commit 72a2236

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

Microsoft.Toolkit.Mvvm/Messaging/Messenger.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,18 @@ public void UnregisterAll(object recipient)
157157
// Removes all the lists of registered handlers for the recipient
158158
foreach (IMapping mapping in set!)
159159
{
160-
if (mapping.TryRemove(key))
160+
if (mapping.TryRemove(key) &&
161+
mapping.Count == 0)
161162
{
162-
if (mapping.Count == 0)
163-
{
164-
// Maps here are really of type Mapping<,> and with unknown type arguments.
165-
// If after removing the current recipient a given map becomes empty, it means
166-
// that there are no registered recipients at all for a given pair of message
167-
// and token types. In that case, we also remove the map from the types map.
168-
// The reason for keeping a key in each mapping is that removing items from a
169-
// dictionary (a hashed collection) only costs O(1) in the best case, while
170-
// if we had tried to iterate the whole dictionary every time we would have
171-
// paid an O(n) minimum cost for each single remove operation.
172-
this.typesMap.TryRemove(mapping.TypeArguments, out _);
173-
}
163+
// Maps here are really of type Mapping<,> and with unknown type arguments.
164+
// If after removing the current recipient a given map becomes empty, it means
165+
// that there are no registered recipients at all for a given pair of message
166+
// and token types. In that case, we also remove the map from the types map.
167+
// The reason for keeping a key in each mapping is that removing items from a
168+
// dictionary (a hashed collection) only costs O(1) in the best case, while
169+
// if we had tried to iterate the whole dictionary every time we would have
170+
// paid an O(n) minimum cost for each single remove operation.
171+
this.typesMap.TryRemove(mapping.TypeArguments, out _);
174172
}
175173
}
176174

@@ -249,19 +247,16 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
249247
// If the map is empty, remove the recipient entirely from its container
250248
map.TryRemove(key);
251249

252-
if (map.Count == 0)
250+
// If no handlers are left at all for the recipient, across all
251+
// message types and token types, remove the set of mappings
252+
// entirely for the current recipient, and lost the strong
253+
// reference to it as well. This is the same situation that
254+
// would've been achieved by just calling UnregisterAll(recipient).
255+
if (map.Count == 0 &&
256+
set.Remove(Unsafe.As<IMapping>(map)) &&
257+
set.Count == 0)
253258
{
254-
// If no handlers are left at all for the recipient, across all
255-
// message types and token types, remove the set of mappings
256-
// entirely for the current recipient, and lost the strong
257-
// reference to it as well. This is the same situation that
258-
// would've been achieved by just calling UnregisterAll(recipient).
259-
set.Remove(Unsafe.As<IMapping>(map));
260-
261-
if (set.Count == 0)
262-
{
263-
this.recipientsMap.TryRemove(key, out _);
264-
}
259+
this.recipientsMap.TryRemove(key, out _);
265260
}
266261
}
267262
}
@@ -320,9 +315,8 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
320315

321316
HashSet<IMapping> set = this.recipientsMap[key];
322317

323-
set.Remove(mapping);
324-
325-
if (set.Count == 0)
318+
if (set.Remove(mapping) &&
319+
set.Count == 0)
326320
{
327321
this.recipientsMap.TryRemove(key, out _);
328322
}

0 commit comments

Comments
 (0)