@@ -157,20 +157,18 @@ public void UnregisterAll(object recipient)
157
157
// Removes all the lists of registered handlers for the recipient
158
158
foreach ( IMapping mapping in set ! )
159
159
{
160
- if ( mapping . TryRemove ( key ) )
160
+ if ( mapping . TryRemove ( key ) &&
161
+ mapping . Count == 0 )
161
162
{
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 _ ) ;
174
172
}
175
173
}
176
174
@@ -249,19 +247,16 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
249
247
// If the map is empty, remove the recipient entirely from its container
250
248
map . TryRemove ( key ) ;
251
249
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 )
253
258
{
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 _ ) ;
265
260
}
266
261
}
267
262
}
@@ -320,9 +315,8 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
320
315
321
316
HashSet < IMapping > set = this . recipientsMap [ key ] ;
322
317
323
- set . Remove ( mapping ) ;
324
-
325
- if ( set . Count == 0 )
318
+ if ( set . Remove ( mapping ) &&
319
+ set . Count == 0 )
326
320
{
327
321
this . recipientsMap . TryRemove ( key , out _ ) ;
328
322
}
0 commit comments