27 lines
804 B
C#
27 lines
804 B
C#
namespace Toolkit.Foundation;
|
|
|
|
public class HandlerProvider(SubscriptionCollection subscriptions) :
|
|
IHandlerProvider
|
|
{
|
|
public IEnumerable<object?> Get(Type type,
|
|
object? key = null)
|
|
{
|
|
var d = subscriptions;
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |