@@ -168,13 +168,13 @@ public void UnregisterAll(object recipient)
168
168
// dictionary (a hashed collection) only costs O(1) in the best case, while
169
169
// if we had tried to iterate the whole dictionary every time we would have
170
170
// paid an O(n) minimum cost for each single remove operation.
171
- this . typesMap . Remove ( mapping . TypeArguments ) ;
171
+ this . typesMap . TryRemove ( mapping . TypeArguments , out _ ) ;
172
172
}
173
173
}
174
174
}
175
175
176
176
// Remove the associated set in the recipients map
177
- this . recipientsMap . Remove ( key ) ;
177
+ this . recipientsMap . TryRemove ( key , out _ ) ;
178
178
}
179
179
}
180
180
@@ -242,26 +242,24 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
242
242
243
243
// Try to remove the registered handler for the input token,
244
244
// for the current message type (unknown from here).
245
- if ( holder . Remove ( token ) )
245
+ if ( holder . TryRemove ( token , out _ ) &&
246
+ holder . Count == 0 )
246
247
{
247
- if ( holder . Count == 0 )
248
- {
249
- // If the map is empty, remove the recipient entirely from its container
250
- map . Remove ( key ) ;
248
+ // If the map is empty, remove the recipient entirely from its container
249
+ map . TryRemove ( key , out _ ) ;
251
250
252
- if ( map . Count == 0 )
251
+ if ( map . Count == 0 )
252
+ {
253
+ // If no handlers are left at all for the recipient, across all
254
+ // message types and token types, remove the set of mappings
255
+ // entirely for the current recipient, and lost the strong
256
+ // reference to it as well. This is the same situation that
257
+ // would've been achieved by just calling UnregisterAll(recipient).
258
+ set . Remove ( Unsafe . As < IMapping > ( map ) ) ;
259
+
260
+ if ( set . Count == 0 )
253
261
{
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 . Remove ( key ) ;
264
- }
262
+ this . recipientsMap . TryRemove ( key , out _ ) ;
265
263
}
266
264
}
267
265
}
@@ -309,25 +307,23 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
309
307
}
310
308
311
309
// Remove the target handler
312
- if ( dictionary ! . Remove ( token ) )
310
+ if ( dictionary ! . TryRemove ( token , out _ ) &&
311
+ dictionary . Count == 0 )
313
312
{
314
313
// If the map is empty, it means that the current recipient has no remaining
315
314
// registered handlers for the current <TMessage, TToken> combination, regardless,
316
315
// of the specific token value (ie. the channel used to receive messages of that type).
317
316
// We can remove the map entirely from this container, and remove the link to the map itself
318
317
// to the current mapping between existing registered recipients (or entire recipients too).
319
- if ( dictionary . Count == 0 )
320
- {
321
- mapping . Remove ( key ) ;
318
+ mapping . TryRemove ( key , out _ ) ;
322
319
323
- HashSet < IMapping > set = this . recipientsMap [ key ] ;
320
+ HashSet < IMapping > set = this . recipientsMap [ key ] ;
324
321
325
- set . Remove ( mapping ) ;
322
+ set . Remove ( mapping ) ;
326
323
327
- if ( set . Count == 0 )
328
- {
329
- this . recipientsMap . Remove ( key ) ;
330
- }
324
+ if ( set . Count == 0 )
325
+ {
326
+ this . recipientsMap . TryRemove ( key , out _ ) ;
331
327
}
332
328
}
333
329
}
0 commit comments