Skip to content

Commit 9d8cff6

Browse files
committed
Fixed a merge error, minor code refactoring
1 parent 70d1986 commit 9d8cff6

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

Microsoft.Toolkit.Mvvm/Messaging/Internals/Microsoft.Collections.Extensions/DictionarySlim{TKey,TValue}.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ public void Clear()
130130
this.entries = InitialEntries;
131131
}
132132

133-
/// <inheritdoc/>
133+
/// <summary>
134+
/// Checks whether or not the dictionary contains a pair with a specified key.
135+
/// </summary>
136+
/// <param name="key">The key to look for.</param>
137+
/// <returns>Whether or not the key was present in the dictionary.</returns>
134138
public bool ContainsKey(TKey key)
135139
{
136140
Entry[] entries = this.entries;
@@ -176,7 +180,18 @@ public bool TryGetValue(TKey key, out TValue? value)
176180
}
177181

178182
/// <inheritdoc/>
179-
public bool TryRemove(TKey key, out object? result)
183+
public bool TryRemove(TKey key)
184+
{
185+
return TryRemove(key, out _);
186+
}
187+
188+
/// <summary>
189+
/// Tries to remove a value with a specified key, if present.
190+
/// </summary>
191+
/// <param name="key">The key of the value to remove.</param>
192+
/// <param name="result">The removed value, if it was present.</param>
193+
/// <returns>Whether or not the key was present.</returns>
194+
public bool TryRemove(TKey key, out TValue? result)
180195
{
181196
Entry[] entries = this.entries;
182197
int bucketIndex = key.GetHashCode() & (this.buckets.Length - 1);

Microsoft.Toolkit.Mvvm/Messaging/Internals/Microsoft.Collections.Extensions/IDictionarySlim{TKey}.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,11 @@ namespace Microsoft.Collections.Extensions
1313
internal interface IDictionarySlim<in TKey> : IDictionarySlim
1414
where TKey : IEquatable<TKey>
1515
{
16-
/// <summary>
17-
/// Checks whether or not the dictionary contains a pair with a specified key.
18-
/// </summary>
19-
/// <param name="key">The key to look for.</param>
20-
/// <returns>Whether or not the key was present in the dictionary.</returns>
21-
public bool ContainsKey(TKey key);
22-
2316
/// <summary>
2417
/// Tries to remove a value with a specified key, if present.
2518
/// </summary>
2619
/// <param name="key">The key of the value to remove.</param>
27-
/// <param name="result">The removed value, if it was present.</param>
2820
/// <returns>Whether or not the key was present.</returns>
29-
bool TryRemove(TKey key, out object? result);
21+
bool TryRemove(TKey key);
3022
}
3123
}

Microsoft.Toolkit.Mvvm/Messaging/Messenger.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ 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, out _))
160+
if (mapping.TryRemove(key))
161161
{
162162
if (mapping.Count == 0)
163163
{
@@ -243,11 +243,11 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
243243

244244
// Try to remove the registered handler for the input token,
245245
// for the current message type (unknown from here).
246-
if (holder.TryRemove(token, out _) &&
246+
if (holder.TryRemove(token) &&
247247
holder.Count == 0)
248248
{
249249
// If the map is empty, remove the recipient entirely from its container
250-
map.TryRemove(key, out _);
250+
map.TryRemove(key);
251251

252252
if (map.Count == 0)
253253
{

Microsoft.Toolkit.Mvvm/Messaging/WeakRefMessenger.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public bool IsRegistered<TMessage, TToken>(object recipient, TToken token)
6565
return
6666
this.recipientsMap.TryGetValue(type2, out ConditionalWeakTable<object, IDictionarySlim>? table) &&
6767
table!.TryGetValue(recipient, out IDictionarySlim mapping) &&
68-
Unsafe.As<IDictionarySlim<TToken>>(mapping).ContainsKey(token);
68+
Unsafe.As<DictionarySlim<TToken, object>>(mapping).ContainsKey(token);
6969
}
7070
}
7171

@@ -133,7 +133,7 @@ public void UnregisterAll<TToken>(object recipient, TToken token)
133133
if (enumerator.Key.TToken == typeof(TToken) &&
134134
enumerator.Value.TryGetValue(recipient, out IDictionarySlim mapping))
135135
{
136-
Unsafe.As<IDictionarySlim<TToken>>(mapping).TryRemove(token, out _);
136+
Unsafe.As<DictionarySlim<TToken, object>>(mapping).TryRemove(token, out _);
137137
}
138138
}
139139
}
@@ -156,7 +156,7 @@ public void Unregister<TMessage, TToken>(object recipient, TToken token)
156156
if (enumerator.Key.Equals(type2) &&
157157
enumerator.Value.TryGetValue(recipient, out IDictionarySlim mapping))
158158
{
159-
Unsafe.As<IDictionarySlim<TToken>>(mapping).TryRemove(token, out _);
159+
Unsafe.As<DictionarySlim<TToken, object>>(mapping).TryRemove(token, out _);
160160
}
161161
}
162162
}
@@ -275,7 +275,7 @@ void IMessenger.Cleanup()
275275
// Remove all the mappings with no handlers left
276276
foreach (Type2 key in type2s.Span)
277277
{
278-
this.recipientsMap.Remove(key);
278+
this.recipientsMap.TryRemove(key, out _);
279279
}
280280
}
281281
}

0 commit comments

Comments
 (0)