WIP to allow mediter handlers to be subscribed to

This commit is contained in:
TheXamlGuy
2024-05-14 22:49:16 +01:00
parent 32829d1cf3
commit 7e57c0d28d
15 changed files with 81 additions and 83 deletions
+26
View File
@@ -0,0 +1,26 @@
namespace Toolkit.Foundation;
public class HandlerProvider(SubscriptionCollection subscriptions) :
IHandlerProvider
{
public IEnumerable<object?> Get(Type type,
object key)
{
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
{
foreach (WeakReference weakRef in subscribers.ToArray())
{
object? target = weakRef.Target;
if (target != null)
{
yield return target;
}
else
{
subscribers.Remove(weakRef);
}
}
}
}
}